opencv surf匹配

本文介绍了一个双目视觉项目中的特征匹配过程,利用OpenCV实现SURF特征点提取及匹配,从左右两张图片中各选取400个特征点,并通过距离排序选取前50个最佳匹配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



        双目视觉项目的一部分,对两个摄像头采集到的图像surf提取特征点(共400个特征点,选取distance最小的50个)

        详细过程翻阅OpenCV2 Computer Vision Application Programming Cookbook 第八章Detecting the scale-invariant SURF features和Describing SURF features部分

        程序主题参考了http://blog.youkuaiyun.com/lee_cv/article/details/8804578



#include <iostream>
#include <fstream>
#include <cv.h>
#include <highgui.h>
#include <opencv2/nonfree/features2d.hpp>
#include "opencv2/core/core.hpp"          //因为在属性中已经配置了opencv等目录,所以把其当成了本地目录一样
#include "opencv2/features2d/features2d.hpp"
#include <opencv2/legacy/legacy.hpp>
#include   "opencv.hpp"
using namespace std;


int main()
{

    const char LEFT_PATH[]= {"/home/liyuanzhe/two-view/code/opencv_test/left2.jpg"};
    const char RIGHT_PATH[]= {"/home/liyuanzhe/two-view/code/opencv_test/right2.jpg"};

    cv::Mat img_left = cv::imread(LEFT_PATH);
    cv::Mat img_right = cv::imread(RIGHT_PATH);
    cv::Mat img_left_trans = img_right;
    IplImage*    img_left_trans_c = cvLoadImage(LEFT_PATH);

    if(!img_left.data || !img_right.data)//如果数据为空
    {
        cout<<"opencv error \n"<<endl;
        return -1;
    }
    cout<<"open OK \n"<<endl;

    cv::SurfFeatureDetector detector(400);
    std::vector<cv::KeyPoint> keypoints_left,keypoints_right;//构造2个专门由点组成的点向量用来存储特征点

    detector.detect(img_left,keypoints_left); //将img_left中的特征点放到keypoints-left中
    detector.detect(img_right,keypoints_right);

    cv::Mat img_keypoints_left=img_left; //创建新图像,用于画特征点
    cv::Mat img_keypoints_right=img_right;

    //将特征点keypoints—left画到img-left中
    cv::drawKeypoints(img_left,keypoints_left,
                      img_keypoints_left,cv::Scalar::all(-1),
                      cv::DrawMatchesFlags::DEFAULT);
    cv::drawKeypoints(img_right,keypoints_right,
                      img_keypoints_right,cv::Scalar::all(-1),
                      cv::DrawMatchesFlags::DEFAULT);

    //计算特征向量
    cv::SurfDescriptorExtractor extractor;//定义描述子对象

    cv::Mat descriptors_left,descriptors_right;//存放特征向量的矩阵

    extractor.compute(img_left,keypoints_left,descriptors_left);
    extractor.compute(img_right,keypoints_right,descriptors_right);

    //用burte force进行匹配特征向量
    cv::BruteForceMatcher<cv::L2<float> >matcher;//定义一个burte force matcher对象
    vector<cv::DMatch>matches;
    matcher.match(descriptors_left,descriptors_right,matches);

    //get 25matches with lowest distance
    std::nth_element(matches.begin(),matches.begin()+49,matches.end());
    matches.erase(matches.begin()+50,matches.end());

    //绘制匹配线段
    cv::Mat img_matches;
    cv::drawMatches(img_left,keypoints_left,img_right,keypoints_right,matches,img_matches);//将匹配出来的结果放入内存img_matches中

    cv::namedWindow("img_left");
    cv::namedWindow("img_right");
    cv::namedWindow("img_match");
    cv::namedWindow("img_left_trans");

    cv::imshow("img_left",img_keypoints_left);
    cv::imshow("img_right",img_keypoints_right);
    cv::imshow("img_match",img_matches);
    cv::imshow("img_left_trans",img_left_trans);

    
    cv::imwrite("/home/liyuanzhe/two-view/code/opencv_test/matches.jpg",img_matches);


cv::waitKey(0); return 0;}







                
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值