【opencv学习笔记】FLANN特征点检测

简介

Flann-based matcher (cv::FlannBasedMatcher),使用快速近似最近邻搜索算法寻找。

相关函数

代码展示

# include<opencv2\opencv.hpp>
# include<opencv2/xfeatures2d.hpp>
# include <iostream>
using namespace std;
using namespace cv;
using namespace cv::xfeatures2d;
double t1 = (double)getTickCount();
int main(int argc, char** argv) {
	Mat src1,src2;
	src1 = imread("E:/tuku/nihuai1.jpg",IMREAD_GRAYSCALE);
	src2 = imread("E:/tuku/nihuai2.jpg", IMREAD_GRAYSCALE);
	if (src1.empty()||src2.empty()) {
		cout << "can't find this picture...";
		return -1;
	}
	imshow("input1", src1);
	imshow("input2", src2);
	//surf feature extraction
	int minHessian = 400;
	Ptr<SURF> detector = SURF::create(minHessian);
	vector<KeyPoint>keypoint_1;
	vector<KeyPoint>keypoint_2;
	Mat descriptor_1, descriptor_2;
	//计算特征点和描述子
	detector->detectAndCompute(src1, Mat(), keypoint_1, descriptor_1);
	detector->detectAndCompute(src2, Mat(), keypoint_2, descriptor_2);

	//match
	FlannBasedMatcher matcher;
	vector<DMatch>matchers;
	matcher.match(descriptor_1, descriptor_2, matchers);

	//find good matched points 寻找最大与最小距离
	double maxDist = 1000;
	double minDist = 0;
	for (int row = 0; row < descriptor_1.rows; row++) {
		double dist = matchers[row].distance;
		if (dist > maxDist)
		{
			maxDist = dist;
		}
		if (dist < minDist)
		{
			minDist = dist;
		}
	}
	printf("max distance :%f\n", maxDist);
	printf("min distance :%f\n", minDist);

	vector<DMatch>goodmatchers;
	for (int row = 0; row < descriptor_1.rows; row++) {
		double dist = matchers[row].distance;
		if (dist < max(2* minDist, 0.04)) {
			goodmatchers.push_back(matchers[row]);
		}
	}
	Mat MatchImage;
	drawMatches(src1, keypoint_1, src2, keypoint_2, goodmatchers, MatchImage, Scalar::all(-1), Scalar::all(-1),
		vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
	imshow("out put", MatchImage);
	//计算时间
	double t2= (double)getTickCount();
	double t = (t2 - t1)/ getTickFrequency();
	printf("time :%f\n", t);
	waitKey(0);
	return 0;
}

效果图

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值