0

I am using FAST and FREAK to get the descriptors of a couple of images and then I apply knnMatch with a BruteForceMatcher matcher and next I am using a loop to separate the good matches:

  float nndrRatio = 0.7f;
  std::vector<KeyPoint> keypointsA, keypointsB;
  Mat descriptorsA, descriptorsB;
  std::vector< vector< DMatch >  > matches; 

  int threshold=9;
       // detect keypoints:
  FAST(objectMat,keypointsA,threshold,true);
  FAST(sceneMat,keypointsB,threshold,true);

  FREAK extractor;
       // extract descriptors:
  extractor.compute( objectMat, keypointsA, descriptorsA );
  extractor.compute( sceneMat, keypointsB, descriptorsB );

  BruteForceMatcher<Hamming> matcher;
       // match
  matcher.knnMatch(descriptorsA, descriptorsB, matches, 2);


       // good matches search: 
  vector< DMatch > good_matches;

  for (size_t i = 0; i < matches.size(); ++i)
  { 
        if (matches[i].size() < 2)      
              continue;

        const DMatch &m1 = matches[i][0];   
        const DMatch &m2 = matches[i][1];

        if(m1.distance <= nndrRatio * m2.distance)        
        good_matches.push_back(m1);   
  }

       //If there are at least 7 good matches, then object has been found
  if ( (good_matches.size() >=7))
  { 
  cout << "OBJECT FOUND!" << endl;
  }

I think the problem could be the good matches search method, because using it with the FlannBasedMatcher works fine but with the BruteForceMatcher very weirdly. I'm suspecting that I may be doing a nonsense with this method because the Hamming distance uses binary descriptors, but I can't think of a way to adapt it!

Any links, snippets, ideas,... please?

Str1101
  • 859
  • 2
  • 12
  • 22
  • So the problem is that the matches given by the BFMatcher are wrong? How do you initialize it? `cv::BFMatcher(cv::NORM_HAMMING)`? – JonasVautherin Jun 25 '13 at 12:43
  • I tried with: `cv::BFMatcher matcher(cv::NORM_HAMMING);` but the results are almost the same. I still think that my problem may be in the latest part... – Str1101 Jun 25 '13 at 19:59
  • I still don't get your problem. Why do you say that the results are bad? – JonasVautherin Jun 26 '13 at 07:18
  • My problem is, basically, that I don't know how to use the matches that returns the knnMatch function to detect if the object of the first image is inside the second one or not. I have been doing some research and I'm not sure if I should compute the fundamental matrix or the Homography in order to be able to get a value indicating the odds of the object in `objectMat` being inside `sceneMat`. I really don't know what is the right approach... – Str1101 Jun 26 '13 at 10:17
  • And why do you say that it works fine using the FlannBasedMatcher? – JonasVautherin Jun 26 '13 at 12:46
  • Because after comparing some images I almost didn't get false positives using the `(good_matches.size() >=7)` condition to determine if the object is in the second image, but using the same with FAST+FREAK+BFMatcher totally fails :( – Str1101 Jun 26 '13 at 13:48

1 Answers1

1

Your code is not bad, but I don't think it is what you want to do. Why did you choose this method?

If you want to detect an object in an image using OpenCV, you should maybe try the Cascade Classification. This link will explain how to train a classifier.

EDIT: If you think it is too complicated and if the object you want to detect is planar, you can try this tutorial (it basically computes the inliers by trying to find a homography transform between the object and the image). But the cascade classification is more general for object detection.

JonasVautherin
  • 7,297
  • 6
  • 49
  • 95
  • Thank you very much for your time @JonesV , I really think that cascade classification could be the method that I was looking for and I'm going to give it a try. And thanks for the other link too, I'll keep it as a plan B :) – Str1101 Jun 26 '13 at 16:53