【opencv学习笔记】BF暴力匹配

1、前言

暴力匹配(Brute-Force-Match)是opencv中一种很基础的匹配方法,它的算法比较简单。

2、算法

//暴力匹配算法
#include<iostream>
int main()
{
	char *s = "BBCABCDABABCDABCDABDE";
	char *p = "ABCDABD";
	int slen = strlen(s);
	int plen = strlen(p);
	int i = 0, j = 0;
	while (i < slen && j < plen)
	{	//如果当前字符匹配成功
		if (s[i] == p[j])
		{
			i++;
			j++;
		}
		//如果失配,即s[i] != p[j]
		else 
		{
			i = i - j + 1;
			j = 0;
		}
	}
	//匹配成功
	if (j == plen)
		printf("目标在原匹配串中的位置:%d", (i-j));	
	else
		return -1;
	
}

3、使用SURF算法进行暴力匹配

代码:


# include<opencv2\opencv.hpp>
# include<opencv2/xfeatures2d.hpp>
# include <iostream>
using namespace std;
using namespace cv;
using namespace cv::xfeatures2d;
int main(int argc, char** argv) {
	Mat src1, dst,src2;
	src1 = imread("E:/tuku/lang.jpg",IMREAD_GRAYSCALE);
	src2= imread("E:/tuku/lang.jpg", IMREAD_GRAYSCALE);
	if (src1.empty()||src2.empty()) {
		cout << "can't find this picture...";
		return -1;
	}
	//SURF特征点检测
	int minHessian = 400;
	Ptr<SURF>detecor = SURF::create(minHessian);
	vector<KeyPoint> keypoint_1;
	vector<KeyPoint> keypoint_2;
	Mat descript_1, descript_2;
	detecor->detectAndCompute(src1, Mat(), keypoint_1, descript_1);
	detecor->detectAndCompute(src2, Mat(), keypoint_2, descript_2);

	BFMatcher matcher;//有许多匹配的方法;
	vector<DMatch> matchers; //这也是一个结构体,是一个用来盛放匹配数据的point;
	matcher.match(descript_1, descript_2, matchers);
	Mat matchImage;
	drawMatches(src1, keypoint_1, src2, keypoint_2, matchers, matchImage);
	imshow("out put", matchImage);
	waitKey(0);
	return 0;
}

效果图

【opencv学习笔记】BF暴力匹配_第1张图片

你可能感兴趣的:(opencv)