Exercise: images as arrays & geometric transforms

Goal

By the end of this exercise you should be able to:

  • read what a 2\times2 matrix does to an image from its columns and determinant,
  • compute a geometric transform backwards, with nearest-neighbour and bilinear interpolation,
  • rotate about a point other than the origin, and explain why the order of transforms matters,
  • (math) build an affine transform from homogeneous 3\times3 matrices.

Do the pen-and-paper part first, then the interactive part. Questions marked (math) ask for derivations; do the others first.

By hand

B1. Sketch what the shear H = \begin{bmatrix} 1 & 2\\ 0 & 1\end{bmatrix} does to the unit square with corners (0,0), (1,0), (1,1), (0,1). What is \det H, and what does that say about the area?

B2. For each matrix, say whether the result is a mirror image, and give the factor by which areas change: (a) \begin{bmatrix} 2 & 0\\ 0 & 0.5\end{bmatrix} (b) \begin{bmatrix} -1 & 0\\ 0 & 1\end{bmatrix} (c) \begin{bmatrix} -1 & 0\\ 0 & -1\end{bmatrix} (d) \begin{bmatrix} 1 & 1\\ 0.5 & 0.5\end{bmatrix}

B3. The image is scaled by A = \begin{bmatrix} 2 & 0\\ 0 & 2\end{bmatrix} about the origin. Which input position does the output pixel at \mathbf{x}' = (3, 5) come from?

B4. Bilinear interpolation. Four neighbouring pixels have the values I(0,0) = 10, I(1,0) = 20, I(0,1) = 30, I(1,1) = 60 (written as I(x, y)). Estimate I(0.25, 0.5): first interpolate along x on the rows y = 0 and y = 1, then along y. What does nearest-neighbour give?

B5. Rotate by 90° counter-clockwise about the point \mathbf{p} = (2, 1), without matrices: shift \mathbf{p} to the origin, turn a quarter turn, shift back. Where do the points (3, 1) and (2, 3) go? Check your answers with a sketch.

B6. Let S stretch the image by 2 along x (keep y), and R rotate it by 90° counter-clockwise about the centre. Where does the point (1, 0) go if you apply S first and then R? And if you apply R first and then S? A square image: is it tall or wide afterwards, in each case?

B1. H keeps (1, 0) and sends (0, 1) to (2, 1): horizontal lines stay horizontal and every point slides sideways by twice its height. The square becomes a parallelogram with corners (0,0), (1,0), (3,1), (2,1). \det H = 1: the area is unchanged.

B2. (a) \det = 1: twice as wide, half as tall, area unchanged, not mirrored. (b) \det = -1: mirrored left–right, area unchanged. (c) \det = 1: a rotation by 180°, not a mirror image. (d) \det = 0.5 - 0.5 = 0: the columns are parallel, the whole image is squashed onto a line; area 0, not invertible.

B3. A^{-1} = \tfrac12 I, so \mathbf{x} = A^{-1}\mathbf{x}' = (1.5, 2.5): between pixels, so we must interpolate.

B4. Along x at y = 0: 10 + 0.25\,(20 - 10) = 12.5. At y = 1: 30 + 0.25\,(60 - 30) = 37.5. Along y: 12.5 + 0.5\,(37.5 - 12.5) = 25. Nearest neighbour: (0.25, 0.5) is equally far from (0, 0) and (0, 1), so it depends on the tie-breaking rule: 10 or 30. (Rounding 0.5 up gives (0, 1), value 30.)

B5. (3, 1): shifted, it is (1, 0), one step to the right of \mathbf{p}; a quarter turn takes it to (0, 1); shifting back gives (2, 2), one step above \mathbf{p}. (2, 3): shifted, (0, 2); turned, (-2, 0); shifted back, (0, 1), two steps to the left of \mathbf{p}. The point \mathbf{p} itself stays where it is.

B6. S then R: (1, 0) \to (2, 0) \to (0, 2). R then S: (1, 0) \to (0, 1) \to (0, 1): the stretch along x no longer touches it, because it now points up. The square stretched first becomes wide, then the quarter turn stands it up: tall (twice as tall as wide). Turned first, it is still a square, then stretched along x: wide. The two orders give different images, so the order of transforms matters.

Math questions

B7. (math) Write the 3\times3 homogeneous matrix that rotates by 90° counter-clockwise about the point \mathbf{p} = (2, 1), as a product T_{\mathbf{p}}\,R\,T_{-\mathbf{p}}. Check it: where does \mathbf{p} go? Where does (3, 1) go? Compare with B5.

B8. (math) Show that in bilinear interpolation it does not matter whether you interpolate along x first (as in B4) or along y first: both give (1-\alpha)(1-\beta)\,I_{00} + \alpha(1-\beta)\,I_{10} + (1-\alpha)\beta\,I_{01} + \alpha\beta\,I_{11} at the point (\alpha, \beta). Check it on the numbers of B4, interpolating along y first.

B7. T_{\mathbf{p}}RT_{-\mathbf{p}} = \begin{bmatrix}1&0&2\\0&1&1\\0&0&1\end{bmatrix} \begin{bmatrix}0&-1&0\\1&0&0\\0&0&1\end{bmatrix} \begin{bmatrix}1&0&-2\\0&1&-1\\0&0&1\end{bmatrix} = \begin{bmatrix}0&-1&3\\1&0&-1\\0&0&1\end{bmatrix}. Check: (2, 1) \mapsto (0\cdot2 - 1 + 3,\; 2 + 0 - 1) = (2, 1), so \mathbf{p} stays fixed. (3, 1) \mapsto (-1 + 3,\; 3 - 1) = (2, 2): the point one step to the right of \mathbf{p} ends up one step above it, a quarter turn counter-clockwise, as in B5.

B8. Along x first: the two rows give (1-\alpha)I_{00} + \alpha I_{10} and (1-\alpha)I_{01} + \alpha I_{11}; interpolating these along y with weights 1-\beta and \beta gives the four terms above. Along y first: the two columns give (1-\beta)I_{00} + \beta I_{01} and (1-\beta)I_{10} + \beta I_{11}; interpolating along x with weights 1-\alpha and \alpha gives the same four products, in a different order. With the numbers of B4 (\alpha = 0.25, \beta = 0.5): along y at x = 0: 10 + 0.5\,(30 - 10) = 20; at x = 1: 20 + 0.5\,(60 - 20) = 40; then along x: 20 + 0.25\,(40 - 20) = 25, the same as before.

Explore

The same ideas on a picture: a drawn cell (with an “F” in the corner, so mirror images are easy to spot), centred on the origin. Drag the red and blue handles (the columns of A) or use the presets and the rotation slider.

E1. Drag the blue handle to make the shear H from B1. Does the frame (solid outline) look like your sketch?

E2. Before clicking, predict each preset from its columns: Rotate 30°, Scale, Reflect, Misaligned. Which one gives a mirror image? Then set the rotation slider to 180°: is that a mirror image too?

E3. Click Collapse, then slowly drag the blue handle away from the red one. When exactly does the cell become a line?

E4. Click Scale and look closely at the edges of the cell and the F. The demo reads the input with bilinear interpolation. What does that do to the sharp edges, and what would nearest-neighbour lookup show instead? Which of the two would you use for a label image?

E1. The red handle stays at (1, 0); put the blue one at (2, 1). Horizontal lines stay horizontal and every point slides sideways by 2 times its height: the x-axis stays put, the top edge of the frame (height 1.5) slides 3 units to the right and the bottom edge 3 units to the left, so the corners leave the view. The lean is the same as in the B1 sketch, where the top of the unit square (height 1) slides 2 units.

E2. Rotate 30°: columns (0.87, 0.5) and (-0.5, 0.87), the picture turns counter-clockwise. Scale: columns (2, 0) and (0, 0.5), twice as wide, half as tall. Reflect: column 1 is (-1, 0), so the picture is mirrored left–right and the F reads backwards. Misaligned: tilted and skewed, as two images of the same sample often are before registration. Rotating by 180° turns the F upside down, but it is not a mirror image: turn your head (or the screen) and it reads normally again. Only the reflection flips it (\det = -1).

E3. Exactly when the two columns lie on one line through the origin, i.e. when \det A = 0. Then every point x\cdot\text{col 1} + y\cdot\text{col 2} is on that line, so the whole picture is squashed onto it. As soon as the columns point in different directions, the picture is a (thin) parallelogram again.

E4. In the horizontal direction each input pixel now covers two output pixels. With bilinear interpolation the edges become soft ramps: output pixels between a dark and a bright input pixel get in-between grey levels that were not in the input. Nearest-neighbour lookup would instead copy the closest input pixel, so edges would look blocky and staircase-like but every output value would be one of the input values. For a label image (every value is an object number) only nearest neighbour is correct: an averaged label is the number of a different object, or of no object at all.

Code

Write the algorithms of this unit yourself, in Rust. Setup, and how to run and check your code: coding exercises: getting started. Your code goes in images/src/images/exercise.rs.

Positions in the code are (x, y) = (column, row), counted from 0, and y points down: a rotation by a positive angle turns the picture clockwise on the screen.

C1. nearest(img, x, y) and bilinear(img, x, y): read the image at a position between pixels (0 outside the image). Bilinear interpolation, as in B4:

x0 = floor(x),  y0 = floor(y)            # the pixel square the position lies in
a  = x − x0,    b  = y − y0              # where inside it, both between 0 and 1
return (1−a)(1−b)·I(x0, y0)   + a(1−b)·I(x0+1, y0)
     + (1−a)b·I(x0, y0+1)     + ab·I(x0+1, y0+1)

The check uses the four pixels of B4.

C2. Homogeneous 3\times3 matrices: mat3_mul, apply (a matrix times (x, y, 1)), translation, rotation and rotate_about, which is “shift \mathbf p to the origin, rotate, shift back”. The checks are B5, B6 and B7.

C3. invert_affine: the transform that undoes an affine map, or none when \det = 0.

C4. warp(img, m, interp): transform an image backwards, with nearest or bilinear:

back = invert_affine(m)
for every output pixel (x, y):
    (sx, sy) = back · (x, y, 1)          # where did this pixel come from?
    out(x, y) = interp(I, sx, sy)

Then cargo run -p images -- images and open the pictures in out/images/.

C5. Compare zoom-nearest.png with zoom-bilinear.png, and rotate30-nearest.png with rotate30-bilinear.png. Then change main.rs to rotate the micrograph by 30° twelve times in a row (a full turn), once with each kind of lookup. What do you expect, and what do you get?

The model solution is images/src/images/solution.rs in the download.

C5. Enlarged, nearest neighbour gives 8 \times 8 blocks and bilinear gives smooth ramps, as on the slide “Computing the output: go backwards”. Rotated by 30°, nearest neighbour gives jagged staircase edges, while bilinear is smooth and slightly blurred.

After twelve rotations the image is upright again, but:

  • The corners are gone. Each rotation turns the corners out of the square frame; they read 0 from outside the image, and the black corners grow with every step. What remains is a disc.
  • Bilinear blurs a little more each time. Every lookup between pixels is a weighted average, a small blur, and twelve small blurs add up to a visible one.
  • Nearest neighbour stays sharp, but rearranges pixels. At every step some pixels are duplicated and others dropped, so fine detail gets jagged and scrambled.

Better: compose the twelve rotations into one matrix, and interpolate once.

Where this leads

Finding the matrix that aligns two images (registration) is an optimisation problem over the entries of A and \mathbf{t}; this unit only applies a given transform. Next: changing pixel values using their neighbours (filtering).

Later: smoother interpolation than bilinear (splines on images); finding a shift or an affine map between two images (FFT registration, alignment from keypoints); homogeneous coordinates for 3D scenes and cameras (3D graphics, cameras).