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源碼:
- DoG尺度空間構造(Scale-space extrema detection)
- 關鍵點搜索與定位(Keypoint localization)
- 方向賦值(Orientation assignment)
- 關鍵點描述(Keypoint descriptor)
- OpenCV實現:特徵檢測器FeatureDetector
- SIFT中LoG和DoG的比較
SIFT in OpenCV
構造函數:
- SIFT::SIFT(int nfeatures=0, int nOctaveLayers=3, double contrastThreshold=0.04, double edgeThreshold=
- 10, double sigma=1.6)
nOctaveLayers:金字塔中每組的層數(算法中會自己計算這個值,後面會介紹)。
contrastThreshold:過濾掉較差的特徵點的對閾值。contrastThreshold越大,返回的特徵點越少。
edgeThreshold:過濾掉邊緣效應的閾值。edgeThreshold越大,特徵點越多(被多濾掉的越少)。
sigma:金字塔第0層圖像高斯濾波係數,也就是σ。
重載操作符:
- void SIFT::operator()(InputArray img, InputArray mask, vector<KeyPoint>& keypoints, OutputArray
- descriptors, bool useProvidedKeypoints=false)
img:8bit灰度圖像
mask:圖像檢測區域(可選)
keypoints:特徵向量矩陣
descipotors:特徵點描述的輸出向量(如果不需要輸出,需要傳cv::noArray())。
useProvidedKeypoints:是否進行特徵點檢測。ture,則檢測特徵點;false,只計算圖像特徵描述。
函數源碼
- SIFT::SIFT( int _nfeatures, int _nOctaveLayers,
- double _contrastThreshold, double _edgeThreshold, double _sigma )
- : nfeatures(_nfeatures), nOctaveLayers(_nOctaveLayers),
- contrastThreshold(_contrastThreshold), edgeThreshold(_edgeThreshold), sigma(_sigma)
- // sigma:對第0層進行高斯模糊的尺度空間因子。
- // 默認爲1.6(如果是軟鏡攝像頭捕獲的圖像,可以適當減小此值)
- {
- }
主要操作還是利用重載操作符()來執行:
- void SIFT::operator()(InputArray _image, InputArray _mask,
- vector<KeyPoint>& keypoints,
- OutputArray _descriptors,
- bool useProvidedKeypoints) const
- // mask :Optional input mask that marks the regions where we should detect features.
- // Boolean flag. If it is true, the keypoint detector is not run. Instead,
- // the provided vector of keypoints is used and the algorithm just computes their descriptors.
- // descriptors – The output matrix of descriptors.
- // Pass cv::noArray() if you do not need them.
- {
- Mat image = _image.getMat(), mask = _mask.getMat();
- if( image.empty() || image.depth() != CV_8U )
- CV_Error( CV_StsBadArg, "image is empty or has incorrect depth (!=CV_8U)" );
- if( !mask.empty() && mask.type() != CV_8UC1 )
- CV_Error( CV_StsBadArg, "mask has incorrect type (!=CV_8UC1)" );
- // 得到第1組(Octave)圖像
- Mat base = createInitialImage(image, false, (float)sigma);
- vector<Mat> gpyr, dogpyr;
- // 每層金字塔圖像的組數(Octave)
- int nOctaves = cvRound(log( (double)std::min( base.cols, base.rows ) ) / log(2.) - 2);
- // double t, tf = getTickFrequency();
- // t = (double)getTickCount();
- // 構建金字塔(金字塔層數和組數相等)
- buildGaussianPyramid(base, gpyr, nOctaves);
- // 構建高斯差分金字塔
- buildDoGPyramid(gpyr, dogpyr);
- //t = (double)getTickCount() - t;
- //printf("pyramid construction time: %g\n", t*1000./tf);
- // useProvidedKeypoints默認爲false
- // 使用keypoints並計算特徵點的描述符
- if( !useProvidedKeypoints )
- {
- //t = (double)getTickCount();
- findScaleSpaceExtrema(gpyr, dogpyr, keypoints);
- //除去重複特徵點
- KeyPointsFilter::removeDuplicated( keypoints );
- // mask標記檢測區域(可選)
- if( !mask.empty() )
- KeyPointsFilter::runByPixelsMask( keypoints, mask );
- // retainBest:根據相應保留指定數目的特徵點(features2d.hpp)
- if( nfeatures > 0 )
- KeyPointsFilter::retainBest(keypoints, nfeatures);
- //t = (double)getTickCount() - t;
- //printf("keypoint detection time: %g\n", t*1000./tf);
- }
- else
- {
- // filter keypoints by mask
- // KeyPointsFilter::runByPixelsMask( keypoints, mask );
- }
- // 特徵點輸出數組
- if( _descriptors.needed() )
- {
- //t = (double)getTickCount();
- int dsize = descriptorSize();
- _descriptors.create((int)keypoints.size(), dsize, CV_32F);
- Mat descriptors = _descriptors.getMat();
- calcDescriptors(gpyr, keypoints, descriptors, nOctaveLayers);
- //t = (double)getTickCount() - t;
- //printf("descriptor extraction time: %g\n", t*1000./tf);
- }
- }
函數中用到的構造金字塔: buildGaussianPyramid(base, gpyr, nOctaves);等步驟請參見文章後續系列。