18

I used OpenCV's cv::findHomography API to calculate the homography matrix of two planar images. The matched key points are extracted by SIFT and matched by BFMatcher. As I know, cv:findHomography use RANSAC iteration to find out the best four corresponding points to get the homography matrix. So I draw the selected four pairs of points with the calculated contour using homograhy matrix of the edge of the object. The result are as the links:

https://postimg.cc/image/5igwvfrx9/

As we can see, the selected matched points by RANSAC are correct, but the contour shows that the homography is not accurate.

But these test shows that, both the selected matched points and the homography are correct:

https://postimg.cc/image/dvjnvtm53/

My guess is that if the selected matched points are too close, the small error of the pixel position will lead to the significant error of the homography matrix. If the four points are in the corner of the image, then the shift of the matched points by 4-6 pixels still got good homography matrix. (According the homogenous coordinate, I think it is reasonable, as the small error in the near plane will be amplified in the far away)

My question is:

1.Is my guess right? 2.Since the four matched points are generated by the RANSAC iteration, the overall error of all the keypoints are minimal. But How to get the stable homography, at least making the contour's mapping is correct? The theory proved that if the four corresponding points in a plane are found, the homography matrix should be calculated, but is there any trick in the engineer work?

Cœur
  • 37,241
  • 25
  • 195
  • 267
binzhang
  • 1,171
  • 4
  • 12
  • 22
  • did you find any solution or any explanation? I am having same problem – MMH Apr 03 '14 at 08:21
  • @binzhang The images are no longer online. Could you post them again to imgur.com and add a link? It would help this question's readability with the original images. Thanks in advance. – Basj Jun 18 '18 at 23:37

3 Answers3

11

I think you're right, and the proximity of the 4 points does not help the accuracy of the result. What you observe is maybe induced by numerical issues: the result may be locally correct for these 4 points but becomes worse when going further.

However, RANSAC will not help you here. The reason is simple: RANSAC is a robust estimation procedure that was designed to find the best point pairs among many correspondences (including some wrong ones). Then, in the inner loop of the RANSAC, a standard homography estimation is performed.

You can see RANSAC as a way to reject wrong point correspondences that would provoke a bad result.

Back to your problem:

What you really need is to have more points. In your examples, you use only 4 point correspondences, which is just enough to estimate an homography. You will improve your result by providing more matches all over the target image. The problem then becomes over-determined, but a least squares solution can still be found by OpenCV. Furthermore, of there is some error either in the point correspondence process or in some point localization, RANSAC will be able to select the best ones and still give you a reliable result.

If RANSAC results in overfitting on some 4 points (as it seems to be the case in your example), try to relax the constraint by increasing the ransacReprojThreshold parameter. Alternatively, you can either:

  • use a different estimator (the robust median CV_LMEDS is a good choice if there are few matching errors)
  • or use RANSAC in a first step with a large reprojection error (to get a rough estimate) in order to detect the spurious matchings then use LMEDS on the correct ones.
sansuiso
  • 9,259
  • 1
  • 40
  • 58
  • thanks for your feedback. May be I did not describe clearly enough. In fact, I have provided with about hundreds of matched points to cvFindHomography. I have dig into the ransac source code in opencv, the four points I drawed is what the ransac process "considers" best results.(It is a kind of iteration and each time pick up four pairs of points) unfortunately, this does not always yield to good homography matrix though the pairs of points seems to matched by human eyes. – binzhang Sep 26 '13 at 08:03
  • 2
    My experiment shows: if the provided matched keypoints are distributed among the center of the rectangle. Then if I use the caculated homography to transform the corner points of the rectangle, the error is big with the small shift of the position of the keypoints. If the picked up matched keypoints' position are distributed among each of the four corners of the rectangle. Then If I use the homography to transform points inside the rectangle, the result is correct. So my problem is, if I can only get matched key points near the center of the rectangle, how can I improve the performance? – binzhang Sep 26 '13 at 08:38
  • 1
    This is apparent. Center matches will get you much bigger error. Try to get some corner matches. Improve your key point candidates. – SolessChong Apr 25 '14 at 07:25
  • A question about you're second method--using `CV_RANSAC` as a first step and then moving on to `CV_LMEDS`. Would the technique be to use the `mask` output of `findHomography` to prune the list of matches and then to run `findHomography`, this time using `CV_LMEDS`? – Victor Odouard Aug 29 '17 at 00:49
  • @VictorOdouard I think that is the idea – JohannB May 27 '19 at 18:25
7

Just to extend @sansuiso's answer, with which I agree:

If you provide around 100 correspondences to RANSAC, probably you are getting more than 4 inliers from cvFindHomography. Check the status output parameter. To obtain a good homography, you should have many more than 4 correspondences (note that 4 correspondences gives you an homography always), which are well distributed around the image and which are not linear. You can actually use a minimum number of inliers to decide whether the homography obtained is good enough.

Note that RANSAC finds a set of points that are consistent, but the way it has to say that that set is the best one (the reprojection error) is a bit limited. There is a RANSAC-like method, called MSAC, that uses a slightly different error measurement, check it out.

The bad news, in my experience, is that it is little likely to obtain a 100% precision homography most of the times. If you have several similar frames, it is possible that you see that homography changes a little between them.

There are tricks to improve this. For example, after obtaining a homography with RANSAC, you can use it to project your model into the image, and look for new correspondences, so you can find another homography that should be more accurate.

ChronoTrigger
  • 8,459
  • 1
  • 36
  • 57
1

Your target has a lot of symmetric and similar elements. As other people mentioned (and you clarified later) the point spacing and point number can be a problem. Another problem is that SIFT is not designed to deal with significant perspective distortions that are present in your case. Try to track your object through smaller rotations and as was mentioned reproject it using the latest homography to make it look as close as possible to the original. This will also allow you to skip processing heavy SIFT and to use something as lightweight as FAST with cross correlation of image patches for matching.

You also may eventually come to understanding that using points is not enough. You have to use all that you got and this means lines or conics. If a homography transforms a point Pb = H* Pa it is easy to verify that in homogeneous coordinates line Lb = Henv.transposed * La. this directly follows from the equation La’.Pa = 0 = La’ * Hinv * H * Pa = La’ * Hinv * Pb = Lb’.Pb The possible min. configurations is 1 line and three points or three lines and one point. Two lines and two points doesn’t work. You can use four lines or four points as well. Of course this means that you cannot use the openCV function anymore and has to write your own DLT and then non-linear optimization.

Vlad
  • 4,425
  • 1
  • 30
  • 39