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。

  1. /** 
  2. * @SURF特征点检测并绘制特征点 
  3. * @author holybin 
  4. */  
  5.   
  6. #include <stdio.h>  
  7. #include <iostream>  
  8. #include "opencv2/core/core.hpp"  
  9. #include "opencv2/highgui/highgui.hpp"  
  10. //#include "opencv2/features2d/features2d.hpp"  
  11. #include "opencv2/nonfree/features2d.hpp"   //SurfFeatureDetector实际在该头文件中  
  12. using namespace cv;  
  13. using namespace std;  
  14.   
  15. int main( int argc, char** argv )  
  16. {  
  17.     Mat src = imread( "D:\\opencv_pic\\cat3d120.jpg", 0 );  
  18.     //Mat src = imread( "D:\\opencv_pic\\cat0.jpg", 0 );  
  19.   
  20.     if( !src.data )  
  21.     {  
  22.         cout<< " --(!) Error reading images "<<endl;  
  23.         return -1;  
  24.     }  
  25.   
  26.     //1--初始化SURF检测算子  
  27.     int minHessian = 400;  
  28.     SurfFeatureDetector detector( minHessian );  
  29.   
  30.     //2--使用SURF算子检测特征点  
  31.     vector<KeyPoint> keypoints;  
  32.     detector.detect( src, keypoints );  
  33.   
  34.     //3--绘制特征点  
  35.     Mat keypointImg;  
  36.     drawKeypoints( src, keypoints, keypointImg, Scalar::all(-1), DrawMatchesFlags::DEFAULT );  
  37.     imshow("SURF keypoints", keypointImg );  
  38.     cout<<"keypoint number: "<<keypoints.size()<<endl;  
  39.   
  40.     waitKey(0);  
  41.     return 0;  
  42. }  
/**
* @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类,实验代码如下:

  1. /** 
  2. * @SIFT特征点检测并绘制特征点 
  3. * @author holybin 
  4. */  
  5.   
  6. #include <stdio.h>  
  7. #include <iostream>  
  8. #include "opencv2/core/core.hpp"  
  9. #include "opencv2/highgui/highgui.hpp"  
  10. //#include "opencv2/features2d/features2d.hpp"  
  11. #include "opencv2/nonfree/features2d.hpp"   //SiftFeatureDetector实际在该头文件中  
  12. using namespace cv;  
  13. using namespace std;  
  14.   
  15. int main( int argc, char** argv )  
  16. {  
  17.     Mat src = imread( "D:\\opencv_pic\\cat3d120.jpg", 0 );  
  18.     //Mat src = imread( "D:\\opencv_pic\\cat0.jpg", 0 );  
  19.   
  20.     if( !src.data )  
  21.     {  
  22.         cout<< " --(!) Error reading images "<<endl;  
  23.         return -1;  
  24.     }  
  25.   
  26.     //1--初始化SIFT检测算子  
  27.     //int minHessian = 400;  
  28.     SiftFeatureDetector detector;//( minHessian );  
  29.   
  30.     //2--使用SIFT算子检测特征点  
  31.     vector<KeyPoint> keypoints;  
  32.     detector.detect( src, keypoints );  
  33.   
  34.     //3--绘制特征点  
  35.     Mat keypointImg;  
  36.     drawKeypoints( src, keypoints, keypointImg, Scalar::all(-1), DrawMatchesFlags::DEFAULT );  
  37.     imshow("SIFT keypoints", keypointImg );  
  38.     cout<<"keypoint number: "<<keypoints.size()<<endl;  
  39.   
  40.     waitKey(0);  
  41.     return 0;  
  42. }  
/**
* @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算子进行特征点匹配来区分。



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值