A projective transformation (also called a homography) maps points from one plane to another. Unlike affine transformations, projective transformations do not necessarily preserve parallelism — parallel lines in the input space can converge in the output space.
Perspective projection is a particular type of projection where all the rays of the projection pass through a single point. This puts constraints on the form of the matrix P.
A perspective projection has the form:
x1x2x3=PXYZS
where: (x1,x2,x3)⊤ are the homogeneous coordinates of a point in the image plane P is a 3x4 matrix (X,Y,Z,S)⊤ are the homogeneous coordinates of a point in the world
Four pairs of points p^=(p1,p2),q^=(q1,q2) (four points from each image which are paired together) give eight linear equations and then A,B,...,H can be solved. One condition is that no three of the four points can be collinear (i.e. lie on the same line).
Quad-To-Quad Projection
The above projection algorithm can be used to perform “quad-to-quad” projection between two 2D spaces (or two 2D coordinate systems). A quadrilateral (four sided polygon) is defined both in the input space and the output space. It is guaranteed that there is exactly one transformation that will map points from the input space to the output space as defined by the quadrilaterals.
A quad-to-quad transformation of an image, going from a rectangle to a complex non-rectangular quadrilateral with no parallel edges.
Quadrilateral restrictions: No three of the four points in the quadrilateral can be collinear (i.e. lie on the same line). That is the same as saying that no two adjacent edges of the quadrilateral may lie on the same line (which would degenerate the quadrilateral into a triangle).
The order in which you define the four vertices is not important, as long as the pairing between the two quadrilaterals is consistent — the nth vertex of the input quad must correspond to the nth vertex of the output quad. A different correspondence gives a different transformation.
We want to find the matrix T that projects an input point p^ to an output point q^, such that:
q^=Tp^
We can find the individual numbers A,B,...,H that make up T by forming some linear equations. For each point p^=(px,py) that maps to point q^=(qx,qy) we can form two linear equations:
The four corners of the input quad map to the four corners of the output quad, so this gives us 8 equations with 8 unknowns. To solve these linear equations, we can use matrices. If we pull all of the coefficients of A,B,...,H into a matrix M (not called A, to avoid confusion with the coefficient A), we can write the equation in the form Mx^=b^:
This can then be re-arranged to solve for x^ (which is our vector of coefficients A,B,...H which we will eventually put back into the transformation matrix T).
x^=M−1b^
Once x^ has been found, T can be made from the values in x^. You can then convert points from your input coordinate space to the output coordinate space using:
Qt provides a QTransform::quadToQuad() method which can be used to create a transformation object that can then be applied to things such as images. Note, however, that when transforming an image, “extra” translation is removed from the output so that the translated image is contained within the smallest number of pixels possible. Qt also specifies the structure of the transformation matrix slightly differently, with the order of each element being different from “standard” (it looks like it has been mirrored around the leading diagonal).
The structure of a Qt transformation matrix. Notice how the ordering is different to ‘standard’ (it looks like the matrix has been mirrored around the leading diagonal).
Python’s image library (PIL) provides a transform() function which can do perspective transformations, along with interpolation (this is what is used to transform the above “Hello” image).