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

FLANN特征匹配

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

C++源码

#include <opencv2/opencv.hpp>
#include <opencv2/xfeatures2d.hpp>
using namespace cv;
using namespace std;
using namespace cv::xfeatures2d;

int main()
{
	Mat srcImage = imread("curry_dlt.jpg");
	Mat dstImage = imread("curry1.jpg");

	// surf 特征提取
	int minHessian = 450;
	Ptr<SURF> detector = SURF::create(minHessian);
	vector<KeyPoint> keypoints_src;
	vector<KeyPoint> keypoints_dst;
	Mat descriptor_src, descriptor_dst;
	detector->detectAndCompute(srcImage, Mat(), keypoints_src, descriptor_src);
	detector->detectAndCompute(dstImage, Mat(), keypoints_dst, descriptor_dst);

	// matching
	FlannBasedMatcher matcher;
	vector<DMatch> matches;
	matcher.match(descriptor_dst, descriptor_src, matches);

	// find good matched points
	double minDist = 0, maxDist = 0;
	for (size_t i = 0; i < matches.size(); i++)
	{
		double dist = matches[i].distance;
		if (dist > maxDist)
			maxDist = dist;
		if (dist < minDist)
			minDist = dist;
	}

	vector<DMatch> goodMatches;
	for (size_t i = 0; i < matches.size(); i++)
	{
		double dist = matches[i].distance;
		if (dist < max(3 * minDist, 0.02))
		{
			goodMatches.push_back(matches[i]);
		}
	}

	Mat matchesImage;
	drawMatches(dstImage, keypoints_dst, srcImage, keypoints_src, goodMatches, matchesImage, Scalar::all(-1), \
		Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);

	imshow("matchesImage", matchesImage);

	waitKey(0);
	return 0;
}

blog 2 link:http://www.pianshen.com/article/5346279399/
FLANN特征匹配