src/matching2D_Student.cpp
// Find best matches for keypoints in two camera images based on several matching methods
Find best matches for keypoints in two camera images based on several matching methods
Find best matches for keypoints in two camera images based on several matching methods
void matchDescriptors(std::vector<cv::KeyPoint> &kPtsSource, std::vector<cv::KeyPoint> &kPtsRef,
                      cv::Mat &descSource, cv::Mat &descRef,
                      std::vector<cv::DMatch> &matches, std::string descriptorType,
                      std::string matcherType, std::string selectorType)
{
  bool crossCheck = false;
  cv::Ptr<cv::DescriptorMatcher> matcher;
  if (matcherType == "MAT_BF")
  {
    int normType = (descriptorType == "DES_HOG") ? cv::NORM_L2 : cv::NORM_HAMMING;
    matcher = cv::BFMatcher::create(normType, crossCheck);
  }
  else if (matcherType == "MAT_FLANN")
  {
    if (descSource.type() != CV_32F)
    { 
      descSource.convertTo(descSource, CV_32F);
    }
    if (descRef.type() != CV_32F)
    {
      descRef.convertTo(descRef, CV_32F);
    }
    matcher = cv::DescriptorMatcher::create(cv::DescriptorMatcher::FLANNBASED);
  }
  if (selectorType == "SEL_NN")
  { 
    auto t = static_cast<double>(cv::getTickCount());
    matcher->match(descSource, descRef, matches); 
    t = (static_cast<double>(cv::getTickCount()) - t) / cv::getTickFrequency();
    cout << " (NN) with n=" << matches.size() << " matches in " << 1000 * t / 1.0 << " ms" << endl;
  }
  else if (selectorType == "SEL_KNN")
  { 
    int k = 2; 
    vector<vector<cv::DMatch>> knn_matches;
    auto t = static_cast<double>(cv::getTickCount());
    matcher->knnMatch(descSource, descRef, knn_matches, k); 
    t = (static_cast<double>(cv::getTickCount()) - t) / cv::getTickFrequency();
    cout << " (KNN) with n=" << knn_matches.size() << " matches in " << 1000 * t / 1.0 << " ms" << endl;
    double minDescDistRatio = 0.8;
    for (auto& knn_match : knn_matches)
    {
      if (knn_match[0].distance < minDescDistRatio * knn_match[1].distance)
      {
        matches.push_back(knn_match[0]);
      }
    }
    cout << "(KNN) # keypoints removed = " << knn_matches.size() - matches.size() << endl;
  }
}
void matchDescriptors ( std :: vector < cv :: KeyPoint > & kPtsSource , std :: vector < cv :: KeyPoint > & kPtsRef , cv :: Mat & descSource , cv :: Mat & descRef , std :: vector < cv :: DMatch > & matches , std :: string descriptorType , std :: string matcherType , std :: string selectorType ) { bool crossCheck = false ; cv :: Ptr < cv :: DescriptorMatcher > matcher ; if ( matcherType == " " ) { int normType = ( descriptorType == " " ) ? cv :: NORM_L2 : cv :: NORM_HAMMING ; matcher = cv :: BFMatcher :: create ( normType , crossCheck ) ; } else if ( matcherType == " " ) { if ( descSource . type ( ) != CV_32F ) { descSource . convertTo ( descSource , CV_32F ) ; } if ( descRef . type ( ) != CV_32F ) { descRef . convertTo ( descRef , CV_32F ) ; } matcher = cv :: DescriptorMatcher :: create ( cv :: DescriptorMatcher :: FLANNBASED ) ; } if ( selectorType == " " ) { auto t = static_cast < double > ( cv :: getTickCount ( ) ) ; matcher -> match ( descSource , descRef , matches ) ; t = ( static_cast < double > ( cv :: getTickCount ( ) ) - t ) / cv :: getTickFrequency ( ) ; cout << " " << matches . size ( ) << " " << 1000 * t / 1.0 << " " << endl ; } else if ( selectorType == " " ) { int k = 2 ; vector < vector < cv :: DMatch > > knn_matches ; auto t = static_cast < double > ( cv :: getTickCount ( ) ) ; matcher -> knnMatch ( descSource , descRef , knn_matches , k ) ; t = ( static_cast < double > ( cv :: getTickCount ( ) ) - t ) / cv :: getTickFrequency ( ) ; cout << " " << knn_matches . size ( ) << " " << 1000 * t / 1.0 << " " << endl ; double minDescDistRatio = 0.8 ; for ( auto & knn_match : knn_matches ) { if ( knn_match [ 0 ] . distance < minDescDistRatio * knn_match [ 1 ] . distance ) { matches . push_back ( knn_match [ 0 ] ) ; } } cout << " " << knn_matches . size ( ) - matches . size ( ) << endl ; } }
Find best matches for keypoints in two camera images based on several matching methods
Find best matches for keypoints in two camera images based on several matching methods
// configure matcher // OpenCV bug workaround : //     convert binary descriptors to floating point due to a bug in current OpenCV implementation // perform matching task // nearest neighbor (best match) // Finds the best match for each descriptor in desc1 // k nearest neighbors (k=2) // number of neighbours // finds the k best matches // filter matches using descriptor distance ratio test