File size: 7,436 Bytes
d962d48
2d190aa
 
 
 
 
 
 
 
d962d48
2d190aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d962d48
2d190aa
 
 
 
 
 
 
 
d962d48
2d190aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d962d48
2d190aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d962d48
2d190aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d962d48
2d190aa
 
 
 
 
 
 
 
 
 
 
d962d48
2d190aa
 
 
 
 
 
d962d48
2d190aa
 
 
 
 
 
 
 
 
 
 
 
d962d48
2d190aa
 
d962d48
2d190aa
 
 
 
 
d962d48
2d190aa
 
 
 
 
 
 
 
 
 
d962d48
2d190aa
d962d48
 
2d190aa
 
 
 
 
 
 
 
d962d48
2d190aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d962d48
2d190aa
 
 
 
 
 
d962d48
2d190aa
 
 
 
 
 
d962d48
2d190aa
 
 
 
 
 
 
d962d48
2d190aa
 
 
 
 
 
 
 
d962d48
2d190aa
 
 
d962d48
2d190aa
 
 
 
 
 
d962d48
2d190aa
 
 
 
 
d962d48
2d190aa
 
d962d48
 
 
 
 
2d190aa
d962d48
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#  Image Sampling and Quantization Demo - Project Summary

## Overview

This interactive educational demo teaches fundamental concepts in digital image processing:
- **Spatial Sampling**: How resolution affects image quality and storage
- **Quantization**: How bit depth impacts color representation
- **Compression**: Differences between PNG (lossless) and JPEG (lossy)

## Key Features

### Interactive Controls
- **Sampling Grid Size Slider** (1-16x): Shows pixelation effect and pixel count reduction
- **Bits per Pixel Slider** (1-8 bits): Demonstrates color depth and posterization
- **JPEG Quality Slider** (1-100): Shows compression artifacts

### Real-Time Feedback
- **Live File Size Estimates**: Shows how settings affect storage requirements
- **Side-by-Side Comparisons**: Original vs. processed images
- **Detailed Calculations**: Transparent math showing how sizes are computed

### Educational Content
- **Three Educational Tabs**: Dedicated sections for Sampling, Quantization, and Compression
- **Visual Examples**: All concepts demonstrated with live image processing
- **Formula Explanations**: Mathematical basis for all calculations

## Educational Objectives

Students will learn:
1. The relationship between sampling rate and image resolution
2. How quantization affects color depth and file size
3. The trade-offs between image quality and storage
4. Differences between lossy and lossless compression
5. How JPEG blocking artifacts occur

## Project Structure

```
sampling-quantization/
β”œβ”€β”€ app.py                  # Main Streamlit application (500+ lines)
β”œβ”€β”€ requirements.txt        # Python dependencies
β”œβ”€β”€ README.md              # Main documentation
β”œβ”€β”€ QUICKSTART.md          # Getting started guide
β”œβ”€β”€ CONTRIBUTING.md        # Contribution guidelines
β”œβ”€β”€ IMAGES.md              # Guide for sample images
β”œβ”€β”€ LICENSE                # MIT License
β”‚
β”œβ”€β”€ Configuration Files:
β”œβ”€β”€ packages.txt           # System dependencies for HF Spaces
β”œβ”€β”€ .python-version        # Python 3.11
β”œβ”€β”€ .gitignore            # Git ignore rules
β”œβ”€β”€ pyproject.toml        # Project metadata
β”œβ”€β”€ Dockerfile            # Docker deployment
β”œβ”€β”€ Makefile              # Convenient make commands
β”‚
└── Scripts:
    β”œβ”€β”€ setup.sh           # Initial setup with venv
    β”œβ”€β”€ run_simple.sh      # Quick local run
    β”œβ”€β”€ deploy.sh          # Deploy to HuggingFace
    └── check_status.sh    # Check deployment status
```

## Quick Start

### For Users (Simplest)
```bash
chmod +x run_simple.sh
./run_simple.sh
```

### Using Make
```bash
make setup    # First time only
make run      # Start the app
```

### Manual Setup
```bash
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
streamlit run app.py
```

## Deployment Options

### 1. Hugging Face Spaces (Recommended)
```bash
./deploy.sh
```
- Free hosting
- Automatic builds
- Share with students via URL
- No server maintenance

### 2. Docker
```bash
docker build -t sampling-demo .
docker run -p 8501:8501 sampling-demo
```

### 3. Local Server
```bash
streamlit run app.py --server.port 8501
```

## Teaching with This Demo

### Suggested Lesson Plan

**Part 1: Sampling (15 minutes)**
1. Start with original image at 1x sampling, 8 bits
2. Gradually increase sampling rate (2x, 4x, 8x, 16x)
3. Discuss: When does text become unreadable?
4. Calculate storage savings

**Part 2: Quantization (15 minutes)**
1. Reset sampling to 1x
2. Reduce bits per pixel (8 to 4 to 2 to 1)
3. Discuss: Color banding, posterization
4. Show grayscale interpretation

**Part 3: Compression (20 minutes)**
1. Apply moderate sampling/quantization
2. Compare PNG vs JPEG at different qualities
3. At JPEG quality < 30: Point out 8x8 blocking
4. Discuss use cases for each format

**Part 4: Interactive Exploration (10 minutes)**
1. Let students experiment with their own images
2. Find optimal settings for different use cases
3. Calculate real-world storage requirements

### Discussion Questions

1. What is the minimum acceptable sampling rate for your use case?
2. How many bits per pixel do you really need for grayscale medical images?
3. When would you choose PNG over JPEG and vice versa?
4. Why do JPEG artifacts appear in 8x8 blocks?
5. What's the total storage for 1000 photos at different settings?

## Technical Details

### Image Processing Pipeline
```
Original Image
    ↓
Spatial Sampling (downsample to upsample with nearest neighbor)
    ↓
Quantization (reduce bits per channel)
    ↓
Compression (PNG lossless or JPEG lossy)
    ↓
Display + File Size Calculation
```

### File Size Calculation
```
Raw Size = (Width / Sampling) x (Height / Sampling) x Channels x Bits / 8

PNG Size β‰ˆ Raw Size x 0.7  (typical compression ratio)
JPEG Size β‰ˆ Raw Size x 0.3  (typical compression ratio)
```

### Key Algorithms
- **Downsampling**: `cv.INTER_AREA` (best quality for reduction)
- **Upsampling**: `cv.INTER_NEAREST` (shows pixelation clearly)
- **Quantization**: `floor(value / step) * step` where `step = 256 / 2^bits`
- **JPEG**: OpenCV's `cv.imencode()` with quality parameter

## Customization

### Change Default Image
Edit `load_sample_image()` in `app.py`:
```python
image_path = hf_hub_download(
    repo_id="your-username/your-dataset",
    filename="your-image.jpg",
    repo_type="dataset",
)
```

### Adjust Slider Ranges
In `app.py`, modify slider parameters:
```python
sampling_rate = st.sidebar.slider(
    "Sampling Grid Size (pixels)",
    min_value=1,      # Change these
    max_value=32,     # Change these
    value=1,
)
```

### Add New Features
See `CONTRIBUTING.md` for guidelines on adding:
- New compression algorithms
- Additional image metrics
- Interactive exercises
- Batch processing

## Performance

- **Load Time**: < 2 seconds on first load (with caching)
- **Interactive Response**: Real-time (< 100ms per slider change)
- **Memory Usage**: ~200-300 MB (depends on image size)
- **Supported Image Sizes**: Up to 4K (auto-resized to 512px for demo)

## Known Limitations

1. Very large images (>10MB) may be slow - auto-resized to 512px
2. JPEG artifact visibility depends on image content
3. File size estimates are approximate (actual compression varies)
4. Generated sample image is simple (encourage uploading real images)

## Educational Resources

Concepts covered align with:
- Digital Image Processing (Gonzalez & Woods)
- Computer Vision fundamentals courses
- Signal processing curricula
- Compression theory courses

## Contributing

We welcome contributions! See `CONTRIBUTING.md` for:
- Bug reports
- Feature requests
- Code contributions
- Documentation improvements
- Educational content enhancements

## License

MIT License - Free for educational and commercial use

## Acknowledgments

- Inspired by interactive teaching tools in computer vision
- Built with Streamlit for rapid prototyping
- OpenCV for image processing
- HuggingFace for free hosting

## Support

- **Issues**: GitHub Issues for bugs and features
- **Questions**: Discussion board for educational questions
- **Documentation**: See README.md, QUICKSTART.md, IMAGES.md

## Success Stories

Perfect for:
- Graduate image analysis courses
- Computer vision fundamentals
- Self-paced online learning
- Workshop demonstrations
- Research group tutorials

--- **Ready to start?** Run `./run_simple.sh` and explore!