title: OpenCV drawMatches error
body: <p>My code consists of a section where I sort through a set of matches and define good matches based on distance. When I try to drawMatches, I receive an error:</p>
  
  <pre><code>OpenCV Error: Assertion failed (i1 &gt;= 0 &amp;&amp; i1 &lt; static_cast&lt;int&gt;(keypoints1.size())) in drawMatches, file /home/user/OpenCV/opencv-2.4.10/modules/features2d/src/draw.cpp, line 207
  terminate called after throwing an instance of 'cv::Exception'
    what():  /home/user/OpenCV/opencv-2.4.10/modules/features2d/src/draw.cpp:207: error: (-215) i1 &gt;= 0 &amp;&amp; i1 &lt; static_cast&lt;int&gt;(keypoints1.size()) in function drawMatches
  </code></pre>
  
  <p>draw.cpp file shows:</p>
  
  <pre><code>// draw matches
  for( size_t m = 0; m &lt; matches1to2.size(); m++ )
  {
       if( matchesMask.empty() || matchesMask[m] )
       {
            int i1 = matches1to2[m].queryIdx;
            int i2 = matches1to2[m].trainIdx;
            CV_Assert(i1 &gt;= 0 &amp;&amp; i1 &lt; static_cast&lt;int&gt;(keypoints1.size()));
            CV_Assert(i2 &gt;= 0 &amp;&amp; i2 &lt; static_cast&lt;int&gt;(keypoints2.size()));
            const KeyPoint &amp;kp1 = keypoints1[i1], &amp;kp2 = keypoints2[i2];
            _drawMatch( outImg, outImg1, outImg2, kp1, kp2, matchColor, flags );
       }
  }
  </code></pre>
  
  <p>My drawMatches call follows:</p>
  
  <pre><code>Mat matchesImage;
  
  drawMatches( im1, keypoints1, im2, keypoints2,
      good_matches, matchesImage, Scalar::all(-1), Scalar::all(-1),
      vector&lt;char&gt;(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
  </code></pre>
  
  <p>Can anybody help explain this error to me?</p>
  
  <p><strong>Update:</strong> </p>
  
  <p>Here is my code for the good_matches calculation</p>
  
  <pre><code>double min_dist = 10000;
  double max_dist = 0;
  
  //-- Quick calculation of max and min distances between keypoints
  for( int i = 0; i &lt; descriptors1.rows; i++ ) { 
      double dist = matches[i].distance;
      if( dist &lt; min_dist ) min_dist = dist;
      if( dist &gt; max_dist ) max_dist = dist;
  }
  
  printf("-- Max dist : %f \n", max_dist );
  printf("-- Min dist : %f \n", min_dist );
  
  //-- Draw only "good" matches
  std::vector&lt; DMatch &gt; good_matches;
  
  for( int i = 0; i &lt; descriptors1.rows; i++ ) {
      if( matches[i].distance &lt;= max(2*min_dist, 0.02) ) { 
          good_matches.push_back( matches[i]); 
      }
  }
  
  for( int i = 0; i &lt; (int)good_matches.size(); i++ ) {
      printf( "-- Good Match [%d] Keypoint 1: %d  -- Keypoint 2: %d  \n", 
          i, good_matches[i].queryIdx, good_matches[i].trainIdx ); 
  }
  
  cout &lt;&lt; "number of good matches: " &lt;&lt; (int)good_matches.size() &lt;&lt; endl;;
  
  //Draw matches and save file
  Mat matchesImage;
  
  drawMatches( im1, keypoints1, im2, keypoints2,
      good_matches, matchesImage, Scalar::all(-1), Scalar::all(-1),
      vector&lt;char&gt;(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
  </code></pre>
  
  <p><strong>Update 2:</strong></p>
  
  <pre><code>BFMatcher matcher(NORM_L2, true);
  vector&lt;DMatch&gt; matches;
  matcher.match(descriptors1, descriptors2, matches);
  </code></pre>