A digital image is a function sampled on a grid: a matrix of numbers.
Grey levels, colour channels and bit depth.
Moving an image around: a geometric transform is a matrix acting on the coordinates, not on the pixel values.
Why we compute the output by going backwards (inverse mapping), and what interpolation is for.
Adding translation: affine transforms and homogeneous coordinates.
Slides titled The math give the formulas and proofs; the ideas are complete without them.
From a scene to a matrix
A camera or microscope measures a continuous brightness and records it at a grid of points. The result is a matrix: the entry in row i, column j, written I[i, j], is the brightness at that grid point.
Each entry is a pixel (picture element). Its value is a grey level, typically stored as an integer 0–255 (8 bits) or 0–65535 (16 bits, common in microscopy).
A colour image is three such matrices (red, green, blue): an M \times N \times 3 array.
Watch the conventions: matrices are indexed (row, column) from the top left; plots use (x, y) with y pointing up.
Image operations come in two kinds
Change the values
Change the positions
each new value is computed from the old value, or from the neighbours
each new pixel takes the value from the place it came from
contrast, thresholds, filters
rotate, scale, shear, register two images
next lectures
this lecture
in symbols: I'[i,j] = g(I[i,j])
in symbols: I'(\mathbf{x}) = I(T^{-1}\mathbf{x})
A geometric transform keeps every grey level; it only moves them.
Try it: transform an image
Read a matrix from its columns
Put the origin in the centre of the image. A 2\times2 matrix moves every point, and the picture travels along.
Column 1 is where the step to the right, (1, 0), lands. Column 2 is where the step up, (0, 1), lands. Every other point follows: (x, y) goes to x times column 1 plus y times column 2. That is all you need to read a matrix.
Example: \begin{bmatrix} 1 & 2\\ 0 & 1\end{bmatrix} keeps (1, 0) and sends (0, 1) to (2, 1): every point slides sideways by twice its height. A shear.
The determinant is the factor by which areas change: 2 doubles every area; a negative determinant means a mirror image; 0 means the columns are parallel and the image is squashed onto a line.
The math: 2 × 2 matrices as maps of the plane
A sends the point \mathbf{x} = (x, y) to A\mathbf{x}:
A = \begin{bmatrix} a & b\\ c & d \end{bmatrix}, \qquad A\begin{bmatrix}1\\0\end{bmatrix} = \begin{bmatrix}a\\c\end{bmatrix} \text{ (col 1)}, \quad A\begin{bmatrix}0\\1\end{bmatrix} = \begin{bmatrix}b\\d\end{bmatrix} \text{ (col 2)}, \quad A\begin{bmatrix}x\\y\end{bmatrix} = x\begin{bmatrix}a\\c\end{bmatrix} + y\begin{bmatrix}b\\d\end{bmatrix}.
The unit square goes to the parallelogram spanned by the two columns, with area |ad - bc| = |\det A|; by linearity every area is scaled by the same factor.
\det A < 0: the orientation is reversed (a mirror image); \det A = 0: the columns are linearly dependent and the image collapses onto a line.
Computing the output: go backwards
Sending every input pixel to its new position (forward mapping) leaves holes when the image is enlarged: scale by 3, and only 1 in 9 output pixels receives a value. When it shrinks, several pixels collide.
Instead, loop over the output pixels and ask where each one came from: undo the transform, and look up the input there.
Every output pixel gets exactly one value: no holes.
The place it came from is almost never exactly on a pixel, so we interpolate:
nearest neighbour: take the closest pixel (blocky, but keeps the original values; used for label images),
bilinear: a weighted average of the 4 surrounding pixels, closer pixels weighing more (smoother; the standard choice). Halfway between a 10 and a 20 gives 15.
This needs a way back: a transform that squashes the image onto a line (determinant 0) cannot be undone.
Backward mapping as pseudocode
compute A⁻¹ once # needs det A ≠ 0
for every output pixel (x', y'):
(x, y) = A⁻¹ · (x', y') # where did it come from?
if (x, y) lies outside the input image:
out(x', y') = 0 # or another border rule
else:
out(x', y') = interpolate(I, x, y) # nearest or bilinear
The loop runs over the output: every output pixel is visited once and gets one value, so there are no holes.
interpolate reads the input between pixels: nearest neighbour, or a weighted average of 4 pixels (bilinear, next slide).
With an affine transform, (x, y) = A^{-1}\big((x', y') - \mathbf t\big): the same loop.
The math: inverse mapping and bilinear interpolation
Inverse mapping. For every output position \mathbf{x}',
Bilinear interpolation. Let A^{-1}\mathbf{x}' lie in the pixel square with corners (0,0), (1,0), (0,1), (1,1) (after shifting), at (\alpha, \beta) with 0 \le \alpha, \beta \le 1:
The four weights are non-negative and add up to 1: a weighted average.
Each weight is the area of the rectangle opposite its corner (figure).
It is linear interpolation along x on both rows, then along y (or the other way round: same result).
Adding translation: affine transforms
A 2\times2 matrix always keeps the origin (the image centre) fixed. To also shift the image, add a translation after the matrix: an affine transform.
Rotate about another point\mathbf{p} (say, a corner): shift \mathbf{p} to the origin, rotate, shift back. Example: a quarter turn about \mathbf{p} = (2, 1) sends (3, 1) to (1, 0), then to (0, 1), then to (2, 2).
Order matters. Stretch x by 2, then rotate a quarter turn: the image ends up tall. Rotate first, then stretch x: it ends up wide.
Image libraries describe such a transform by a small table of 6 numbers: the matrix and the shift (next slide).
The math: homogeneous coordinates
\mathbf{x}' = A\mathbf{x} + \mathbf{t} is not linear, but it becomes a single 3\times3 matrix when every point gets an extra coordinate 1 (homogeneous coordinates):
\begin{bmatrix}x'\\ y'\\ 1\end{bmatrix} = \begin{bmatrix} a & b & t_x\\ c & d & t_y\\ 0 & 0 & 1\end{bmatrix}\begin{bmatrix}x\\ y\\ 1\end{bmatrix}.
Now composition is matrix multiplication again: “rotate about the point \mathbf{p}” = T_{\mathbf{p}}\,R\,T_{-\mathbf{p}}, with T_{\mathbf{p}} the translation by \mathbf{p}.
Order matters: RS \neq SR in general.
Image libraries describe a transform by exactly this 3\times3 (or 2\times3) matrix.
Summary
An image is a sampled function, stored as a matrix I[i, j] (row, column).
A geometric transform moves positions, not values. The columns of the matrix tell you what it does; the determinant tells you about area and mirroring.
Compute it backwards: for every output pixel, find where it came from, and interpolate between pixels.
A shift on top of the matrix gives an affine transform; order matters.
Next: change the values using the neighbours: filtering.