A plane seen at an angle
All points of a plane in the scene are related to their pixels in a photo by one projective transformation, a homography:
is only defined up to scale, so it has 8 degrees of freedom. Lines stay lines, but parallel lines may meet and ratios change; the 2D transformations demo shows these properties one by one. Here maps the photo (, in pixels) to the front view (, in pixels of the output rectangle). The four corners of the object go to the four corners of the rectangle.
Two equations per point
Write the last entry as a ninth unknown and multiply out the division by :
and likewise for . Both equations are linear in the entries of . Stacking them for all correspondences gives a homogeneous system, set up the same way as the one for camera calibration:
Solving with the SVD
always solves the system, so we look for the unit vector that makes smallest: the right singular vector of the smallest singular value (see the SVD and total least squares). Four points give 8 equations for 9 unknowns, so is 8 × 9 and there is always a direction with exactly: the ninth column of . It fixes up to its scale, and dividing by the last entry gives the form . With more than four correspondences the system is overdetermined; the same vector is then the least-squares solution, and its singular value tells how well all points agree.
Not every set of four points works. If three of the corners lie on one line, no invertible projective map can send them to three corners of a rectangle. The best is then a singular , which squashes the plane.
Rectification by inverse mapping
To fill the front view, go backwards: for every output pixel , compute (again with the division by ), and interpolate the photo bilinearly at . Mapping the photo forward instead would leave holes where the front view is stretched. The four corners only fix where the object's corners go, not the object's true proportions; the width-to-height ratio of the rectangle has to come from knowing the object, e.g. √2 for an A4 sheet.
Try this
- At the start, the corners sit exactly on the sheet, and the front view shows the test image undistorted. The grid shows how the evenly spaced lines of the front view are compressed towards the far edge of the photo.
- Pick a point near the far edge of the sheet and one near the near edge, and compare in the panel: the division by is what makes equal steps in the front view unequal in the photo.
- Drag corner 3 by a few pixels: the text in the front view leans and bends into a trapezoid. Straight lines stay straight, but the result is no longer the true front view.
- Switch the ratio to 1 : 1 or 3 : 4: the text gets squashed. maps any four corners onto any rectangle, so the proportions have to be known from outside.
- Drag corner 4 onto the line through corners 1 and 2: drops towards 0, the front view goes blank and the panel warns that is singular.
- Swap the corners left and right (1 ↔ 2 and 4 ↔ 3): the front view is mirrored, and changes sign.
- Open a photo of your own, such as a document on a desk, a facade or a tiled floor, and drag the corners onto a rectangle in it.