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 descriptorDistType, std::string matcherType, std::string selectorType)
{
    bool crossCheck = false;
    cv::Ptr<cv::DescriptorMatcher> matcher;
    if (matcherType.compare("MAT_BF") == 0)
    {
        if (descriptorDistType.compare("DES_BINARY") == 0) 
        {
            matcher = cv::BFMatcher::create(cv::NORM_HAMMING, crossCheck);
        } else if (descriptorDistType.compare("DES_HOG") == 0) 
        {
            matcher = cv::BFMatcher::create(cv::NORM_L2, crossCheck);
        }
    }
    else if (matcherType.compare("MAT_FLANN") == 0)
    {
        if (descSource.type() != CV_32F || descRef.type() != CV_32F)
        {
            descSource.convertTo(descSource,CV_32F);
            descRef.convertTo(descRef,CV_32F);
        }
        matcher = cv::DescriptorMatcher::create(cv::DescriptorMatcher::FLANNBASED);
    }
    if (selectorType.compare("SEL_NN") == 0)
    { 
        matcher->match(descSource, descRef, matches); 
    }
    else if (selectorType.compare("SEL_KNN") == 0)
    { 
        vector<vector<cv::DMatch>> knn_matches;
        matcher->knnMatch(descSource,descRef,knn_matches,2);
        float minDistRatio = 0.8;
        for (int i = 0; i < knn_matches.size(); i++)
        {
            float distRatio = (knn_matches.at(i).at(0).distance)/(knn_matches.at(i).at(1).distance);
            if (distRatio < minDistRatio)
            {
                matches.push_back(knn_matches.at(i).at(0)); 
            }
        }
        cout << "# keypoints removed = " << knn_matches.size() - matches.size() << endl;
        cout << "knn points size = " << knn_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 descriptorDistType , std :: string matcherType , std :: string selectorType ) { bool crossCheck = false ; cv :: Ptr < cv :: DescriptorMatcher > matcher ; if ( matcherType . compare ( " " ) == 0 ) { if ( descriptorDistType . compare ( " " ) == 0 ) { matcher = cv :: BFMatcher :: create ( cv :: NORM_HAMMING , crossCheck ) ; } else if ( descriptorDistType . compare ( " " ) == 0 ) { matcher = cv :: BFMatcher :: create ( cv :: NORM_L2 , crossCheck ) ; } } else if ( matcherType . compare ( " " ) == 0 ) { if ( descSource . type ( ) != CV_32F || descRef . type ( ) != CV_32F ) { descSource . convertTo ( descSource , CV_32F ) ; descRef . convertTo ( descRef , CV_32F ) ; } matcher = cv :: DescriptorMatcher :: create ( cv :: DescriptorMatcher :: FLANNBASED ) ; } if ( selectorType . compare ( " " ) == 0 ) { matcher -> match ( descSource , descRef , matches ) ; } else if ( selectorType . compare ( " " ) == 0 ) { vector < vector < cv :: DMatch > > knn_matches ; matcher -> knnMatch ( descSource , descRef , knn_matches , 2 ) ; float minDistRatio = 0.8 ; for ( int i = 0 ; i < knn_matches . size ( ) ; i ++ ) { float distRatio = ( knn_matches . at ( i ) . at ( 0 ) . distance ) / ( knn_matches . at ( i ) . at ( 1 ) . distance ) ; if ( distRatio < minDistRatio ) { matches . push_back ( knn_matches . at ( i ) . at ( 0 ) ) ; } } cout << " " << knn_matches . size ( ) - matches . size ( ) << endl ; cout << " " << knn_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 // We do this conversion to avoid errors based on current openCV implementation of FLANN method /* Create FLANN based descriptor */ // perform matching task // nearest neighbor (best match) // Finds the best match for each descriptor in desc1 // k nearest neighbors (k=2) // ... /* Find 2 nearest neighbors using knn search */ /* Filter matches based on dist ratio test */ /* code */ // Filter all good matches