欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

FLANN特征匹配

程序员文章站 2022-06-11 12:11:35
...

FLANN特征匹配时基于SIFT或者是SURF特征检测算法的一种匹配,所以它只是具有旋转不变性与光照不变性还有尺度不变性;
代码演示

#include <opencv2/opencv.hpp>
#include <opencv2/xfeatures2d.hpp>
#include <iostream>
#include <math.h>

using namespace cv;
using namespace std;
using namespace cv::xfeatures2d;

int main(int agrc, char**agrv)
{
	Mat img1 = imread("D:/image2/14m.jpg");
	Mat img2 = imread("D:/image2/14m.jpg");
	if(!img1.data || !img2.data)
	{
		return -1;
	}
	imshow("目标图像", img1);
	imshow("有目标的图像", img2);

	int minHessian = 400;
	Ptr<SURF> detector = SURF::create(minHessian);
	vector<KeyPoint> keypoints_obj;
	vector<KeyPoint> keypoints_scene;
	Mat descriptor_obj, descriptor_scene;

	detector->detectAndCompute(img1, Mat(), keypoints_obj, descriptor_obj);
	detector->detectAndCompute(img2, Mat(), keypoints_scene, descriptor_scene);

	//matching
	FlannBasedMatcher matcher;
	vector<DMatch> matches;
	matcher.match(descriptor_obj, descriptor_scene, matches);

	//find good matched points
	double minDist = 1000;
	double maxDist = 0;
	for (int i = 0; i < descriptor_obj.rows; i++)
	{
		double dist = matches[i].distance;
		if (dist > maxDist)
		{
			maxDist = dist;
		}
		if (dist < minDist)
		{
			minDist = dist;
		}	
	}
	printf("最大距离:%f\n", maxDist);
	printf("最小距离:%f\n", minDist);
	vector<DMatch> goodMatches;
	for (int i = 0; i < descriptor_obj.rows; i++)
	{
		double dist = matches[i].distance;
		if (dist < max(3 * minDist, 0.02))
		{
			goodMatches.push_back(matches[i]);
		}
	}

	Mat matchesImg;
	drawMatches(img1, keypoints_obj, img2, keypoints_scene, goodMatches,matchesImg,Scalar::all(-1),
		Scalar::all(-1),vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
	imshow("结果图", matchesImg);

 
	waitKey(0);
	return 0;
}

FLANN特征匹配