# How does the perspectiveTransform( ) function work?

In the tutorial -> http://docs.opencv.org/doc/tutorials/...
I understand that the syntax for this function is as follows: perspectiveTransform(obj_corners, scene_corners, H);
Where obj_corners and scene_corners are vectors of Points and obj_corners contains 4 points of the object to be found and H is the homography matrix. My question is, what exactly does this function do?

edit retag close merge delete

Sort by ยป oldest newest most voted

Hi,

You should first read about the homography concept, for example : https://en.wikipedia.org/wiki/Homogra... or http://people.scs.carleton.ca/~c_shu/...

Basically, the matrix H contains the geometric transformation to pass from a view to another view assuming a planar surface in space.

In the tutorial, after the matching of the keypoints, you estimate the homography H using a RANSAC method (a robust method to estimate the homography without (trying to not) taking care of the possible outliers) with :

> Mat H = findHomography( obj, scene, CV_RANSAC );


So :

perspectiveTransform( obj_corners, scene_corners, H);


will just calculate the coordinates of the box corners in the current view / scene to draw the bounding box.

Edit: The function calculates for each corners (if I am not wrong) :

w = H_31 * obj_corners[i].x + H_32 * obj_corners[i].y + H_33 * 1
scene_corners[i].x = (H_11 * obj_corners[i].x + H_12 * obj_corners[i].y + H_13 * 1) / w
scene_corners[i].y = (H_21 * obj_corners[i].x + H_22 * obj_corners[i].y + H_23 * 1) / w

more

Great, That simplifies a lot of things. When you say that the homography is 'estimated', does that mean that it is not always accurate? Similarly, I have seen that a function warpPerspective( ) also uses the H. Would you happen to know how that works? ---> http://ramsrigoutham.com/2012/11/22/p...

( 2015-02-10 11:10:13 -0600 )edit
2

warpPerspective use the same principle than perspectiveTransform but with an image (the box in the tutorial will be warped to the box as you can seen it in the scene image). It is not always accurate for different reasons:

• first, the matching of the keyppoints is not accurate (e.g. false / bad matching)
• some other reasons may occured (general scenario, not only in the case of the tutorial): coordinates in images are not accurate, camera distorsion, image noise, numerical errors, etc.
( 2015-02-10 11:33:00 -0600 )edit

That clears a lot of things up. Thank you for you help!

( 2015-02-10 13:05:38 -0600 )edit

Official site

GitHub

Wiki

Documentation