I am trying to implement a perspective projection. For example, I have a square with a given size, and known corners’ coordinates (A1 .. A4) . I have an image where this square is displayed at some specific position. I know its position and the coordinates of the corners of the displayed square B1 .. B4. I want to find a transformation matrix M that will
translate B -> A.
I followed method Mathematical Illustrations, chapter 10, and I managed to transform 3 corners well. But I cannot find the matrix to transform correctly 4 corners… I find the matrix as on page 7, but it does not translate all 4 points well.
~
As your reference indicates, the standard way to represent a general projective transformation is to augment your
(x,y)2-D coordinates with awcoordinate (to get homogeneous coordinates[x,y,w]), and use a 3×3 matrix to transform them.It is not clear from your question exactly how you are currently trying to use your points, but I suspect that there is some confusion with how to use the extra coordinate. Mathematically, the thing to remember is that you can multiply the entire homogeneous vector by an arbitrary nonzero scaling factor (because in the end dividing by the third coordinate will cancel out the scaling factor). However, it is sometimes hard to figure out what this means in a practical sense…
For this problem, set up the system as follows (I am treating homogeneous coordinates as row vectors here, to match your reference):
The tricky bit: you can’t just set
wandw'to 1. This is because a projective transformationTdoesn’t necessarily leave the third coordinate unchanged! (If it did, you could skip the homogeneous bit altogether, and get an affine transform using only three point pairs…).One way to express this (in a way that can be solved readily) is to add a parameter,
K = w' / w. Then you can express the above as a linear system, as follows:Remember, all the
aandbvalues are fixed constants; you’re trying to solve for the nine elements of yourTmatrix. Each point pair adds three constraints (from the vector equality above) and one additional parameter (the K for that equation), for a total of 4*3=12 constraints, and 9+4=13 parameters. So, we’re missing one constraint here…An additional constraint is needed because the matrix
Teffectively has an arbitrary scaling factor: it is mapping between homogeneous coordinates (which all divide out an arbitrary scaling factor anyway), so there is no inherent way to decide what this scaling factor is. One way to fix this factor is to arbitrarily setT_ww = 1:This gives you a fully-determined linear system:
You now have 12 linear equations in 12 variables (matrix elements
T_**and additional scaling parametersK*). Use a linear algebra library to solve the system and get your results (assuming you don’t feel like writing your own solver…).