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

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

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

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

#include
#include         //SIFT SURF

#include
#include

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 detector;
	detector = cv::xfeatures2d::SURF::create(800);  //800为海塞矩阵阈值,越大越精准

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

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

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

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

	-----MSD-----
	cv::Ptr detector;
	detector = cv::xfeatures2d::MSDDetector::create();
	*/
	std::vector  key0;
	std::vector  key1;
	detector->detect(image0,key0,cv::noArray());
	detector->detect(image1, key1, cv::noArray());
	
	/*
	step2:描述子提取器
	*/
	cv::Ptr 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 matches; // 存放匹配结果
	std::vector 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(),
		cv::DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);  //绘制匹配点  

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

opencv4(三)使用SURF进行特征点匹配_第1张图片

你可能感兴趣的:(opencv-cpp)