用opencv3进行特征点匹配

本文介绍如何使用OpenCV3中的SURF特征检测器、描述符提取器及暴力匹配器进行图像特征点匹配。通过具体代码示例展示了特征点检测、描述符计算及匹配过程。

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

用opencv3进行特征点匹配

本篇文章主要是展示如何用opencv3来使用SurfFeatureDetector,SurfDescriptorExtractor,BruteForceMatcher类

#include "opencv2/opencv.hpp"
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/xfeatures2d.hpp"
#include "iostream"
using namespace cv;
using namespace std;
using namespace cv::xfeatures2d;

int main()
{
    system("color 2F");
    Mat srcimage1, srcimage2;
    srcimage1 = imread("E:/opencv/base2.jpg");
    if (!srcimage1.data)
    {
        cerr << "srcimage1 input error!" << endl;
        return false;
    }
    srcimage2 = imread("E:/opencv/base3.jpg");
    if (!srcimage2.data)
    {
        cerr << "srcimage2 input error!" << endl;
        return false;
    }

    //使用surf算子求特征点
    int minHession = 400;
    Ptr<SURF> detector = SURF::create(minHession);
    vector<KeyPoint> keypoint1, keypoint2;

    //从源图像中找出特征点并存放在vector中
    detector->detect(srcimage1, keypoint1);
    detector->detect(srcimage2, keypoint2);

    //把特征点描绘在图像上
    Mat image_keypoint1, image_keypoint2;
    drawKeypoints(srcimage1, keypoint1, image_keypoint1, Scalar(0, 0, 255),DrawMatchesFlags::DEFAULT);
    drawKeypoints(srcimage2, keypoint2, image_keypoint2, Scalar(0, 255,0), DrawMatchesFlags::DEFAULT);

    //计算特征向量
    Ptr<SURF> extractor = SURF::create();
    Mat descriptors1, descriptors2;
    extractor->compute(srcimage1, keypoint1, descriptors1);
    extractor->compute(srcimage2, keypoint2, descriptors2);

    //使用BruteForce进行匹配
    Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce");
    vector<DMatch> matches;
    matcher->match(descriptors1, descriptors2, matches);

    Mat imagematches;
    drawMatches(srcimage1, keypoint1, srcimage2, keypoint2, matches, imagematches);
    imshow("Matches", imagematches);

    //imshow("base1", image_keypoint1);
    //imshow("base2", image_keypoint2);

    waitKey(0);
    return 0;
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值