【OpenCV】SIFT原理与源码分析

来源:互联网 发布:心形曲线数控宏编程 编辑:程序博客网 时间:2024/04/30 06:32

SIFT简介

Scale Invariant Feature Transform,尺度不变特征变换匹配算法,是由David G. Lowe在1999年(《Object Recognition from Local Scale-Invariant Features》)提出的高效区域检测算法,在2004年(《Distinctive Image Features from Scale-Invariant Keypoints》)得以完善。

SIFT特征对旋转、尺度缩放、亮度变化等保持不变性,是非常稳定的局部特征,现在应用很广泛。而SIFT算法是将Blob检测,特征矢量生成,特征匹配搜索等步骤结合在一起优化。我会更新一系列文章,分析SIFT算法原理及OpenCV 2.4.2实现的SIFT源码:

  1. DoG尺度空间构造(Scale-space extrema detection
  2. 关键点搜索与定位(Keypoint localization
  3. 方向赋值(Orientation assignment
  4. 关键点描述(Keypoint descriptor
  5. OpenCV实现:特征检测器FeatureDetector
OpenCV2.3之后实现了SIFT的代码,2.4改掉了一些bug。本系列文章主要分析OpenCV 2.4.2SIFT函数源码。
SIFT位于OpenCV nonfree的模块,David G. Lowe申请了算法的版权,请尊重作者权力,务必在允许范围内使用。

SIFT in OpenCV

OpenCV中的SIFT函数主要有两个接口。

构造函数:

[cpp] view plaincopy
  1. SIFT::SIFT(int nfeatures=0, int nOctaveLayers=3, double contrastThreshold=0.04, double edgeThreshold=  
  2. 10, double sigma=1.6)  
nfeatures:特征点数目(算法对检测出的特征点排名,返回最好的nfeatures个特征点)。
nOctaveLayers:金字塔中每组的层数(算法中会自己计算这个值,后面会介绍)。
contrastThreshold:过滤掉较差的特征点的对阈值。contrastThreshold越大,返回的特征点越少。
edgeThreshold:过滤掉边缘效应的阈值。edgeThreshold越大,特征点越多(被多滤掉的越少)。
sigma:金字塔第0层图像高斯滤波系数,也就是σ。

重载操作符:

[cpp] view plaincopy
  1. void SIFT::operator()(InputArray img, InputArray mask, vector<KeyPoint>& keypoints, OutputArray  
  2. descriptors, bool useProvidedKeypoints=false)  

img:8bit灰度图像
mask:图像检测区域(可选)
keypoints:特征向量矩阵
descipotors:特征点描述的输出向量(如果不需要输出,需要传cv::noArray())。
useProvidedKeypoints:是否进行特征点检测。ture,则检测特征点;false,只计算图像特征描述。

函数源码

构造函数SIFT()主要用来初始化参数,并没有特定的操作:
[cpp] view plaincopy
  1. SIFT::SIFT( int _nfeatures, int _nOctaveLayers,  
  2.            double _contrastThreshold, double _edgeThreshold, double _sigma )  
  3.     : nfeatures(_nfeatures), nOctaveLayers(_nOctaveLayers),  
  4.     contrastThreshold(_contrastThreshold), edgeThreshold(_edgeThreshold), sigma(_sigma)  
  5.     // sigma:对第0层进行高斯模糊的尺度空间因子。  
  6.     // 默认为1.6(如果是软镜摄像头捕获的图像,可以适当减小此值)  
  7. {  
  8. }  

主要操作还是利用重载操作符()来执行:
[cpp] view plaincopy
  1. void SIFT::operator()(InputArray _image, InputArray _mask,  
  2.                       vector<KeyPoint>& keypoints,  
  3.                       OutputArray _descriptors,  
  4.                       bool useProvidedKeypoints) const  
  5. // mask :Optional input mask that marks the regions where we should detect features.  
  6. // Boolean flag. If it is true, the keypoint detector is not run. Instead,  
  7. // the provided vector of keypoints is used and the algorithm just computes their descriptors.  
  8. // descriptors – The output matrix of descriptors.  
  9. // Pass cv::noArray() if you do not need them.              
  10. {  
  11.     Mat image = _image.getMat(), mask = _mask.getMat();  
  12.   
  13.     if( image.empty() || image.depth() != CV_8U )  
  14.         CV_Error( CV_StsBadArg, "image is empty or has incorrect depth (!=CV_8U)" );  
  15.   
  16.     if( !mask.empty() && mask.type() != CV_8UC1 )  
  17.         CV_Error( CV_StsBadArg, "mask has incorrect type (!=CV_8UC1)" );  
  18.   
  19.           
  20.     // 得到第1组(Octave)图像  
  21.     Mat base = createInitialImage(image, false, (float)sigma);  
  22.     vector<Mat> gpyr, dogpyr;  
  23.     // 每层金字塔图像的组数(Octave)  
  24.     int nOctaves = cvRound(log( (double)std::min( base.cols, base.rows ) ) / log(2.) - 2);  
  25.   
  26.     // double t, tf = getTickFrequency();  
  27.     // t = (double)getTickCount();  
  28.       
  29.     // 构建金字塔(金字塔层数和组数相等)  
  30.     buildGaussianPyramid(base, gpyr, nOctaves);  
  31.     // 构建高斯差分金字塔  
  32.     buildDoGPyramid(gpyr, dogpyr);  
  33.   
  34.     //t = (double)getTickCount() - t;  
  35.     //printf("pyramid construction time: %g\n", t*1000./tf);  
  36.       
  37.     // useProvidedKeypoints默认为false  
  38.     // 使用keypoints并计算特征点的描述符  
  39.     if( !useProvidedKeypoints )  
  40.     {  
  41.         //t = (double)getTickCount();  
  42.         findScaleSpaceExtrema(gpyr, dogpyr, keypoints);  
  43.         //除去重复特征点  
  44.         KeyPointsFilter::removeDuplicated( keypoints );   
  45.   
  46.         // mask标记检测区域(可选)  
  47.         if( !mask.empty() )  
  48.             KeyPointsFilter::runByPixelsMask( keypoints, mask );  
  49.   
  50.         // retainBest:根据相应保留指定数目的特征点(features2d.hpp)  
  51.         if( nfeatures > 0 )  
  52.             KeyPointsFilter::retainBest(keypoints, nfeatures);  
  53.         //t = (double)getTickCount() - t;  
  54.         //printf("keypoint detection time: %g\n", t*1000./tf);  
  55.     }  
  56.     else  
  57.     {  
  58.         // filter keypoints by mask  
  59.         // KeyPointsFilter::runByPixelsMask( keypoints, mask );  
  60.     }  
  61.   
  62.     // 特征点输出数组  
  63.     if( _descriptors.needed() )  
  64.     {  
  65.         //t = (double)getTickCount();  
  66.         int dsize = descriptorSize();  
  67.         _descriptors.create((int)keypoints.size(), dsize, CV_32F);  
  68.         Mat descriptors = _descriptors.getMat();  
  69.   
  70.         calcDescriptors(gpyr, keypoints, descriptors, nOctaveLayers);  
  71.         //t = (double)getTickCount() - t;  
  72.         //printf("descriptor extraction time: %g\n", t*1000./tf);  
  73.     }  
  74. }  

函数中用到的构造金字塔: buildGaussianPyramid(base, gpyr, nOctaves);等步骤请参见文章后续系列。

(转载请注明作者和出处:http://blog.csdn.net/xiaowei_cqu 未经允许请勿用于商业用途)