2D transformations

Translation, rotation, scaling, mirroring, shear and perspective all fit into one 3 × 3 matrix acting on homogeneous coordinates. Combine them, watch a grid and an image deform, and see which properties survive: straight lines, parallel lines, ratios and orientation.

Plane p′=Hpp' = H p click to pick a point; faint: before the transformation
Transformed image gray: no part of the original image

Parametric transformations

A transformation TT is a machine that changes coordinates: every point pp goes to p′=T(p)p' = T(p). It is global (or parametric) when the same rule applies to every point and a few parameters describe it: 2 for a translation, 1 for a rotation, 2 for a scaling. Aligning two images, for example from matched features, means finding these parameters. The plane here uses image coordinates, with xx to the right and yy down; the image covers [−1,1]2[-1, 1]^2 with its center at the origin. The faint grid and "F" are the original, the solid ones their image under the transformation. The two colored segments start out parallel; on the teal one, the dot is the image of its midpoint and the ring is the midpoint of its image.

Linear transformations

Scaling, rotation, mirroring and shear are linear: a 2 × 2 matrix times the point.

[sx00sy]⏟scaling[cos⁡θ−sin⁡θsin⁡θcos⁡θ]⏟rotation[−1001]⏟mirror at the y-axis[1axay1]⏟shear\underbrace{\begin{bmatrix} s_x & 0 \\ 0 & s_y \end{bmatrix}}_{\text{scaling}} \quad \underbrace{\begin{bmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{bmatrix}}_{\text{rotation}} \quad \underbrace{\begin{bmatrix} -1 & 0 \\ 0 & 1 \end{bmatrix}}_{\text{mirror at the } y\text{-axis}} \quad \underbrace{\begin{bmatrix} 1 & a_x \\ a_y & 1 \end{bmatrix}}_{\text{shear}}

Because yy points down, a positive angle θ\theta turns clockwise on the screen. Mirroring over the origin, x′=−xx' = -x, y′=−yy' = -y, is the same as a rotation by 180°. Every combination of these maps is linear again, and linear maps share a set of properties:

The determinant tells how areas change: a rotation has det⁡=1\det = 1, a scaling sxsys_x s_y, a shear 1−axay1 - a_x a_y. A negative determinant means the plane is flipped over, as by the mirror at the yy-axis: the "F" then reads backwards.

Translation and homogeneous coordinates

A translation x′=x+txx' = x + t_x, y′=y+tyy' = y + t_y is not linear: it moves the origin, and no 2 × 2 matrix can do that. Homogeneous coordinates solve this with a third coordinate 1, which turns the translation into a matrix product as well:

[x′y′1]=[10tx01ty001][xy1]\begin{bmatrix} x' \\ y' \\ 1 \end{bmatrix} = \begin{bmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} x \\ y \\ 1 \end{bmatrix}

All other maps get the 2 × 2 matrix in the top left corner of a 3 × 3 matrix, and a sequence of transformations becomes a single product. The order matters: in H=T R Sh S Mi PH = T\,R\,\mathit{Sh}\,S\,\mathit{Mi}\,P the rightmost factor acts first. Here the point is mirrored and scaled first, then sheared, rotated about the origin and finally translated.

Affine transformations

Linear maps followed by a translation are affine, with six free entries:

[x′y′1]=[abcdef001][xy1]\begin{bmatrix} x' \\ y' \\ 1 \end{bmatrix} = \begin{bmatrix} a & b & c \\ d & e & f \\ 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} x \\ y \\ 1 \end{bmatrix}

They keep all the properties of linear maps except that the origin may move: lines stay lines, parallel lines stay parallel and ratios along a line are preserved.

Projective transformations

A projective transformation, or homography, combines an affine map with a perspective part that fills the last row:

Maffine⋅Mperspective=[abcdef001][100010gh1],[x′y′w′]=[a′b′c′d′e′f′gh1][xy1]M_\text{affine} \cdot M_\text{perspective} = \begin{bmatrix} a & b & c \\ d & e & f \\ 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ g & h & 1 \end{bmatrix}, \qquad \begin{bmatrix} x' \\ y' \\ w' \end{bmatrix} = \begin{bmatrix} a' & b' & c' \\ d' & e' & f' \\ g & h & 1 \end{bmatrix} \begin{bmatrix} x \\ y \\ 1 \end{bmatrix}

Now w′=gx+hy+1w' = g x + h y + 1 is no longer 1, and the point is (x′/w′,y′/w′)(x'/w', y'/w'). Points with a larger w′w' are pulled towards the origin, so the image looks like a plane seen at an angle. Multiplying the whole matrix by a factor k≠0k \ne 0 multiplies x′x', y′y' and w′w' alike and gives the same points, so HH is only defined up to scale: 9 entries, but 8 degrees of freedom.

A photo of a flat object, such as a document or a facade, is related to its front view by such a transformation. Knowing HH, the photo can be unwarped (rectified) into the front view.

Try this