#include "opencv2/core/core.hpp"
#include <opencv2/nonfree/features2d.hpp>
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/nonfree/nonfree.hpp>
#include <opencv2/legacy/legacy.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
//载入素材图
Mat srcImage1 = imread("2.jpg", 1);
Mat srcImage2 = imread("1.jpg", 1);
if (!srcImage1.data || !srcImage2.data)
{
printf("读取图片错误,请确定目录下是否有imread函数指定的图片存在!\n");
return false;
}
//使用surf算子检查关键点
int minHessian = 700;//surf中的hessian阈值
SurfFeatureDetector detector(minHessian);
std::vector<KeyPoint> keypoint1, keypoint2;
//调用detect函数检测出surf特征关键点,保存在vector容器中
detector.detect(srcImage1, keypoint1);
detector.detect(srcImage2, keypoint2);
//计算描述符
SurfDescriptorExtractor extractor;
Mat descriptors1, descriptors2;
extractor.compute(srcImage1, keypoint1, descriptors1);
extractor.compute(srcImage2, keypoint2, descriptors2);
//使用bruteforce进行匹配,实例化一个匹配器
BruteForceMatcher<L2<float>> matcher;
std::vector<DMatch> matches;
//匹配两幅图中的描述子
matcher.match(descriptors1, descriptors2, matches);
//绘制从两个图像中匹配出的关键点
Mat imgmatches;
drawMatches(srcImage1, keypoint1, srcImage2, keypoint2, matches, imgmatches);
imshow(" ", imgmatches); //imshow("图片", imgmatches);之间因为有汉字,所以一直会报error:常量中有换行符,尽量不要使用汉字,一般尽量不要用汉字
waitKey(0);
return 0;
}
//后面更改错误后,也是一直显示不出来两幅图片,是因为两幅图片太过于相似