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


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

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



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;
}

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;
}


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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值