Search code examples
pythonopencvimage-processinghomographyransac

Which inliers is the RANSAC Algorithm using in cv2.FindHomography to find the homography matrix?


1) How is the RANSAC algorithm in OpenCV choosing an inlier over an outlier? I am presuming it calculates some total least square matching between the matched keypoints.

2) I am fully aware that apart from the H matrix, the cv2.FindHomography also outputs the mask. And from this mask a matched keypoint which is a 1 is considered an inlier whilst a 0 is considered an outlier. But which inliers is the RANSAC algorithm choosing to find the final H homography outputted by cv2.FindHomography? Is it considering all of them to find the H matrix?

The documentation is not explaining these two questions properly.


Solution

  • The ransacReprojThreshold argument in the findHomography docs describes inliers vs outliers:

    enter image description here

    For 1), RANSAC computes the error (vector norm) between the observed projection, dstPoint_i, and the projection of srcPoint_i computed with a candidate homography, H. Then it just does a simple threshold check. The user can set the ransacReprojThreshold to whatever value you find that works best for your data. If you're in pixel coordinates, you just set the maximum error in pixel distance for a point to be considered an inlier. Anything with greater error is considered an outlier.

    For 2), the RANSAC algorithm will simply choose the H matrix that maximizes the number of inliers based on the threshold you set. So the mask it returns to you is the is the set with the greatest number of inliers.

    Does that make sense? It doesn't look like it gives you a ton of options in terms of random sample sizes, number of iterations, etc. Here's the source, if you're interested.