【OpenCV学习笔记】三十八、特征检测与匹配(三)——SURF特征点检测与匹配

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/abc8730866/article/details/70157199

特征检测与匹配(二)——SURF特征点检测与匹配

1.SURF特征点提取

2.绘制特征点

3.特征点描述符(特征向量)提取

4.使用Flann匹配器进行匹配

5.对匹配结果进行筛选(依据DMatch结构体中的float类型变量distance进行筛选)

6.绘制匹配结果

先上ppt:












代码:1.SURF特征点检测

///SURF特征点检测
#include "opencv2/opencv.hpp"
using namespace cv;
#include "opencv2/nonfree/nonfree.hpp"//SURF相关
#include <iostream>
using namespace std;
int main()
{
	Mat srcImg1 = imread("00.jpg",CV_LOAD_IMAGE_COLOR);
	Mat srcImg2 = imread("01.jpg", CV_LOAD_IMAGE_COLOR);
	Mat dstImg1, dstImg2;
	//定义SURF特征检测类对象  
	SurfFeatureDetector surfDetector;//SurfFeatureDetector是SURF类的别名  
	//定义KeyPoint变量  
	vector<KeyPoint> keyPoints1;
	vector<KeyPoint> keyPoints2;
	//特征点检测  
	surfDetector.detect(srcImg1,keyPoints1);
	surfDetector.detect(srcImg2, keyPoints2);
	//绘制特征点(关键点)  
	drawKeypoints(srcImg1, keyPoints1, dstImg1);
	drawKeypoints(srcImg2, keyPoints2, dstImg2);
	//显示结果  
	imshow("dstImg1", dstImg1);
	imshow("dstImg2", dstImg2);
	waitKey(0);
	return 0;
}

运行结果:




代码:2.SURF特征点匹配

///SURF特征点匹配
#include "opencv2/opencv.hpp"
using namespace cv;
#include "opencv2/nonfree/nonfree.hpp"//SURF相关
#include "opencv2/legacy/legacy.hpp"//匹配器相关
#include <iostream>
using namespace std;
int main()
{
	//1.SURF特征点提取——detect()方法  
	Mat srcImg1 = imread("11.jpg",CV_LOAD_IMAGE_COLOR);
	Mat srcImg2 = imread("22.jpg", CV_LOAD_IMAGE_COLOR);
	Mat dstImg1, dstImg2;
	//定义SURF特征检测类对象  
	SurfFeatureDetector surfDetector;//SurfFeatureDetector是SURF类的别名  
	//定义KeyPoint变量  
	vector<KeyPoint> keyPoints1;
	vector<KeyPoint> keyPoints2;
	//特征点检测  
	surfDetector.detect(srcImg1,keyPoints1);
	surfDetector.detect(srcImg2, keyPoints2);
	//绘制特征点(关键点)  
	drawKeypoints(srcImg1, keyPoints1, dstImg1);
	drawKeypoints(srcImg2, keyPoints2, dstImg2);
	//显示结果  
	imshow("dstImg1", dstImg1);
	imshow("dstImg2", dstImg2);
	//2.特征点描述符(特征向量)提取——compute()方法  
	SurfDescriptorExtractor descriptor;//SurfDescriptorExtractor是SURF类的别名   
	Mat description1;
	Mat description2;
	descriptor.compute(srcImg1,keyPoints1,description1);
	descriptor.compute(srcImg2, keyPoints2, description2);
	//3.使用Flann匹配器进行匹配——FlannBasedMatcher类的match()方法  
	FlannBasedMatcher matcher;//实例化Flann匹配器
	vector<DMatch> matches;
	matcher.match(description1,description2,matches);
	//4.对匹配结果进行筛选(依据DMatch结构体中的float类型变量distance进行筛选)  
	float minDistance = 100;
	float maxDistance = 0;
	for (int i = 0; i < matches.size(); i++)
	{
		if (matches[i].distance < minDistance)
			minDistance = matches[i].distance;
		if (matches[i].distance > maxDistance)
			maxDistance = matches[i].distance;
	}
	cout << "minDistance: " << minDistance << endl;
	cout << "maxDistance: " << maxDistance << endl;
	vector<DMatch> goodMatches;
	for (int i = 0; i < matches.size(); i++)
	{
		if (matches[i].distance < 2 * minDistance)
		{
			goodMatches.push_back(matches[i]);
		}
	}
	//5.绘制匹配结果——drawMatches()  
	Mat dstImg3;
	drawMatches(srcImg1, keyPoints1, srcImg2, keyPoints2, goodMatches, dstImg3);
	imshow("dstImg3", dstImg3);
	waitKey(0);
	return 0;
}

运行结果:









猜你喜欢

转载自blog.csdn.net/abc8730866/article/details/70157199