SurfFeatureDetector是包含在opencv2/nonfree/features2d.hpp中,所以应该include这个头文件。

类SurfFeatureDetector中,利用类内的detect函数可以检测出SURF特征的关键点,保存在vector容器中。

 使用 DescriptorExtractor 接口来寻找关键点对应的特征向量. 特别地:
使用 SurfDescriptorExtractor 以及它的函数 compute 来完成特定的计算.

 将之前的vector变量变成向量矩阵形式保存在Mat中

使用 类BruteForceMatcher 中的match来匹配两幅图像的特征向量。
使用函数 drawMatches 来绘制检测到的匹配点


#include "opencv/cv.h"
#include "opencv/highgui.h"
#include "opencv2/nonfree/features2d.hpp"
#include "opencv2/legacy/legacy.hpp"

using namespace std;
using namespace cv;
int main()
{
Mat pic1=imread("./45.jpg");
Mat pic2=imread("./46.jpg");

vector<KeyPoint> keypointpic1,keypointpic2;
SurfFeatureDetector detector(400);

detector.detect(pic1,keypointpic1);
detector.detect(pic2,keypointpic2);

SurfDescriptorExtractor surfdesc;
Mat dst1,dst2;
surfdesc.compute(pic1,keypointpic1,dst1);
surfdesc.compute(pic2,keypointpic2,dst2);

BruteForceMatcher<L2<float> > matcher;
vector<DMatch> matches;
matcher.match(dst1,dst2,matches);

nth_element(matches.begin(),matches.begin()+24,matches.end());
matches.erase(matches.begin()+25,matches.end());

Mat showmatch;
drawMatches(pic1,keypointpic1,pic2,keypointpic2,matches,showmatch,Scalar(80,80,80));

imshow("ss",showmatch);
waitKey(0);


}



Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐