13

I am using the function findHomography of OpenCV with the RANSAC method in order to find the homography that relates two images linked with a set of keypoints.

The main issue is that I haven't been able to find anywhere yet what are the values of the mask matrix that the function outputs.

The only information that I know is that 0 values are outliers, and non zero values are inliers. But what does it mean the inliers value? Does anyone know?

Piece of code where I call findHomography:

cv::Mat H12;
cv::Mat mask;

H12 = cv::findHomography(FvPointsIm1, FvPointsIm2, mask, CV_RANSAC, 5); 
ui->Debug_Label->setText(Mat2QString(mask));
nbro
  • 15,395
  • 32
  • 113
  • 196
jsalvador
  • 733
  • 2
  • 7
  • 18

2 Answers2

15

The mask returned by findHomography is an 8-bit, single-channel cv::Mat (or std::vector<uchar>, if you prefer) containing either 0 or 1 indicating the outlier status.

EDIT: You access each element of the mask by calling .at<double>, which is leading to the confusing output. You should be using .at<uchar>, which will interpret the matrix value correctly.

Aurelius
  • 11,111
  • 3
  • 52
  • 69
  • To convert the values of the mask to QString I use the following for each matrix field: ` QString::number(mask.at(i,j)) ` – jsalvador Apr 04 '13 at 16:33
  • Nice, that was the mistake. Thank you very much! – jsalvador Apr 04 '13 at 17:00
  • 1
    I cannot print the value correctly by using: `mask.at(0, 0)`. It prints a small matrix icon with values "0 0, 0 1"... Do you know why? – JonasVautherin Apr 15 '13 at 14:59
  • Okay... It is just that I call findHomography() exactly as he does, and when I print `cout << mask.at(0, 0) << endl;` as you proposed, I don't get a 0 or a 1. I will see to ask another question if you think it is different as here. – JonasVautherin Apr 15 '13 at 15:15
  • 3
    @user1368342 I've reproduced your problem. Cast the result to an `int` to see the numerical value, like so: `cout << (int)mask.at(0,0) << endl;` – Aurelius Apr 15 '13 at 15:23
  • @Aurelius, you are right: http://stackoverflow.com/questions/24456788/opencv-how-to-get-inlier-points-using-findhomography-findfundamental-and-ra/24456789#24456789 Here is the question and also the answer. It was quite trivial after all. :P – rbaleksandar Jul 02 '14 at 18:20
2

I used the findHomography method after applying keypoint matching.

  • Inliers are matched keypoints that are calculated to be true positives (correct matches);
  • Outliers are matched keypoints that are calculated to be false positives (false matches).

Then you can use the mask output to extract the subset of correct matches from all matches.

  • There is an example in Python 3.6 & OpenCV 3.4.1:

    good_kp = [gray_kp[m.queryIdx].pt for m in good_matches]
    correct_matched_kp = [good_kp[i] for i in range(len(good_kp)) if mask[i]]
    
jtlz2
  • 7,700
  • 9
  • 64
  • 114
Ali Eren Çelik
  • 239
  • 4
  • 4