Search code examples
matlabopencvhomographyperspectivecamera

Perspective Transform in Matlab


Lets say :

point1 = [1 2 3 4 5 ; 1 2 3 4 5 ];
point2 = [2 3 4 5 6 ; 2 3 4 5 6 ];

s = findHomography(points1,points2);

Coordinates of an object containing points1 are [0,0; 10,0; 0,10; 10,10]

How do I find calculate perspective transform on the object so that it transforms to my test coordinates. There are built in function in opencv that can do this , however , I really need a simple example to clear out my confusion . Thanks.


Solution

  • Perspective transform is not a linear transformation. So you can't have matrix M 2x2 such that w=M*v (point v=(x1,y1) from first plane and point w=(x2,y2) from second plane). But you can do the trick if you use "homogeneous coordinates". 2d point in homogeneous coordinates looks like (x,y,1). Or in more general case (x,y,z) is equivalent to (x/z,y/z,1). This notation makes sense if you think about how points from 3d scene are projected to 2d sensor of the camera. In homogeneous coordinates matrix M 3x3 actually exists and w=M*v. So when you are working with perspective transformation from 2d to 2d you should expect to have 3x3 matrices and 3xn points.

    Edit (answer to comment):

    xTag = M11*x1 + M12*y2 + M13

    yTag = M21*x1 + M22*y2 + M23

    zTag = M31*x1 + M32*y2 + M33 (M33 will always be equal to 1, since there only 8 degrees of freedom)

    x2 = xTag/zTag

    y2 = yTag/zTag