Geometric transforms and colour spaces
Rotation and translation as 2×3 matrices, and why HSV or LAB separate colour from brightness better than BGR.
- Write rotation and translation as 2×3 affine matrices and apply them with warpAffine
- Explain why warping maps output pixels back to the input
- Choose a colour space in which a colour can be thresholded under changing light
Moving pixels around is linear algebra in homogeneous coordinates. A pixel \((x, y)\) becomes \((x, y, 1)\), and any rotation, scaling, shear or translation is a 2 × 3 matrix:
\[\begin{pmatrix} x' \\ y' \end{pmatrix} = \begin{pmatrix} a & b & t_x \\ c & d & t_y \end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix}\]05_image_transform.py builds both kinds:
rotate_matrix = cv2.getRotationMatrix2D(center=(width/2, height/2), angle=45, scale=1)
rotated_image = cv2.warpAffine(src=image, M=rotate_matrix, dsize=(width, height))
tx, ty = width / 4, height / 4
translate_matrix = np.array([[1, 0, tx],
[0, 1, ty]], dtype=np.float32)
translated_image = cv2.warpAffine(src=image, M=translate_matrix, dsize=(width, height))
getRotationMatrix2D returns
with \(\alpha = s\cos\theta\), \(\beta = s\sin\theta\): a rotation about the origin, wrapped in a translation so that the centre \((c_x, c_y)\) stays put. A positive angle turns the image counter-clockwise on screen, because the \(y\)-axis of an image points down.
Warping runs backwards. For every output pixel, warpAffine inverts the matrix, finds where it came from in the input, and interpolates there. Going forwards would leave holes wherever two input pixels land on one output pixel and none land on its neighbour. Corners that come from outside the input are filled with black — the triangles around the rotated image.
The 2 × 3 matrix is the affine special case of the 3 × 3 homography of lesson 7, which can also make parallel lines converge.
Colour spaces
07_color_spaces.py loads the same Rubik’s cube photographed in bright and dim light and converts both to LAB, YCrCb and HSV. The point: in BGR, all three channels change when the light changes, so “red” has no fixed range. The other spaces split the signal into brightness and colour:
| Space | Brightness | Colour |
|---|---|---|
| HSV | V (value) | H (hue, an angle; OpenCV stores 0–179) and S (saturation) |
| LAB | L (lightness) | a (green–red), b (blue–yellow) |
| YCrCb | Y (luma) | Cr, Cb (red and blue difference) |
To segment a colour under varying light, threshold the colour channels and give the brightness channel a wide range — for example cv2.inRange(hsv, (0, 120, 40), (10, 255, 255)) for strong reds. Red sits at both ends of the hue circle in HSV, so a full red mask needs a second range near 170–179.
Drawing on images
06_image_annotation.py draws lines, circles, rectangles, ellipses and text. Every call writes into the array in place, which is why the script draws on a copy() each time. Points are (x, y) tuples of integers; colours are BGR tuples; a thickness of -1 fills the shape. 12_callback_fun_annotate.py hooks the same drawing onto mouse events with cv2.setMouseCallback, the basis of every quick labelling tool.
Try it yourself. Print cv2.getRotationMatrix2D((200, 100), 90, 1) and check by hand that it sends \((200, 100)\) to itself and \((300, 100)\) to \((200, 0)\). Then rotate an image by 45° with dsize enlarged so that no corner is cut off — the new width is \(\vert w\cos\theta\vert + \vert h\sin\theta\vert\), and the translation column needs the matching shift.