opencv库图像特征提取与匹配--参考SLAM十四讲7.2

#include
#include
#include
#include
using namespace std;
using namespace cv;

int main(int argc, char **argv)
{
   /* if(argc !=3){
    cout<<"usage:feature_extraction img1 img2"<     return 1;
    }*/
    Mat imag1=imread("1.png");
    Mat imag2=imread("2.png");
    //初始化
    std::vector keypoints1,keypoints2;
    Mat descriptors1,descriptors2;
    Ptr orb=ORB::create();//(500,1.2f,8,31,0,2,ORB::HARRIS_SCORE,31,20);
    //第一步:检测角点位置
    orb->detect(imag1,keypoints1);
    orb->detect(imag2,keypoints2);
    //第二步:根据角点位置计算BRIEF描述子
    orb->compute(imag1,keypoints1,descriptors1);
    orb->compute(imag2,keypoints2,descriptors2);
    //画出关键点的位置
    Mat outimage1;
    drawKeypoints(imag1,keypoints1,outimage1);//,Scalar::all(-1),DrawMatchesFlags::DEFAULT);
    imshow("ORB特征点",outimage1);
    waitKey(0);
    //第三步:对两幅图像中的BRIEF描述子进行匹配,使用Hamming距离
    vector matches;
    BFMatcher matcher (NORM_HAMMING);
    matcher.match(descriptors1,descriptors2,matches);
    //第四步:匹配点筛选
    double min_dist=10000,max_dist=0;
    //找出所有匹配之间的最小距离和最大距离
    for(int i=0;i     double dist=matches[i].distance;
    if(dist         min_dist=dist;
    if(dist>max_dist)
        max_dist=dist;
    }
    printf("max_dist: %f \n",max_dist);
    printf("min_dist: %f\n",min_dist);
    //认为当描述子之间的距离大于两倍最小距离时,为误匹配
    //但有时最小距离会非常小,所以应设置下限(经验值)
    std::vector good_matches;
    for(int i=0;i     if(matches[i].distance         good_matches.push_back(matches[i]);
    }
    }
    //第五步:绘制匹配结果
    Mat imag_match;
    Mat imag_goodmatch;
    drawMatches(imag1,keypoints1,imag2,keypoints2,matches,imag_match);
    drawMatches(imag1,keypoints1,imag2,keypoints2,good_matches,imag_goodmatch);
    imshow("所有匹配点对",imag_match);
    imshow("筛选过后的点对",imag_goodmatch);
    waitKey(0);
    
    
    return 0;
}
 

你可能感兴趣的:(SLAM)