surf与sift特征点检测代码实现

本文转自: http://blog.csdn.net/holybin/article/details/17247657

概述

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

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

surf与sift特征点检测代码实现_第1张图片


surf与sift特征点检测代码实现_第2张图片


SURF特征点检测

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

[cpp] view plain copy print ?
  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与sift特征点检测代码实现_第3张图片


surf与sift特征点检测代码实现_第4张图片


SIFT特征点检测

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

[cpp] view plain copy print ?
  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. }  

检测结果:

surf与sift特征点检测代码实现_第5张图片


surf与sift特征点检测代码实现_第6张图片


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




你可能感兴趣的:(surf与sift特征点检测代码实现)