Search code examples
c++opencvsurf

OpenCV C++ Bag Of Words


Everywhere online, you can find little tutorials on different segments of a BOW, but (from I've found anyway) nothing on what you do after:

bowDE.setVocabulary(dictionary);

...

bowDE.compute(image, keypoints, descriptors);

Once you've used the BOWImgDescriptorExtractor to compute, what do you then do?

How do you find out what is a good match, and what is not?

And then can you then utilize that information?

If so, how?


Solution

  • If you have both descriptor and extractor, you can use a matcher to find matches.

    Here is a sample function:

    void drawMatches(const Mat& Img1,const Mat& Img2,const vector<KeyPoint>& Keypoints1,
        const vector<KeyPoint>& Keypoints2,const Mat& Descriptors1,const Mat& Descriptors2)
    {
        Ptr<DescriptorMatcher> descriptorMatcher = DescriptorMatcher::create( "BruteForce" ); //
        vector<DMatch> matches;
        descriptorMatcher->match( Descriptors1, Descriptors2, matches );
        Mat matchImg;
        drawMatches(Img1,Keypoints1,Img2,Keypoints2,matches,matchImg,Scalar::all(-1),CV_RGB(255,255,255),Mat(),4);
        imshow("match",show);
    }
    

    Once you get those matches, you can determine which matches are "good" by inspecting their max distance, average distance, total match size and so on.

    There is also an official tutorial about how to use those descriptors and keypoints to get matches

    Features2D + Homography to find a known object

    Although it uses a different feature detector from yours, you can still use the matching part of the article.

    Update:

    There is no way to make an accurate answer to whether a match is a "correct" match. But you can get the values of the matching pairs.

    Here is an example of "wrong" matches and "right" matches, using SIFT feature detector and BruteForce matcher.

    Part of the code:

    size_t matches_size = matches.size();
    for( unsigned i = 0; i < matches_size; i++ )
    {
        if( matches[i].distance < MY_GOOD_DISTANCE)//You can get the matching distance like this.
        {
            good_matches.push_back( matches[i]); 
        }
    }
    

    This is a right match. enter image description here

    After computing the matches, I listed the distance of the matches:

    27.7669 43.715  45.2217 47.4552 53.1601 54.074  57.3672 58.2924 59.0593 63.3009 
    63.6475 64.1093 64.8922 67.0075 70.9718 73.4507 74.0878 76.6225 76.6551 80.075  
    81.2219 82.2192 83.6959 89.2412 90.7855 91.4604 95.3363 95.352  95.6033 98.209  
    98.3362 98.3412 99.4082 101.035 104.024 109.567 110.095 110.345 112.858 118.339 
    119.311 123.976 125.948 126.625 128.02  128.269 130.219 133.015 135.739 138.43  
    144.499 146.055 146.492 147.054 152.925 160.044 161.165 168.899 170.871 179.881 
    183.39  183.573 187.061 192.764 192.961 194.268 194.44  196.489 202.255 204.854 
    230.643 230.92  231.961 233.238 235.253 236.023 244.225 246.337 253.829 260.384 
    261.383 263.934 266.933 269.232 272.586 273.651 283.891 289.261 291.805 297.165 
    297.22  297.627 304.132 307.633 307.695 314.798 325.294 334.74  335.272 344.17  
    352.095 353.456 354.144 357.398 363.762 366.344 367.301 368.977 371.102 371.44  
    371.863 372.459 372.85  373.17  376.082 378.844 382.372 389.01  389.704 397.028 
    398.236 400.53  414.523 417.628 422.61  430.731 461.3   
    
    Min value: 27.76
    Max value: 461.3
    Average: 210.2526882
    

    And here is a wrong match:

    Wrong match

    336.161 437.132 310.587 376.245 368.683 449.708 334.148 354.79  333.981 399.794 368.889 
    361.653 341.778 266.443 259.365 338.726 352.789 381.097 427.143 350.732 355.522 349.819 
    358.569 373.139 348.201 341.923 383.188 378.233 399.844 294.16  505.107 347.978 314.021 
    332.983 335.364 403.217 385.8   408.859 381.472 372.078 434.167 436.489 279.646 253.271 
    268.522 376.303 418.071 373.3   369.004 272.145 254.448 408.185 326.351 351.886 333.981 
    371.59  440.336 230.558 250.928 337.368 288.579 262.107 409.971 339.391 380.58  374.162 
    361.96  392.59  345.936 328.691 383.586 398.986 336.283 365.768 492.984 392.379 377.042 
    371.652 279.014 370.849 378.213 351.048 311.148 319.168 324.268 319.191 261.555 339.257 
    298.572 241.622 406.977 286.068 438.586 
    
    Min value: 230
    Max value: 505
    Average: 352.6009711
    

    After you get the distance of all matches, you can easily see what is a "good" match and what is a "bad" match.

    Here's the scoring part. A little bit tricky, highly related with data. MY_AVG_DISTANCE, MY_LEAST_DISTANCE, MY_MAX_DISTANCE and MY_GOOD_DISTANCE are values you should carefully select. Check your own matching distances, and select some value for them.

    int good_size = good_matches.size() > 30 ? 30 : good_matches.size(); //In case there are too many "good matches"
    //...
    //===========SCORE ============
    double avg = 0;     //Calculates the average of some of the matches. 
    int avgCount = 0;
    int goodCount = 0 ;
    for( unsigned i = 0; i < matches.size(); i++ )
    {
        double dist = matches[i].distance;
        if( dist < MY_AVG_DISTANCE  && dist > MY_LEAST_DISTANCE )
        {
            avg += dist;
            avgCount++;
        }
        if(dist < MY_GOOD_DISTANCE && dist > MY_LEAST_DISTANCE ){
            goodCount++;
        }
    }
    if(avgCount > 6){
        avg /= avgCount;
        if(goodCount < 12){
            avg = avg + (12-goodCount) * 4;
        }
    }else{
        avg = MY_MAX_DISTANCE;
    }
    
    avg = avg > MY_AVG_DISTANCE ? MY_AVG_DISTANCE : avg;
    avg = avg < MY_MIN_DISTANCE ? MY_MIN_DISTANCE : avg;
    
    double score_avg = (MY_AVG_DISTANCE - avg) / ( MY_AVG_DISTANCE - MY_MIN_DISTANCE ) * 100;
    if(formsHomography){ //Some bonus...not related with your matching method, but you can adopt something like this
        score_avg += 40;
        score_avg = score_avg > 100 ? 100 : score_avg;
    }else{
        score_avg -= 5;
        score_avg = score_avg < 0 ? 0 : score_avg;
    }
    return score_avg;