Image Fundamentals
🎯 Learning Objectives
- Understand how digital images are represented as matrices and tensors
- Differentiate between grayscale, RGB, and RGBA color models
- Perform the 2-D convolution operation by hand
- Describe common kernels: edge detection, blur, and sharpen
- Calculate output dimensions given padding, stride, and kernel size
- Explain max pooling vs average pooling and their roles in CNNs
1. Digital Images
A digital image is a 2-D grid of discrete picture elements called pixels. Each pixel stores one or more intensity values that encode color or brightness information.
1.1 Image Resolution
Resolution is expressed as W × H (width × height in pixels). A 1920 × 1080 image contains 1920 columns and 1080 rows, giving 2,073,600 total pixels.
1.2 Bit Depth
The number of bits used per channel determines the range of intensity values:
- 8-bit: Values 0–255 (most common for display images)
- 16-bit: Values 0–65 535 (medical imaging, HDR)
- 32-bit float: Values in [0, 1] (neural-network inputs after normalisation)
2. Pixel & Colour Representations
2.1 Grayscale
A single channel where 0 = black and 255 = white. The image is an H × W matrix.
Example — 4 × 4 Grayscale Image Matrix
I = | 10 20 30 40 |
| 50 60 70 80 |
| 90 100 110 120 |
|130 140 150 160 |
Pixel at row 0, col 2 has intensity 30.
2.2 RGB
Three channels — Red, Green, Blue — each in [0, 255]. The image is an H × W × 3 tensor. A pure red pixel is (255, 0, 0); white is (255, 255, 255); black is (0, 0, 0).
2.3 RGBA
RGB plus an Alpha (transparency) channel. H × W × 4 tensor. Alpha = 0 means fully transparent; Alpha = 255 means fully opaque.
2.4 Other Colour Spaces
| Colour Space | Channels | Use Case |
|---|---|---|
| RGB | Red, Green, Blue | Display devices, default in most libraries |
| HSV | Hue, Saturation, Value | Colour-based segmentation, object tracking |
| YCbCr | Luminance (Y), Chrominance blue (Cb), Chrominance red (Cr) | JPEG compression, video encoding |
| LAB | Lightness, a* (green-red), b* (blue-yellow) | Perceptually uniform colour differences |
3. Image as a Matrix / Tensor
Mathematically, a single-channel image is a matrix I ∈ ℜH×W. A colour image is a 3-D tensor I ∈ ℜH×W×C. A batch of N images forms a 4-D tensor of shape (N, C, H, W) in PyTorch or (N, H, W, C) in TensorFlow.
4. Convolution Operation on Images
The 2-D convolution (technically cross-correlation in deep learning) slides a small matrix called a kernel (or filter) over the input image and computes element-wise products followed by a sum at each position.
(I * K)(i, j) = ∑m=0k−1 ∑n=0k−1 I(i+m, j+n) · K(m, n)
Worked Example — 3×3 Convolution
Input (5×5): Kernel (3×3): | 1 2 3 0 1 | | 1 0 -1 | | 0 1 2 3 1 | | 1 0 -1 | | 1 0 1 0 2 | | 1 0 -1 | | 2 1 0 1 1 | | 1 0 2 1 0 | Output at (0,0): 1×1 + 2×0 + 3×(−1) + 0×1 + 1×0 + 2×(−1) + 1×1 + 0×0 + 1×(−1) = 1 + 0 − 3 + 0 + 0 − 2 + 1 + 0 − 1 = −4 Output size (no padding, stride 1): (5−3+1) × (5−3+1) = 3 × 3
5. Common Kernels / Filters
5.1 Edge Detection
Highlights intensity gradients. The vertical Sobel kernel detects horizontal edges:
Vertical Sobel: Horizontal Sobel: | -1 0 1 | | -1 -2 -1 | | -2 0 2 | | 0 0 0 | | -1 0 1 | | 1 2 1 |
The Laplacian kernel detects edges in all directions:
| 0 1 0 | | 1 -4 1 | | 0 1 0 |
5.2 Blur (Averaging / Gaussian)
A 3×3 box blur (mean filter) replaces each pixel with the average of its 3×3 neighbourhood:
(1/9) × | 1 1 1 |
| 1 1 1 |
| 1 1 1 |
A Gaussian blur weights the centre pixel more heavily, producing smoother results.
5.3 Sharpen
| 0 -1 0 | | -1 5 -1 | | 0 -1 0 |
Enhances edges by amplifying the centre and subtracting neighbours. The kernel values sum to 1 to preserve overall brightness.
6. Padding and Stride
O = ⌊(n − k + 2p) / s⌋ + 1
Example — Output Size Calculations
| Input n | Kernel k | Padding p | Stride s | Output O |
|---|---|---|---|---|
| 32 | 3 | 0 | 1 | ⌊(32−3+0)/1⌋+1 = 30 |
| 32 | 3 | 1 | 1 | ⌊(32−3+2)/1⌋+1 = 32 (same) |
| 32 | 5 | 0 | 2 | ⌊(32−5+0)/2⌋+1 = 14 |
| 224 | 7 | 3 | 2 | ⌊(224−7+6)/2⌋+1 = 112 |
7. Pooling Operations
Pooling reduces the spatial dimensions of feature maps, decreasing the number of parameters and providing translation invariance.
7.1 Max Pooling
Takes the maximum value in each pooling window. A 2×2 max pool with stride 2 halves each spatial dimension.
Example — 2×2 Max Pooling (stride 2)
Input (4×4): Output (2×2): | 1 3 2 4 | | 5 6 | | 5 2 6 1 | | 8 7 | | 7 8 3 0 | | 2 4 1 7 | Top-left: max(1,3,5,2) = 5 Top-right: max(2,4,6,1) = 6 Bot-left: max(7,8,2,4) = 8 Bot-right: max(3,0,1,7) = 7
7.2 Average Pooling
Takes the mean of all values in each window. Keeps more information but is less robust to noise. Often used as Global Average Pooling (GAP) before the final classifier (e.g., GoogLeNet, ResNet).
| Property | Max Pooling | Average Pooling |
|---|---|---|
| Output value | Maximum in window | Mean of window |
| Gradient flow | Only to the max element | Distributed equally |
| Common use | Intermediate CNN layers | Global average before FC |
| Translation invariance | Strong | Moderate |
O = ⌊(n − k) / s⌋ + 1
8. Basic Image Operations
- Resize: Interpolation methods — nearest-neighbour (fast, blocky), bilinear (smooth, common), bicubic (smoother, slower).
- Crop: Extract a sub-region I[y1:y2, x1:x2]. Used extensively in data augmentation.
- Rotate: Apply a 2-D rotation matrix. Requires interpolation for non-integer pixel locations.
- Flip: Horizontal flip (left-right) or vertical flip (up-down). Very common augmentation.
- Threshold: Convert a grayscale image to binary: pixel = 1 if I(i, j) ≥ T, else 0. Used in segmentation pre-processing.
Example — Binary Thresholding (T = 100)
Input: Output (T = 100): | 50 120 80 | | 0 1 0 | |200 90 30 | | 1 0 0 | |110 100 250 | | 1 1 1 |
9. Data Augmentation for Vision
Augmentation artificially increases the effective size of a training set and improves generalisation:
- Geometric: Random crop, resize, rotation, flip, affine transform
- Colour: Brightness / contrast jitter, hue shift, colour inversion
- Noise: Gaussian noise injection, cutout (random masking), mixup
10. Summary
| Concept | Key Detail |
|---|---|
| Grayscale image | H × W matrix, values 0–255 |
| RGB image | H × W × 3 tensor |
| Convolution formula | ∑∑ I(i+m, j+n) · K(m,n) |
| Output size | ⌊(n − k + 2p) / s⌋ + 1 |
| Max pooling | Takes max in each window; translation invariant |
| Avg pooling | Takes mean; used in GAP layers |
| Sobel kernel | Detects edges via gradient approximation |
| Padding “same” | p = (k−1)/2 keeps output size = input size |