title: opencv DMatch imgIdx is greater than the size of the training set
body: <p>I have 1000 patterns (images I wish to look up on a particular scene), my current algorithm gets the descriptors for each pattern, stored them in a vector and use that vector to train FlannBasedMatcher.</p>
  
  <p>If I do a knnMatch with k = 3 I get matches with an imgIdx > 1000, that's the reason for this question (I tried k = 2 and have the same problem), I couldn't find anything about this, could someone give me a hand with this?.</p>
  
  <p>UPDATE : I was adding two times the patterns descriptors, that's why I was getting imgIdx above 1000, now the problem I have is that with <code>k == 2</code> I get one match with distance 0 and one with > 0, the distance ratio is 0 and my theory is that I'm getting distance 0 because the pattern matching is exactly the same as the scene, but when I do this without training I'm not getting distance 0, any idea why?</p>
  
  <p><a href="https://stackoverflow.com/questions/21836779/opencv-flannbasedmatcher-knnmatch">I have another question that's related to this one in here.</a></p>
  
  <p>UPDATED</p>
  
  <p>This is basically the code I'm running</p>
  
  <pre><code>const Ptr&lt;flann::IndexParams&gt;&amp; indexParams=new flann::KDTreeIndexParams(4);
  const Ptr&lt;flann::SearchParams&gt;&amp; searchParams=new flann::SearchParams(64);
  cv::Ptr&lt;cv::DescriptorMatcher&gt;   matcher = new cv::FlannBasedMatcher(indexParams,  searchParams);
  matcher-&gt;clear();
  std::vector&lt;Anakin::RichImg*&gt;* patterns = loadPatterns();
  std::vector&lt;cv::Mat&gt; pdescriptors(patterns-&gt;size());
  for (int i = 0; i &lt; patterns-&gt;size();i++){
      RichImg* p = patterns-&gt;at(i);
      pdescriptors.at(i) = p-&gt;getDescriptors();
  }
  this-&gt;detector-&gt;add(pdescriptors);
  this-&gt;detector-&gt;train();
  std::vector&lt; std::vector&lt;cv::DMatch&gt; &gt; m_knnMatches;
  const cv::Mat&amp; queryDescriptors = getSceneDescriptors();
  if (queryDescriptors.empty()) return;
  if(queryDescriptors.type()!=CV_32F) {
      queryDescriptors.convertTo(queryDescriptors, CV_32F);
  }
  matcher-&gt;knnMatch(queryDescriptors, m_knnMatches, 3);
  for (size_t i=0; i&lt;m_knnMatches.size(); i++) {
      const cv::DMatch&amp; match_0 = m_knnMatches[i][0];
      const cv::DMatch&amp; match_1 = m_knnMatches[i][1];
      const cv::DMatch&amp; match_2 = m_knnMatches[i][2];
      std::cout &lt;&lt; "match_0 : imgIdx(" &lt;&lt; match_0.imgIdx &lt;&lt; ") distance(" &lt;&lt; match_0.distance &lt;&lt; ")" &lt;&lt; std::endl;
      std::cout &lt;&lt; "match_1 : imgIdx(" &lt;&lt; match_1.imgIdx &lt;&lt; ") distance(" &lt;&lt; match_1.distance &lt;&lt; ")" &lt;&lt; std::endl;
      std::cout &lt;&lt; "match_2 : imgIdx(" &lt;&lt; match_2.imgIdx &lt;&lt; ") distance(" &lt;&lt; match_2.distance &lt;&lt; ")" &lt;&lt; std::endl;
  
  }
  </code></pre>
  
  <p>And results are:</p>
  
  <pre><code>match_0 : imgIdx(403) distance(0)
  match_1 : imgIdx(1403) distance(0)
  match_2 : imgIdx(1145) distance(0.224424)
  match_0 : imgIdx(403) distance(0)
  match_1 : imgIdx(1403) distance(0)
  match_2 : imgIdx(1098) distance(0.270127)
  match_0 : imgIdx(403) distance(0)
  match_1 : imgIdx(1403) distance(0)
  match_2 : imgIdx(1985) distance(0.227835)
  </code></pre>
  
  <p>NOTE: I just posted a lighter version of my code (the code needed to understand what I'm doing) and the result is just a part of what I get (original result have 800+ lines)</p>