Image Fundamentals

GATE Relevance: Low–Medium

🎯 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.

Definition — Pixel: The smallest addressable element of a raster image. In a grayscale image each pixel is a single scalar; in a colour image each pixel is a vector of channel values.

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.

Note: In NumPy / PyTorch the shape convention is (H, W, C) — rows first, columns second, channels last. Be careful when interpreting tensor shapes.

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 SpaceChannelsUse Case
RGBRed, Green, BlueDisplay devices, default in most libraries
HSVHue, Saturation, ValueColour-based segmentation, object tracking
YCbCrLuminance (Y), Chrominance blue (Cb), Chrominance red (Cr)JPEG compression, video encoding
LABLightness, a* (green-red), b* (blue-yellow)Perceptually uniform colour differences
Conversion (RGB → Grayscale): Y = 0.299 R + 0.587 G + 0.114 B (ITU-R BT.601 standard). Green contributes most because human eyes are most sensitive to green light.

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.

Definition — Feature Map: The output of applying a convolutional filter to an input. Also called an “activation map.” Each filter produces one feature map.

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.

Definition — 2-D Convolution: For input I and kernel K of size k × k, the output at position (i, j) is:
(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

Definition — Padding: Adding extra pixels (usually zeros) around the border of the input. Valid padding = no padding. Same padding = pad so that the output has the same spatial dimensions as the input.
Definition — Stride: The number of pixels the kernel moves between successive applications. Stride s > 1 down-samples the output.
Output Size Formula: Given input size n, kernel size k, padding p, and stride s, the output dimension is:
O = ⌊(n − k + 2p) / s⌋ + 1

Example — Output Size Calculations

Input nKernel kPadding pStride sOutput O
32301⌊(32−3+0)/1⌋+1 = 30
32311⌊(32−3+2)/1⌋+1 = 32 (same)
32502⌊(32−5+0)/2⌋+1 = 14
224732⌊(224−7+6)/2⌋+1 = 112
⚠ Common Mistake: Forgetting the floor operation when stride > 1. Fractional values are always rounded down.

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).

PropertyMax PoolingAverage Pooling
Output valueMaximum in windowMean of window
Gradient flowOnly to the max elementDistributed equally
Common useIntermediate CNN layersGlobal average before FC
Translation invarianceStrongModerate
Pooling Output Size: Same formula as convolution (with p = 0 typically):
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
⚠ Important: Augmentation is applied only at training time (stochastically). At test/inference time, use the original (possibly centre-cropped) image for deterministic results.

10. Summary

ConceptKey Detail
Grayscale imageH × W matrix, values 0–255
RGB imageH × W × 3 tensor
Convolution formula∑∑ I(i+m, j+n) · K(m,n)
Output size⌊(n − k + 2p) / s⌋ + 1
Max poolingTakes max in each window; translation invariant
Avg poolingTakes mean; used in GAP layers
Sobel kernelDetects edges via gradient approximation
Padding “same”p = (k−1)/2 keeps output size = input size