src/matching2D_Student.cpp
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.compare("MAT_BF") == 0)
    {
        int normType = descriptorType.compare("DES_BINARY") == 0 ? cv::NORM_HAMMING : cv::NORM_L2;
        matcher = cv::BFMatcher::create(normType, crossCheck);
    }
    
    else if (matcherType.compare("MAT_FLANN") == 0)
    {
        if (descSource.type() != CV_32F)
        { // OpenCV bug workaround : convert binary descriptors to floating point due to a bug in current OpenCV implementation
            descSource.convertTo(descSource, CV_32F);
            descRef.convertTo(descRef, CV_32F);
            cout << "Converted to CV_32F" << endl;
        }
        matcher = cv::DescriptorMatcher::create(cv::DescriptorMatcher::FLANNBASED);
    }

    // perform matching task
    if (selectorType.compare("SEL_NN") == 0)
    { // nearest neighbor (best match)
        matcher->match(descSource, descRef, matches); // Finds the best match for each descriptor in desc1
    }

    else if (selectorType.compare("SEL_KNN") == 0)
    { 
        // parameters
        vector<vector<cv::DMatch>> knn_matches;
        auto k = 2;
        double minDescDistRatio = 0.8;
        matcher->knnMatch(descSource, descRef, knn_matches, k);
        
        for (auto it = knn_matches.begin(); it != knn_matches.end(); ++it)
        {
            if ((*it)[0].distance < minDescDistRatio * (*it)[1].distance) 
            //if best match SSD is better than 0.8*second best match SSD, then push_back.
            {
                matches.push_back((*it)[0]);
            }
        }
    }
}
// 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.compare("MAT_BF") == 0)
    {
        int normType = descriptorType.compare("DES_BINARY") == 0 ? cv::NORM_HAMMING : cv::NORM_L2;
        matcher = cv::BFMatcher::create(normType, crossCheck);
    }
    else if (matcherType.compare("MAT_FLANN") == 0)
    {
        if (descSource.type() != CV_32F)
        { 
            descSource.convertTo(descSource, CV_32F);
            descRef.convertTo(descRef, CV_32F);
            cout << "Converted to CV_32F" << endl;
        }
        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;
        auto k = 2;
        double minDescDistRatio = 0.8;
        matcher->knnMatch(descSource, descRef, knn_matches, k);
        for (auto it = knn_matches.begin(); it != knn_matches.end(); ++it)
        {
            if ((*it)[0].distance < minDescDistRatio * (*it)[1].distance) 
            {
                matches.push_back((*it)[0]);
            }
        }
    }
}
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 . compare ( " " ) == 0 ) { int normType = descriptorType . compare ( " " ) == 0 ? cv :: NORM_HAMMING : cv :: NORM_L2 ; matcher = cv :: BFMatcher :: create ( normType , crossCheck ) ; } else if ( matcherType . compare ( " " ) == 0 ) { if ( descSource . type ( ) != CV_32F ) { descSource . convertTo ( descSource , CV_32F ) ; descRef . convertTo ( descRef , CV_32F ) ; cout << " " << endl ; } 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 ; auto k = 2 ; double minDescDistRatio = 0.8 ; matcher -> knnMatch ( descSource , descRef , knn_matches , k ) ; for ( auto it = knn_matches . begin ( ) ; it != knn_matches . end ( ) ; ++ it ) { if ( ( * it ) [ 0 ] . distance < minDescDistRatio * ( * it ) [ 1 ] . distance ) { matches . push_back ( ( * it ) [ 0 ] ) ; } } } }
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
// 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 // parameters //if best match SSD is better than 0.8*second best match SSD, then push_back.