【OpenCV】SIFT原理與源碼分析

【OpenCV】SIFT原理與源碼分析

http://blog.csdn.net/xiaowei_cqu/article/details/8069548


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
  6. SIFT中LoG和DoG的比較
OpenCV2.3之後實現了SIFT的代碼,2.4改掉了一些bug。本系列文章主要分析OpenCV 2.4.2SIFT函數源碼。
SIFT位於OpenCV nonfree的模塊,David G. Lowe申請了算法的版權,請尊重作者權力,務必在允許範圍內使用。

SIFT in OpenCV

OpenCV中的SIFT函數主要有兩個接口。

構造函數:

[cpp] view plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  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 未經允許請勿用於商業用途)


發佈了6 篇原創文章 · 獲贊 7 · 訪問量 163萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章