title: Opencv Flannbasedmatcher
body: <p>I got an issue with:
      cv::FlannBasedMatcher
  More precisely with the knnMatch method.
  My program take as input and IplImage* and detect a face then it cut the faces and compare the  face founded with an image stored on my computer. If i got more than 10 good match it writes on standard output Matched.</p>
  
  <p>The images loaded aren't grayscale. Is that important?</p>
  
  <p>My problem is that it works but for a random amount of time which vary from 1 minute to 3 minutes more or less.</p>
  
  <p>The error messages always appears on the knnMatch methods. Here they are (Note that there is only one of them each time):</p>
  
  <pre><code>OpenCV Error: Assertion failed ((globalDescIdx&gt;=0) &amp;&amp; (globalDescIdx &lt; size())) in           getLocalIdx, file    /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-  2.4.9/modules/features2d/src/matchers.cpp, line 163
   libc++abi.dylib: terminating with uncaught exception of type cv::Exception:   /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.9/modules/features2d/src/matchers.cpp:163: error: (-215) (globalDescIdx&gt;=0) &amp;&amp; (globalDescIdx &lt; size()) in function getLocalIdx
  </code></pre>
  
  <p>I don't get why this exception is thrown...</p>
  
  <p>Here is my code:</p>
  
  <pre><code>int        DroneCV::matchFaces()
  {
  std::vector&lt;cv::KeyPoint&gt; keypointsO;
  std::vector&lt;cv::KeyPoint&gt; keypointsS;
  cv::Mat descriptors_object, descriptors_scene;
  cv::Mat foundFaces(this-&gt;_faceCut);
  cv::FlannBasedMatcher matcher;
  std::vector&lt;std::vector&lt;cv::DMatch&gt;&gt; matches;
  std::vector&lt;cv::DMatch&gt; good_matches;
  cv::SurfDescriptorExtractor extractor;
  cv::SurfFeatureDetector surf(this-&gt;_minHessian);
  
  surf.detect(foundFaces,keypointsS);
  surf.detect(this-&gt;_faceToRecognize,keypointsO);
  if (!this-&gt;_faceToRecognize.data || !foundFaces.data)
  {
      this-&gt;log("Fail to init data in DronceCV::matchFaces");
      return (0);
  }
  
  extractor.compute(foundFaces, keypointsS, descriptors_scene);
  extractor.compute(this-&gt;_faceToRecognize, keypointsO, descriptors_object);
  
  if(descriptors_scene.empty())//descriptors_scene.type()!=CV_32F)
  {
      this-&gt;log("Descriptor got wrong type");
      descriptors_scene.convertTo(descriptors_scene, CV_32F);
      return 0;
   }
  if(descriptors_object.type()!=CV_32F || descriptors_scene.type()!=CV_32F)
  {
      this-&gt;log("TYPE OBJECT " + std::to_string(descriptors_object.type()));
      this-&gt;log("TYPE SCENE " + std::to_string(descriptors_scene.type()));
      return (0);
  }
  //Both image must be in grayscale ???
  try {
      matcher.knnMatch( descriptors_object, descriptors_scene, matches, 5 ); // find the 2 nearest neighbors
  
  } catch (cv::Exception e) {
      this-&gt;log(e.err);
  }
  good_matches.reserve(matches.size());
  for (size_t i = 0; i &lt; matches.size(); ++i)
  {
      if (matches[i].size() &lt; 2)
          continue;
      const cv::DMatch &amp;m1 = matches[i][0];
      const cv::DMatch &amp;m2 = matches[i][1];
  
      if(m1.distance &lt;= this-&gt;_nndrRatio * m2.distance)
          good_matches.push_back(m1);
  }
  this-&gt;log("Number of good matches" + std::to_string(good_matches.size()));
  foundFaces.release();
  if (good_matches.size() &gt; 8)
      return (1);
  else
      return (0);
  </code></pre>
  
  <p>}</p>
  
  <pre><code>void        DroneCV::analyzeFrame(IplImage *img)
  {
  if (!img)
  {
      this-&gt;log("Frame empty");
      return;
  }
  
  if (this-&gt;detectFaces(img) == 1)
  {
      if (this-&gt;matchFaces() == 1)
      {
          this-&gt;log("Matched");
          cvReleaseImage(&amp;this-&gt;_faceCut);
      }
  }
  }
  </code></pre>
  
  <p>Thanks in advance for your help</p>