开头语
Surf算法的原理就不介绍了,网上很多关于Surf算法的详解或者是开源的代码,Surf算法是对Sift算法的一种改进,主要的优势还是在执行效率上要快于Sift。博主现在正在学习如何将这些算法运用到摄像头提取的图像上来寻找特征点。希望共同学习和进步,先来看看Surf算法在静态图像上的应用。博主还检测了程序主要代码的运行时间。
代码如下:
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/nonfree/nonfree.hpp>
#include<opencv2/legacy/legacy.hpp>
#include <iostream>
#include<time.h>
using namespace cv;
using namespace std;
int main()
{
Mat srcImage1 = imread("D:\\11.jpg",1);
Mat srcImage2 = imread("D:\\12.jpg",1);
if(!srcImage1.data||!srcImage2.data)
{
printf("读取图片错误,请确定目录下是否有imread函数指定的图片\n");
return false;
}
clock_t start,finish = clock();
double duration = 0;
start = clock();
start = clock();
int minHessian = 700;
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);
//实例化一个匹配器
BruteForceMatcher<L2<float>>matcher;
std::vector<DMatch>matches;
matcher.match(descriptors1,descriptors2,matches);
Mat imgMatches;
drawMatches(srcImage1,keyPoint1,srcImage2,keyPoint2,matches,imgMatches);
imshow("匹配图",imgMatches);
finish = clock();
duration = (double)(finish-start)/CLOCKS_PER_SEC;
printf("%f seconds\n",duration);
waitKey(0);
return 0;
}