Feature Detection——SURF
一.先决条件
1.SURF算法
参考博客:
https://www.cnblogs.com/jinjidexuetu/p/90ace4e8de574e3d5f4e6ac16a0dc157.html
设定Hessian行列式的阀值(下面代码中要修改的)
低于Hessian行列式阀值的点不能作为最终的特征点。在实际选择阀值时,根据实际应用中对特征点数量和精确度的要求改变阀值。阀值越大,得到的特征点的鲁棒性越好。在处理场景简单的图像时,其阀值可以适当的调低。在复杂的图像中,图像经旋转或者模糊后特征点变化的数量较大,测试需要适当提高阀值。
2.OpenCV-contrib配置
github上下载opencv对应的的opencv-contrib,然后按照教程编译即可
二.代码实现
#include <stdio.h>
#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/xfeatures2d.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;
using namespace cv::xfeatures2d;
void readme();
/* @function main */
int main( int argc, char** argv )
{
if( argc != 3 )//命令行包括三个参数
{ readme(); return -1; }
Mat img_1 = imread( argv[1], IMREAD_GRAYSCALE );//读取第一幅图像
Mat img_2 = imread( argv[2], IMREAD_GRAYSCALE );//第二幅图像
if( !img_1.data || !img_2.data )//判断图像是否读取成功
{ std::cout<< " --(!) Error reading images " << std::endl; return -1; }
//-- Step 1: Detect the keypoints using SURF Detector
int minHessian = 400;//最小的阈值
Ptr<SURF> detector = SURF::create( minHessian );//指针创建
std::vector<KeyPoint> keypoints_1, keypoints_2;//关键点向量
detector->detect( img_1, keypoints_1 );//将检测到的关键点存入向量
detector->detect( img_2, keypoints_2 );
//-- Draw keypoints
Mat img_keypoints_1; Mat img_keypoints_2;//输出图像矩阵
drawKeypoints( img_1, keypoints_1, img_keypoints_1, Scalar::all(-1), DrawMatchesFlags::DEFAULT );//将img_1中的关键点keypoints_1画到img_keypoints_1中
drawKeypoints( img_2, keypoints_2, img_keypoints_2, Scalar::all(-1), DrawMatchesFlags::DEFAULT );
//-- Show detected (drawn) keypoints
imshow("Keypoints 1", img_keypoints_1 );
imshow("Keypoints 2", img_keypoints_2 );
waitKey(0);
return 0;
}
/* @function readme */
void readme()
{ std::cout << " Usage: ./SURF_detector <img1> <img2>" << std::endl; }
截图: