Ask Your Question

# Revision history [back]

### Compute inliers for a homography

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, 0, 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, 0, 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?

### Compute inliers for a homography

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, 0, RANSAC, 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, 0, RANSAC, 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?