SURF——FLANN快速最近邻匹配

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include<xfeatures2d/nonfree.hpp>
#include<math.h>
using namespace cv;
using namespace std;
using namespace cv::xfeatures2d;


int main()
{
	Mat a = imread("C:/Users/zhang/Desktop/77.png", 0);  
	Mat b = imread("C:/Users/zhang/Desktop/76.png", 0);
	//////////////检测
	Ptr<SURF> surf;              
	surf = SURF::create(800);//海塞矩阵阈值,在这里调整精度,值越大点越少,越精准 
	Mat c, d;
	vector<KeyPoint>key1, key2;
	//KeyPoint专门为特征点建立的坐标类型
	//Point2f pt; //坐标
	//float size; //特征点邻域直径
	//float angle; //特征点的方向,值为[零,三百六十),负值表示不使用
	//int octave; //特征点所在的图像金字塔的组
	//int class_id; //用于聚类的id

	surf->detectAndCompute(a, Mat(), key1, c);
	surf->detectAndCompute(b, Mat(), key2, d);
	//detect寻找特征点的坐标
	//detectAndCompute寻找特征点的坐标同时求出特征点周围的描述子向量


	/////////////////匹配
	FlannBasedMatcher matcher;         //实例化一个FLANN快速最近领匹配器
	vector<DMatch> matches;
	//DMatch专门为特征点匹配建立的类型
	//int queryIdx; //此匹配对应的查询图像的特征描述子索引,如1,2,3。
	//int trainIdx; //此匹配对应的训练图像的特征描述子索引
	//float distance; //两个特征向量之间的欧氏距离,越小表明匹配度越高。
	matcher.match(c, d, matches);            
	//把两幅图像合成一幅图像。并且找到两幅图像相同的特征点(利用欧式距离)
	//所以matchePoints保存的是两个相同特征点之间的距离,距离越小两个特征点就会相似


	////////////////寻找最小特定个数的特征点匹配
											 
	sort(matches.begin(), matches.end());  //matches里面特征对的距离从小到大排序   
	vector< DMatch > good_matches;
	int ptsPairs = std::min(50, (int)(matches.size() * 0.15));
	cout << ptsPairs << endl;
	for (int i = 0; i < ptsPairs; i++)
	{
		good_matches.push_back(matches[i]);//距离最小的50个压入新的DMatch
	}
	Mat outimg;                                
	drawMatches(a, key1, b, key2, good_matches, outimg, Scalar::all(-1), Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS); 
	//绘制匹配点  
	

	imshow("桌面", outimg);
	waitKey(0);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_41721222/article/details/83788661