Ask Your Question
2

Compute inliers for a homography

asked 2013-04-24 07:49:52 -0600

Nyenna gravatar image

updated 2013-04-24 09:19:40 -0600

I am trying to compute the inliers of a homography found using RANSAC by myself, but I never get the exact same list than the one given by the mask. Here are two codes that I wrote. Both use the same Homography matrix cv::Mat H.

Using findHomography(), I get a mask as output and I can print the id of the inliers using the following code:

// Somewhere before I did: findHomography(points1, points2, CV_RANSAC, 3, mask)
for (int i = 0; i < nbMatchings; i++)
{
    // Select only the inliers (mask entry set to 1)
    if ((int)mask.at<uchar>(i, 0) == 1)
    {
        std::cout << i << ", ";
    }
}
std::cout << std::endl;

Then, supposing I want to compute the inliers myself (without using cv::Mat mask), and by computing the distance between a point and its correspondence using the very same code as in modules/calib3d/src/fundam.cpp:

for (int i = 0; i < nbMatchings; i++)
{
    // cv::Mat H is filled by findHomography(points1, points2, CV_RANSAC, 3, mask)
    const double* H2 = H.ptr<double>();

    float Hf[] = { (float)H2[0], (float)H2[1], (float)H2[2], (float)H2[3], (float)H2[4], (float)H2[5], (float)H2[6], (float)H2[7] };
    float ww = 1.f/(Hf[6]*points1[i].x + Hf[7]*points1[i].y + 1.f);
    float dx = (Hf[0]*points1[i].x + Hf[1]*points1[i].y + Hf[2])*ww - points2[i].x;
    float dy = (Hf[3]*points1[i].x + Hf[4]*points1[i].y + Hf[5])*ww - points2[i].y;
    float dist = (float)(dx*dx + dy*dy);

    if (dist <= 9) // The threshold used for RANSAC is 3, so the squared distance is 9
    {
        std::cout << i << ", ";
    }
}
std::cout << std::endl;

Both codes work, and almost give the same results. The point is that in my understanding, it should result exactly in the same list in both codes, and it is obviously not. Usually, the second code outputs up to 10% more inliers than the first code. But some inliers are in the first list and not in the second whilst some are in the second and not in the first.

Does anybody have an idea about this problem? Why is it the case that, using the very same Homography matrix (it is computed only once), I don't get the same inliers twice?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2013-06-06 01:24:44 -0600

SR gravatar image

From the docs:

Regardless of the method, robust or not, the computed homography matrix is refined further (using inliers only in case of a robust method) with the Levenberg-Marquardt method to reduce the re-projection error even more.

Which means: After H was obtained by RANSAC, it is further re-fined (=re-estimated) for non-linear optimization of the homography mapping. In this case, least-median of squared is used, I guess.

edit flag offensive delete link more

Comments

The size of the Mask matches the size of my keypoints.

Does it mean that when a row in the Mask is 1, that the keypoint1 and keypoint2 at the same row position are the corresponding inliers?

hayley gravatar imagehayley ( 2018-09-14 04:26:23 -0600 )edit
0

answered 2014-11-12 09:00:53 -0600

nuhnuh gravatar image

I have the same problem. The masks returned by findHomography seems to be wrong. Anybody knows if it is a bug?

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2013-04-24 07:49:52 -0600

Seen: 5,567 times

Last updated: Nov 12 '14