SLAM十四讲 ch7 demo 复写

无人久伴 提交于 2019-12-02 06:04:01

识别特征点并实现匹配

博主刚刚入门SLAM,之后可能会更多分享SLAM相关的学习总结。

刚刚学习1个多星期的SLAM,感觉自己的知识基础、论文阅读能力严重不足。幸好有高翔的《视觉SLAM十四讲》,由此书入门,觉得更容易了一些。之后应该会把全书通读一遍,而后找时间进行笔记整理和分享,也便于自己以后的复习。

我是看过1,2讲后直接入手第7讲,由于有需求的督促想尽快达到应用级别的了解,后续再补上数学基础。

纸上得来终觉浅,深知此事要躬行。读懂ch7的代码后,博主重写了一遍以加深印象,也加了一些备注以共查看。

 

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

int main ( int argc, char** argv )
{
    if ( argc != 3 )
    {
        cout<<"usage: feature_extraction img1 img2"<<endl;
        return 1;
    }

    
    //1.Input two pictures
    Mat img_1 = imread( argv[1], CV_LOAD_IMAGE_COLOR );
    Mat img_2 = imread( argv[2], CV_LOAD_IMAGE_COLOR );
    
    
    //2.perpararory work
    
    //(1)Save key points(FAST)
    std::vector<KeyPoint> keypoints_1, Keypoints_2;
    
    //(2)Save descriptors (BRIEF)
    Mat descriptors_1, descriptors_2;
    
    //(3)Save results of matching
    std::vector<DMatch> matches;
    
    //(4)Create KeyPoint detector( a tool for geting keypoints)
    Ptr<FeatureDetector> detector = ORB::create();
 
    //(5)Create descriptor extractor( a tool for geting descriptor)
    Ptr<DescriptorExtractor> extractor = ORB::create();
    
    //(6)Create descriptor matcher( a tool for geting keypoint pairing in two pictures by descriptor) 
    Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create( "BruteForce-Hamming" );
    
    
    //3.Get key points
    detector->detect( img_1, keypoints_1 );
    detector->detect( img_2, Keypoints_2 );
    
    
    //4.Draw key points and disply the image
    Mat outimg_1;
    drawKeypoints( img_1, keypoints_1, outimg_1, Scalar::all(-1), DrawMatchesFlags::DEFAULT);
    imshow( "ORB key points" , outimg_1 );
    
    
    //5.Get keypoints descriptions
    extractor->compute(img_1, keypoints_1, descriptors_1);
    extractor->compute(img_2, Keypoints_2, descriptors_2);
    
    
    //6.Get matches 
    matcher->match(descriptors_1, descriptors_2, matches);
    
    
    //7.Draw all matches and disply picture
    Mat img_match;
    drawMatches(img_1, keypoints_1, img_2, Keypoints_2, matches, img_match);
    imshow( "all matches" , img_match );
    
    
    //8.Remove inaccurate matches(optimization)
    
    //Get minimum and maximum distances
    double min_dist=100000.0, max_dist=0.0;
    for(int i=0; i<descriptors_1.rows; i++){
	double temp_dist = matches[i].distance;
	if(min_dist>temp_dist) min_dist = temp_dist;
	if(max_dist<temp_dist) max_dist = temp_dist;
    }
    
    //Ouput minimum and maximum distances
    cout<<"maximum distance: "<<max_dist<<endl;
    cout<<"minimum distance: "<<min_dist<<endl;
    
    //Remove inaccurate matches
    std::vector<DMatch> accurate_match;
    for(int i=0; i<descriptors_1.rows; i++){
	if( matches[i].distance < max(min_dist*2, 40.0) )
	    accurate_match.push_back(matches[i]);
    }
    
    //Show result
    Mat img_accurate_match;
    drawMatches(img_1, keypoints_1, img_2, Keypoints_2, accurate_match, img_accurate_match);
    imshow( "opimized matches" , img_accurate_match );
    
    //pause
    waitKey(0);
    

    return 0;
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!