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();
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; }
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
);
Mat img_keypoints_1
; Mat img_keypoints_2
;
drawKeypoints( img_1
, keypoints_1
, img_keypoints_1
, Scalar
::all(-1), DrawMatchesFlags
::DEFAULT
);
drawKeypoints( img_2
, keypoints_2
, img_keypoints_2
, Scalar
::all(-1), DrawMatchesFlags
::DEFAULT
);
imshow("Keypoints 1", img_keypoints_1
);
imshow("Keypoints 2", img_keypoints_2
);
waitKey(0);
return 0;
}
void readme()
{ std
::cout
<< " Usage: ./SURF_detector <img1> <img2>" << std
::endl
; }
截图: