# Math Help - projection reansformation / homography help

1. ## projection reansformation / homography help

Hey guys

I am having some trouble with a project i am working on and the maths is killing me

basically i have a projector which is pointed at different angles to a screen and i need to correct the skew - keystone effects of what i project via software

i can get the corner coordinates of the polygon that appears on the screen and i can get x.y.z roll pitch, yaw of the projector

i have looked and projection matrices look like the way but i have no idea what so ever on how to make them work

if anyone has any ideas i would be much much appreciative

2. I'm assuming you are working with 2D coordinates using 3D inhomogeneous coordinates (so that the point (x,y) is represented (x,y,1))
There is exactly one projective transformation between two sets of 4 non-collinear points.
It is slightly laborious to calculate this transformation but not difficult: one way is to first
calculate the transformation that sends the four points (1,0,0) (0,1,0) (0,0,1) and (1,1,1) to the first set of points, and then the transformation that sends the same four points to the second set of points. Then the transformation you want is the inverse of the first multiplied by the second.

To send the four reference points to (x1,y1,z1),(x2,y2,z2),(x3,y3,z3),(x4,y4,z4)

write down the matrix M=x1 x2 x3
y1 y2 y3
z1 z2 z3

Calculate M^-1*(x4,y4,z4) (regarding this point as a column vector)
Suppose this gives you the point (a,b,c)
write down the matrix N = a/c 0 0
0 b/c 0
0 0 1
The matrix of the transformation is M*N
Since the polygon you are calculating probably has four vertices you can easily calculate the transformation that the projector has made to your image. If you apply the inverse transformation to your image coordinates first then this will undo the distortion. However you might need to scale your image down first slightly.

3. Hi thanks for the reply it is really apreciated

I am really confused now though

if i have a square

x1,y1 x2,y2
---------------
| ................. |
| ................. |
| ................. |
| ................. |
----------------
x4,y4 x3,y3

u1,v1 u2,v,2
'''''_______|
/...........|
|............|
|............|
\_______|
u4,v4 u3,v3

is there anywhere to use these coords to be able to map the contents of the square into the quad so it still holds the same shape ?

again sorry if this is obvious or i have missed the point

4. I think you will be able to map the contents of the square into the quad provided the transformation that the projector has unintentionally applied is actually projective. This is how I would go about things in that case:

I presume you can calculate both the maximum sized square that the projector can display without clipping the image, and where this square currently appears. Although these images are in different planes we can ignore this and pretend that both planes are the same.
Code:
+D-------+C
|        |
|        |
|        |
+A-------+B

+D'-----------+C'
\           /
\         /
\       /
A'--+B'
ABCD is the original square A'B'C'D' is what actually appears - I've shown it as an isosceles trapezium but in the worst case scenario it could be any convex quadrilateral.
Draw the largest possible square (or just a large enough square you can calculate conveniently) inside A'B'C'D' and call it A''B''C''D''

Now decree that ABCD and A''B''C''D'' have the same coordinates. You can certainly do this because they are similar squares. Now calculate the coordinates of A'B'C'D' relative to this coordinate system. Now you decide that the projector ought to have displayed the square ABCD your image in A''B''C''D'' whereas it actually displays it A'B'C'D', so that it has apparently applied the unique projective transform that turns A''B''C''D'' into A'B'C'D'.

You can calculate this transformation using the method I suggested in my last post.
Suppose the coordinates of A''B''C''D'' are

A''=(0,0,1)
B''=(1,0,1)
C''=(1,1,1)
D''=(0,1,1)

(Whatever they are they should be the same as your coordinates for ABCD - If you were using ordinary 2D coordinates you just assign 1 as the third coordinate).

Maybe the coordinates for A'B'C'D' are something like

(-0.2,-0.3,1)
(1.3,-0.2,1)
(1.5,1.3,1)
(-0.5,1.4,1)

(where now I am pretending that the distortion also spoiled horizontal lines)
The Z coordinate should certainly be the same as it was for the square).

Now calculate the matrix for the following transformations
(1,0,0)->A''
(0,1,0)->B''
(0,0,1)->C''
(1,1,1)->D''

Call this M

Next calculate the matrix for

(1,0,0)->A'
(0,1,0)->B'
(0,0,1)->C'
(1,1,1)->D'

Call this N

The transformation the projector has notionally applied to our undistorted square is

N*M^-1 (i.e. first M^-1 then N)

So to undistort it we need to apply M*N^-1.

To apply this to a point (x,y) (which we assume will be (0,0) for A, (1,0) for B and so on)

Turn (x,y) into (x,y,1) Compute M*N^-1*(x,y,1) (treating this as a column vector).
Now you will have a coordinate X',Y',Z' where Z' is most likely close to 1 but not exactly 1. Now divide X',Y' by Z'. That is the adjusted coordinate you should use for computing your image.

5. Hi there

once again thanks so so much for replying,

i am so so close to getting my head around this now i can feel it

my problem is you say calcualte the matrix for

(1,0,0)->A''
(0,1,0)->B''
......

i am unsure what you mean by this

again sorry if this is simple stuff im new to matrix stuff

thanks again

6. Say A",B",C",D" are the values I said before. Now we put the first three values into a matrix
in columns
Code:
ax bx cx = 0 1 1
ay by cy   0 0 1
az bz cz   1 1 1

Invert this matrix:
-1 0  1
1 -1 0
0 1  0

Multiply D'' by this
-1 0 1  0 =  1
1 -1 0 *1    -1
0 1  0  1     1
Make this into a diagonal matrix but making the third value 1 and dividing the first two coordinates by the third and multiply on the left by the first matrix we wrote down:
Code:
0 1 1   1  0  0  0 -1  1
0 0 1 * 0 -1 0 = 0  0   1
1 1 1   0 0  1   1  -1  1
This transformation maps the projective reference points to A''B''C''D''
which we can check by writing all four vectors in one matrix and multiplying
Code:
0 -1  1     1 0 0 1    0 -1 1 0
0  0  1  *  0 1 0 1 =  0 0 1 1
1 -1  1     0 0 1 1    1 -1 1 1
Note that although we seemed to map 0 1 0 to -1 0 -1 this is the same
as 1 0 1 (we divide the first two coordinates by the third and replace the third by 1). If you ever find yourself dividing by 0 it means that the point is an ideal point and not visible in the projected image.

Now you do all the same things but with A' B' C' D'. Finally you have to invert one of the matrices and multiply them together to get the matrix that moves
A'B'C'D' to A''B''C''D'' or vice versa.
There are probably other ways to calculate these transformations but this one is fairly robust and quite easy to program.