Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV Homography, Transform a point, what is this code doing?

I'm working with a homography calculated by OpenCV. I currently use this homography to transform points using the function below. This function performs the task I require however I have no clue how it actually works.

Can anyone explain, line by line exactly, the logic/theory behind the last 3 lines of code, I understand that this transforms the point x,y but I'm unclear as to why this works:

Why are Z, px and py calculated in this way, what do the elements in h correspond to?

Your comments are greatly appreciated :)

double h[9];
homography = cvMat(3, 3, CV_64F, h);
CvMat ps1 = cvMat(MAX_CALIB_POINTS/2,2,CV_32FC1, points1);
CvMat ps2 = cvMat(MAX_CALIB_POINTS/2,2,CV_32FC1, points2);

cvFindHomography(&ps1, &ps2, &homography, 0);

...

// This is the part I don't fully understand
double x = 10.0;
double y = 10.0;
double Z = 1./(h[6]*x + h[7]*y + h[8]);
px = (int)((h[0]*x + h[1]*y + h[2])*Z);
py = (int)((h[3]*x + h[4]*y + h[5])*Z);
like image 680
Jayson Avatar asked Feb 14 '12 10:02

Jayson


People also ask

How do you use homography matrix to points?

This spatial relationship is represented by a transformation known as a homography, H, where H is a 3 x 3 matrix. To apply homography H to a point p, simply compute p' = Hp, where p and p' are (3-dimensional) homogeneous coordinates. p' is then the transformed point.

What is homography in OpenCV?

Homography is a transformation that maps the points in one point to the corresponding point in another image. The homography is a 3×3 matrix : If 2 points are not in the same plane then we have to use 2 homographs. Similarly, for n planes, we have to use n homographs.

What is homography in image processing?

Homography, also referred to as planar homography, is a transformation that is occurring between two planes. In other words, it is a mapping between two planar projections of an image. It is represented by a 3x3 transformation matrix in a homogenous coordinates space.

How does cv2 perspective transform work?

The PerspectiveTransform() function takes the coordinate points on the source image which is to be transformed as required and the coordinate points on the destination image that corresponds to the points on the source image as the input parameters.


1 Answers

cvFindHomography() returns a matrix using homogenous coordinates:

Homogeneous coordinates are ubiquitous in computer graphics because they allow common operations such as translation, rotation, scaling and perspective projection to be implemented as matrix operations

What's happening in the code: The cartesian point p_origin_cartesian(x,y) is transformed to homogenous coordinates, then the 3x3 perspective transformation matrix h is applied and the result is converted back to cartesian coordinates p_transformed_cartesian(px,py).

UPDATE

In detail:

Convert p_origin_cartesian to p_origin_homogenous:

(x,y)  =>  (x,y,1)

Do perspective transformation:

p_transformed_homogenous = h * p_origin_homogenous =

(h0,h1,h2)    (x)   (h0*x + h1*y + h2)   (tx)   
(h3,h4,h5)  * (y) = (h3*x + h4*y + h5) = (ty) 
(h6,h7,h8)    (1)   (h6*x + h7*y + h8)   (tz)

Convert p_transformed_homogenous to p_transformed_cartesian:

(tx,ty,tz)  =>  (tx/tz, ty/tz) 

Your code translated:

px = tx/tz;
py = ty/tz;
Z  = 1/tz;
like image 130
Ben Avatar answered Oct 23 '22 01:10

Ben