SURF 算法 ——“加速版的具有鲁棒性的特征”算法
步骤:
特征检测 —— 特征描述 —— 特征匹配
实现流程:
(1)特征检测:SurfFeatureDetector类 . detect( )函数
(2)特征描述:
SurfDescriptorExtractor类 .
compute( )函数
(3)特征匹配:BruteForceMatcher类 . match( )函数
(这三步的实现都类似:类实例化一个对象,(定义vector或者Mat存放结果),调用函数,将计算结果存储供给下一步使用)
(4)最后显示“匹配图”:drawMatches( )函数
实现代码:
#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>
using namespace cv;
using namespace std;
int main()
{
//【1】载入素材图
Mat srcImage1 = imread("1.jpg", 1);
Mat srcImage2 = imread("2.jpg", 1);
if (!srcImage1.data || !srcImage2.data)
{
printf("读取图片错误,请确定目录下是否有imread函数指定的图片存在~! \n"); return false;
}
//【2】使用SURF算子检测关键点
int minHessian = 700;//SURF算法中的hessian阈值
SurfFeatureDetector detector(minHessian);//定义一个SurfFeatureDetector(SURF) 特征检测类对象
std::vector<KeyPoint> keyPoint1, keyPoints2;//vector模板类,存放任意类型的动态数组
//【3】调用detect函数检测出SURF特征关键点,保存在vector容器中
detector.detect(srcImage1, keyPoint1);
detector.detect(srcImage2, keyPoints2);
//【4】计算描述符(特征向量)
SurfDescriptorExtractor extractor;
Mat descriptors1, descriptors2;
extractor.compute(srcImage1, keyPoint1, descriptors1);
extractor.compute(srcImage2, keyPoints2, descriptors2);
//【5】使用BruteForce进行匹配
// 实例化一个匹配器
BruteForceMatcher< L2<float> > matcher;
std::vector< DMatch > matches;
//匹配两幅图中的描述子(descriptors)
matcher.match(descriptors1, descriptors2, matches);
//【6】绘制从两个图像中匹配出的关键点
Mat imgMatches;
drawMatches(srcImage1, keyPoint1, srcImage2, keyPoints2, matches, imgMatches);//进行绘制
//【7】显示效果图
imshow("匹配图", imgMatches);
waitKey(0);
return 0;
}