欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

OpenCV中feature2D学习——SURF和SIFT算子实现特征点检测

程序员文章站 2022-06-11 15:48:34
...


OpenCV的features2d中实现了SIFT和SURF算法,可以用于图像特征点的自动检测。具体实现是采用SurfFeatureDetector/SiftFeatureDetector类的detect函数检测SURF/SIFT特征的关键点,并保存在vector容器中,最后使用drawKeypoints函数绘制出特征点。

       实验所用环境是opencv2.4.0+vs2008+win7,测试图片是:

OpenCV中feature2D学习——SURF和SIFT算子实现特征点检测OpenCV中feature2D学习——SURF和SIFT算子实现特征点检测


SURF特征点检测

实验代码如下。这里需要注意SurfFeatureDetector是包含在opencv2/nonfree/features2d.hpp中,所以应该include这个头文件,并且在“项目属性->链接器->输入->附加依赖项”中加入库文件:opencv_nonfree240d.lib。

/**
* @SURF特征点检测并绘制特征点
* @author holybin
*/

#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
//#include "opencv2/features2d/features2d.hpp"
#include "opencv2/nonfree/features2d.hpp"	//SurfFeatureDetector实际在该头文件中
using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
	Mat src = imread( "D:\\opencv_pic\\cat3d120.jpg", 0 );
	//Mat src = imread( "D:\\opencv_pic\\cat0.jpg", 0 );

	if( !src.data )
	{
		cout<< " --(!) Error reading images "<<endl;
		return -1;
	}

	//1--初始化SURF检测算子
	int minHessian = 400;
	SurfFeatureDetector detector( minHessian );

	//2--使用SURF算子检测特征点
	vector<KeyPoint> keypoints;
	detector.detect( src, keypoints );

	//3--绘制特征点
	Mat keypointImg;
	drawKeypoints( src, keypoints, keypointImg, Scalar::all(-1), DrawMatchesFlags::DEFAULT );
	imshow("SURF keypoints", keypointImg );
	cout<<"keypoint number: "<<keypoints.size()<<endl;

	waitKey(0);
	return 0;
}
OpenCV中feature2D学习——SURF和SIFT算子实现特征点检测OpenCV中feature2D学习——SURF和SIFT算子实现特征点检测

SIFT特征点检测

同样的,使用SIFT特征描述子进行特征点检测的过程类似,只不过换成了SiftFeatureDetector类,实验代码如下:

/**
* @SIFT特征点检测并绘制特征点
* @author holybin
*/

#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
//#include "opencv2/features2d/features2d.hpp"
#include "opencv2/nonfree/features2d.hpp"	//SiftFeatureDetector实际在该头文件中
using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
	Mat src = imread( "D:\\opencv_pic\\cat3d120.jpg", 0 );
	//Mat src = imread( "D:\\opencv_pic\\cat0.jpg", 0 );

	if( !src.data )
	{
		cout<< " --(!) Error reading images "<<endl;
		return -1;
	}

	//1--初始化SIFT检测算子
	//int minHessian = 400;
	SiftFeatureDetector detector;//( minHessian );

	//2--使用SIFT算子检测特征点
	vector<KeyPoint> keypoints;
	detector.detect( src, keypoints );

	//3--绘制特征点
	Mat keypointImg;
	drawKeypoints( src, keypoints, keypointImg, Scalar::all(-1), DrawMatchesFlags::DEFAULT );
	imshow("SIFT keypoints", keypointImg );
	cout<<"keypoint number: "<<keypoints.size()<<endl;

	waitKey(0);
	return 0;
}
OpenCV中feature2D学习——SURF和SIFT算子实现特征点检测OpenCV中feature2D学习——SURF和SIFT算子实现特征点检测

从检测结果可以看出,SURF算子检测到的特征点远远多于SIFT算子,至于检测的精确度如何,后面试试利用SIFT和SURF算子进行特征点匹配来区分。