openCV中的特征检测及匹配步骤

openCV中的特征检测及匹配步骤

包含头文件:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>

示例代码如下:

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>
using namespace std;
using namespace cv;

int main(int argc, char** argv)
{
    cv::Mat img1 = cv::imread("/home/gyy/Desktop/SLAM/slam_code/data/rgb1.png");
    cv::Mat img2 = cv::imread("/home/gyy/Desktop/SLAM/slam_code/data/rgb2.png");

    if(img1.data && img2.data)
    {
        /**
    * 使用ORB特征
    */
        //1-创建特征检测器(opencv2.xx写法)
        cv::Ptr<FeatureDetector> detector = cv::FeatureDetector::create("ORB");
        // -- 创建vector数组存储特征点
        vector<cv::KeyPoint> keypoints1,keypoints2;
        detector->detect(img1,keypoints1);
        detector->detect(img2,keypoints2);
        //opencv版本问题如下:
        //------------------------------------------------------------------
        // used in OpenCV3 
    Ptr<FeatureDetector> detector = ORB::create();
    Ptr<DescriptorExtractor> descriptor = ORB::create();
    // use this if you are in OpenCV2 
    // Ptr<FeatureDetector> detector = FeatureDetector::create ( "ORB" );
    // Ptr<DescriptorExtractor> descriptor = DescriptorExtractor::create ( "ORB" );
    Ptr<DescriptorMatcher> matcher  = DescriptorMatcher::create ( "BruteForce-Hamming" );
    //-- 第一步:检测 Oriented FAST 角点位置
    detector->detect ( img_1,keypoints_1 );
    detector->detect ( img_2,keypoints_2 );

    //-- 第二步:根据角点位置计算 BRIEF 描述子
    descriptor->compute ( img_1, keypoints_1, descriptors_1 );
    descriptor->compute ( img_2, keypoints_2, descriptors_2 );

    //-- 第三步:对两幅图像中的BRIEF描述子进行匹配,使用 Hamming 距离
    vector<DMatch> match;
    //BFMatcher matcher ( NORM_HAMMING );
    matcher->match ( descriptors_1, descriptors_2, match );
//-------------------------------------------------------------------------------------------

        //2-创建描述器
        cv::Ptr<DescriptorExtractor> descriptor = cv::DescriptorExtractor::create("ORB");
        // -- 创建Mat数组存储描述子
        Mat descriptors1, descriptors2;
        descriptor->compute(img1,keypoints1,descriptors1);
        descriptor->compute(img2,keypoints2,descriptors2);

        //3-开始特征匹配
        // -- 创建vector容器,存储DMatch
        vector<DMatch> matches;
        // --创建匹配器,采用暴力汉明距离进行匹配
        cv::Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce-Hamming");
        // -- 开始匹配
        matcher->match(descriptors1,descriptors2,matches);

        //4-画出匹配
        Mat outImg1;
        drawMatches(img1,keypoints1,img2,keypoints2,matches,outImg1,Scalar::all(-1));    //将匹配好的图画进outImg中
        imshow("特征匹配",outImg1);  //显示outImg
        cv::waitKey(0);

        //5-消除那些不好的匹配,将距离太大的匹配删除掉
        vector<DMatch> good_matches;
        float min_dis = 9999, max_dis = 0;
        //找出匹配距离最大与最小值
        for(int i=0; i<matches.size(); ++i)
        {
            float dis = matches[i].distance;
            if (dis < min_dis) min_dis = dis;
            if (dis > max_dis) max_dis = dis;
        }
        cout << "max_dis" << max_dis << endl;
        cout << "min_dis" << min_dis << endl;

        for(int i=0; i<matches.size(); ++i)
        {
            if(matches[i].distance < max(min_dis*2, float(30)))
            {
                good_matches.push_back(matches[i]);
            }
        }

        //6-画出好匹配
        Mat outImg2;
        drawMatches(img1,keypoints1,img2,keypoints2,good_matches,outImg2,Scalar::all(-1));    //将匹配好的图画进outImg中
        imshow("特征匹配",outImg2);  //显示outImg
        cv::waitKey(0);
		//7-将好的匹配的特征点单独保存起来
		vector<KeyPoint> goodkeypoints1;
        vector<KeyPoint> goodkeypoints2;
        for(int i=0; i<good_matches.size(); ++i)
        {
            goodkeypoints1.push_back(keypoints1[good_matches[i].queryIdx]);
            goodkeypoints2.push_back(keypoints2[good_matches[i].trainIdx]);
        }
        //8-后续可以用这个比较好的匹配进行基础矩阵的计算
        //将好的匹配的特征点转换为二维点(KeyPoint 转换为 Point2d)
        vector<Point2d> points1;
        vector<Point2d> points2;

        for(int i=0; i<goodkeypoints1.size(); ++i)
        {
            points1.push_back(goodkeypoints1[i].pt);
            points2.push_back(goodkeypoints2[i].pt);
        }
        Mat fundamental_matrix;
        fundamental_matrix = findFundamentalMat(points1,points2, CV_FM_8POINT);
        cout << fundamental_matrix << endl;

    }
    else
    {
        cout << "no data" << endl;
    }

    return 0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值