27

When using OpenCV's findHomography function to estimate an homography between two sets of points, from different images, you will sometimes get a bad homography due to outliers within your input points, even if you use RANSAC or LMEDS.

// opencv java example:
Mat H = Calib3d.findHomography( src_points, dst_points, Calib3d.RANSAC, 10 );

How can you tell if the resulting 3x3 homography matrix is acceptable or not?

I have looked for an answer to this here in Stackoverflow and in Google and was unable to find it.

I found this article, but it is a bit cryptic to me:

"The geometric error for homographies"

Angie Quijano
  • 3,308
  • 3
  • 20
  • 30
Rui Marques
  • 6,898
  • 3
  • 48
  • 82
  • 2
    See question http://stackoverflow.com/questions/10972438/detecting-garbage-homographies-from-findhomography-in-opencv/10981249#10981249 – Francesco Callari Jun 15 '12 at 18:48
  • if your camera movement is limited, you could try to decompose your homography and test scale, rotation and/or translation parameters for drastical values. Other method: If you know (or take the constraint) that big parts of the images should overlap, you could compute the overlap after warping and decide whether it might be an obviously false homography. Other method: if you use RANSAC you can decide from the number of inlier (and/or the ratio inlier/total) whether the homography should be declined) – Micka Apr 01 '14 at 08:31
  • @Micka thanks for your help, I was already using RANSAC, the camera movement is not limited and I was looking for methods to discard homographies (mainly mathematical) before going into heavier computations like applying the homography and checking for overlap. – Rui Marques Apr 01 '14 at 08:49
  • 1
    if you use the built-in openCV RANSAC you don't even get the number of inlier explicitly, do you? Checking for overlap is quite inexpensive since you only need to transform the image corners. Computing determinant like shown in Francesco Callari s posting is common operation, too. – Micka Apr 01 '14 at 08:57
  • 1
    Yes OpenCV findHomography with RANSAC gives you an array with the inliers. And you are right, applying the H to the corners will probably work nicely. – Rui Marques Apr 01 '14 at 11:09

1 Answers1

25

The best way to tell if the homography is acceptable is.

1- Take the points of one image and reproject them using the computed homography.

//for one 3D point, this would be the projection
px' = H * px;
py' = H * py;
pz' = H * pz;

2- Calculate the euclidean distance between the reprojected points and the real points in the image.

Reprojection error for one point. p is the projected point and q is the real point.

enter image description here

3- Establish a treshold that decides if the reprojection error is acceptable.

For example, an error greater than one pixel wouldn't be acceptable for many tracking applications.

Jav_Rock
  • 21,011
  • 18
  • 115
  • 164
  • 11
    What you suggest is valid for tracking, when you know that quite likely two images share a patch in similar locations. When you compute a homography between two different images for checking whether one image contains the other the reprojection error is not useful at all. For example, in my case I often get that many (~30) points from one image are mapped onto the same point (or few, close points) in a second image. Do you have any suggestion for this case? – Antonio Sesto Feb 12 '13 at 11:00
  • You can still use this as a "distance" measurement between pictures. So your statement "for checking whether one image contains the other the reprojection error is not useful at all." is wrong. It is useful if you need to create a distance between pictures. – ZettaCircl Apr 24 '19 at 05:46