opencv4(三)使用SURF进行特征点匹配

进行特征点匹配的一般步骤:

  • 实例化特征点检测器,进行特征点检测
  • 实例化描述子提取器,对计算得到特征点提取描述子
  • 实例化匹配器,根据描述子进行匹配
  • 筛选优秀匹配结果并绘图

下一篇博客介绍一下feature2d和xfeature2d中的各种特征点检测器和描述子提取器,有时一种算法同时拥有检测器和提取器,比如SURF

#include<opencv2/opencv.hpp>
#include <opencv2/xfeatures2d.hpp>        //SIFT SURF

#include<iostream>
#include<vector>

constexpr auto path0 = "F:\\workspace\\opencv\\2_xfeature2d\\pic\\0.png";
constexpr auto path1 = "F:\\workspace\\opencv\\2_xfeature2d\\pic\\1.png";

int main() {
	cv::Mat image0 = cv::imread(path0, 1);
	cv::Mat image1 = cv::imread(path1, 1);

	cv::imshow("image0", image0);
	cv::imshow("image1", image1);
	/*
	step1:特征检测器
	*/
	cv::Ptr<cv::xfeatures2d::SURF> detector;
	detector = cv::xfeatures2d::SURF::create(800);  //800为海塞矩阵阈值,越大越精准

	/*
	-----SURF----
	cv::Ptr<cv::xfeatures2d::SURF> detector;
	detector = cv::xfeatures2d::SURF::create(800);  //800为海塞矩阵阈值,越大越精准

	-----SIFT-----
	cv::Ptr<cv::xfeatures2d::SIFT> detector;
	detector = cv::xfeatures2d::SIFT::create(800);//800为保留的点数

	-----ORB------
	cv::Ptr<cv::ORB> detector;
	detector  = cv::ORB::create(800);//保留点数

	-----STAR-----
	cv::Ptr<cv::xfeatures2d::StarDetector> detector;
	detector = cv::xfeatures2d::StarDetector::create();

	-----MSD-----
	cv::Ptr<cv::xfeatures2d::MSDDetector> detector;
	detector = cv::xfeatures2d::MSDDetector::create();
	*/
	std::vector <cv::KeyPoint > key0;
	std::vector <cv::KeyPoint > key1;
	detector->detect(image0,key0,cv::noArray());
	detector->detect(image1, key1, cv::noArray());
	
	/*
	step2:描述子提取器
	*/
	cv::Ptr<cv::xfeatures2d::SURF> Extractor;
	Extractor = cv::xfeatures2d::SURF::create(800);
	/*
       以下都是xfeature2d中的提取器
	-----SURF-----
	-----SIFT-----
	-----LUCID----
	-----BriefDescriptorExtractor----
	-----VGG-----
	-----BoostDesc-----

	*/
	cv::Mat descriptor0, descriptor1;
	Extractor->compute(image0, key0, descriptor0);
	Extractor->compute(image1, key1, descriptor1);

	/*
	step3:匹配器
	*/
	cv::BFMatcher matcher;//暴力匹配器
	std::vector<cv::DMatch> matches; // 存放匹配结果
	std::vector<cv::DMatch> good_matches; //存放好的匹配结果

	matcher.match(descriptor0, descriptor1, matches);             
	std::sort(matches.begin(), matches.end());     //筛选匹配点,根据match里面特征对的距离从小到大排序

	int ptsPairs = std::min(50, (int)(matches.size() * 0.15));
	std::cout << "匹配点数为" << ptsPairs << std::endl;
	for (int i = 0; i < ptsPairs; i++)
	{
		good_matches.push_back(matches[i]);              //距离最小的50个压入新的DMatch
	}

	cv::Mat result;

	cv::drawMatches(image0, key0,
		image1, key1,
		good_matches, result,
		cv::Scalar::all(-1), cv::Scalar::all(-1),
		std::vector<char>(),
		cv::DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);  //绘制匹配点  

	cv::imshow("result", result);
	cv::waitKey(0);
}

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值