Image Basics
Module 2 - Pixels, Color Spaces, and Histograms
Pixels and Channels
A digital image is a 2D array of pixels. Each pixel holds a value representing light intensity. For a grayscale image, one number per pixel (0 = black, 255 = white). For color images, three numbers per pixel - one per channel.
In memory, a 640×480 RGB image is stored as a 3D tensor of shape (480, 640, 3) - height × width × channels.
Pixel Inspector
Move your mouse over the image to inspect pixel values.
Color Spaces
Different color spaces represent color in different ways. The choice affects what operations are easy or hard.
- RGB - Red, Green, Blue. How screens emit light. Not perceptually uniform.
- Grayscale - Luminance only. Computed as:
0.299R + 0.587G + 0.114B - HSV - Hue, Saturation, Value. Hue = color type (0–360°). Great for color-based segmentation.
- Lab (CIELAB) - Perceptually uniform. L = lightness, a = green↔red, b = blue↔yellow. Used in professional color matching.
- YCbCr - Luminance (Y) + chrominance. Used in JPEG compression and video.
Color Space Explorer
| Space | Values |
|---|
Image Histograms
A histogram counts how many pixels have each intensity value (0–255). It summarizes the tonal distribution of an image at a glance.
- Dark image: histogram skewed left (many low values)
- Bright image: histogram skewed right
- High contrast: wide spread, peaks at both ends
- Low contrast / foggy: narrow peak in the middle
Live Histogram Generator
Histogram Equalization
A classic technique to improve contrast: redistribute pixel values so the histogram becomes flat (uniform). Works well on underexposed photos.
| CDF(v) | Cumulative Distribution Function at value v - how many pixels have intensity ≤ v |
| v | a specific pixel intensity level, from 0 (black) to 255 (white) |
| Σ (sigma) | summation - add up all the terms that follow |
| hist(i) | histogram count - how many pixels in the image have intensity exactly equal to i |
| i = 0..v | loop variable: sum from intensity 0 up to and including intensity v |
| CDF(v) / total_pixels × 255 | remapping step - scales the cumulative count to the 0–255 output range |
The equalized value of a pixel v is: round(CDF(v) / total_pixels × 255)
Equalization Demo
Quiz
Check your understanding
1. What is the shape (tensor dimensions) of a 1920×1080 RGB image in memory?
2. You want to detect a red ball regardless of lighting. Which color space is best?
3. A histogram has almost all values concentrated in the range 100–150. What does this suggest?