Skip to content
ml-recall

Topic notes

Computer Vision Basics

Images as arrays and colour spaces, classic filtering (blur, Sobel and Laplacian edges, median), histogram equalization and other hand-crafted tools, and data augmentation for vision models.

5-min read AI-checked

Practise 35 questions Take a test

Key ideas

Formulas

G=Gx2+Gy2,θ=atan2⁡(Gy,Gx),Kx=[121][−101]G = \sqrt{G_x^2 + G_y^2},\qquad \theta = \operatorname{atan2}(G_y, G_x),\qquad K_x = \begin{bmatrix} 1 \\ 2 \\ 1 \end{bmatrix}\begin{bmatrix} -1 & 0 & 1 \end{bmatrix} h(v)=round⁡(cdf⁡(v)−cdf⁡min⁡N−cdf⁡min⁡ (L−1)),∑rows r0..r1, cols c0..c1=S[r1,c1]−S[r0−1,c1]−S[r1,c0−1]+S[r0−1,c0−1]h(v) = \operatorname{round}\left(\frac{\operatorname{cdf}(v) - \operatorname{cdf}_{\min}}{N - \operatorname{cdf}_{\min}}\,(L - 1)\right),\qquad \sum_{\text{rows } r_0..r_1,\ \text{cols } c_0..c_1} = S[r_1, c_1] - S[r_0 - 1, c_1] - S[r_1, c_0 - 1] + S[r_0 - 1, c_0 - 1]

In the equalization map, cdf⁡(v)\operatorname{cdf}(v) counts the pixels with level ≤v\le v, cdf⁡min⁡\operatorname{cdf}_{\min} is its smallest nonzero value, NN the number of pixels and LL the number of levels7. HSV from RGB in [0,1][0, 1]: V=max⁡V = \max, S=(max⁡−min⁡)/max⁡S = (\max - \min)/\max, hue in 60∘60^\circ sectors depending on which channel is largest5.

Worked example

Sobel at the centre of the patch [003003060306090]\begin{bmatrix} 0 & 0 & 30 \\ 0 & 30 & 60 \\ 30 & 60 & 90 \end{bmatrix} (cross-correlation, yy pointing down):

Median vs mean on the window [12111310012111412]\begin{bmatrix} 12 & 11 & 13 \\ 10 & 0 & 12 \\ 11 & 14 & 12 \end{bmatrix} with a "pepper" centre: the median is 12, the mean 95/9≈10.695/9 \approx 10.6.

In code

import numpy as np
img = np.array([[250, 100], [30, 5]], dtype=np.uint8)
print((img + 10).tolist())                         # [[4, 110], [40, 15]]  uint8 wraps around
print(np.clip(img.astype(np.int16) + 10, 0, 255).astype(np.uint8).tolist())  # [[255, 110], [40, 15]]
batch = np.zeros((16, 120, 160, 3), dtype=np.uint8)  # N x H x W x C, as images are loaded
print(batch.transpose(0, 3, 1, 2).shape)           # (16, 3, 120, 160)  N x C x H x W for PyTorch
green = np.array([0.0, 200.0, 0.0])
print(round(green @ np.array([0.299, 0.587, 0.114]), 1), round(green.mean(), 1))  # 117.4 66.7

Common pitfalls

References

  1. AI for Beginners — Lesson 6 Introduction to Computer Vision: introduction; OpenCV (Loading Images); Examples of using Computer Vision (optical flow)
  2. Dive into Deep Learning — Sec. 14.1 Image Augmentation (introduction); 14.1.1.1 Flipping and Cropping; 14.1.2 Training with Image Augmentation
  3. Wikipedia: Kernel (image processing) — Details (table of kernels); Convolution; Edge handling; Normalization
  4. Wikipedia: Sobel operator — Formulation; Extension to other dimensions
  5. Wikipedia: HSL and HSV — Formal derivation (Lightness); Use in image analysis; Color conversion formulae (From RGB)
  6. Wikipedia: Median filter — Worked one-dimensional example; Edge preservation properties
  7. Wikipedia: Histogram equalization — Overview; Examples (Small image)
  8. Wikipedia: Histogram of oriented gradients — Algorithm implementation (Gradient computation; Orientation binning; Object recognition)