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;
    int normType;
    if (descriptorType.compare("DES_BINARY")) normType = cv::NORM_HAMMING;
    else if (descriptorType.compare("DES_HOG")) normType = cv::NORM_L2;
    else throw invalid_argument("Unknown descriptorType " + descriptorType);
    if (matcherType.compare("MAT_BF") == 0) matcher = cv::BFMatcher::create(normType, crossCheck);
    else if (matcherType.compare("MAT_FLANN") == 0){
        if(normType==cv::NORM_HAMMING)
        {
            const cv::Ptr<cv::flann::IndexParams>& indexParams = cv::makePtr<cv::flann::LshIndexParams>(12, 20, 2);
            matcher = cv::makePtr<cv::FlannBasedMatcher>(indexParams);
        }
        else matcher = cv::FlannBasedMatcher::create();
    }
    else throw invalid_argument("Unknown matcherType " + matcherType);
    if (selectorType.compare("SEL_NN") == 0) { 
        matcher->match(descSource, descRef, matches); 
    } else if (selectorType.compare("SEL_KNN") == 0) { 
        int k = 2;
        vector<vector<cv::DMatch>> knn_matches;
        matcher->knnMatch(descSource, descRef, knn_matches, k);
        double minDescDistRatio = 0.8;
        for (auto matchPair : knn_matches) {
            if (matchPair[0].distance < minDescDistRatio * matchPair[1].distance) {
                matches.push_back(matchPair[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 ; int normType ; if ( descriptorType . compare ( " " ) ) normType = cv :: NORM_HAMMING ; else if ( descriptorType . compare ( " " ) ) normType = cv :: NORM_L2 ; else throw invalid_argument ( " " + descriptorType ) ; if ( matcherType . compare ( " " ) == 0 ) matcher = cv :: BFMatcher :: create ( normType , crossCheck ) ; else if ( matcherType . compare ( " " ) == 0 ) { if ( normType == cv :: NORM_HAMMING ) { const cv :: Ptr < cv :: flann :: IndexParams > & indexParams = cv :: makePtr < cv :: flann :: LshIndexParams > ( 12 , 20 , 2 ) ; matcher = cv :: makePtr < cv :: FlannBasedMatcher > ( indexParams ) ; } else matcher = cv :: FlannBasedMatcher :: create ( ) ; } else throw invalid_argument ( " " + matcherType ) ; if ( selectorType . compare ( " " ) == 0 ) { matcher -> match ( descSource , descRef , matches ) ; } else if ( selectorType . compare ( " " ) == 0 ) { int k = 2 ; vector < vector < cv :: DMatch > > knn_matches ; matcher -> knnMatch ( descSource , descRef , knn_matches , k ) ; double minDescDistRatio = 0.8 ; for ( auto matchPair : knn_matches ) { if ( matchPair [ 0 ] . distance < minDescDistRatio * matchPair [ 1 ] . distance ) { matches . push_back ( matchPair [ 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
// configure matcher // perform matching task // nearest neighbor (best match) // Finds the best match for each descriptor in desc1 // k nearest neighbors (k=2) // Filter knn matches by descriptor distance ratio