OpenCV學習筆記__特徵檢測與匹配之 SURF演算法
阿新 • • 發佈:2019-02-13
SURF 演算法 ——“加速版的具有魯棒性的特徵”演算法
步驟:
特徵檢測 —— 特徵描述 —— 特徵匹配
實現流程:
(1)特徵檢測:SurfFeatureDetector類 . detect( )函式
(2)特徵描述:SurfDescriptorExtractor類 . compute( )函式
(3)特徵匹配:BruteForceMatcher類 . match( )函式
(這三步的實現都類似:類例項化一個物件,(定義vector或者Mat存放結果),呼叫函式,將計算結果儲存供給下一步使用)
(4)最後顯示“匹配圖”:drawMatches( )函式
實現程式碼:
#include "opencv2/core/core.hpp" #include "opencv2/features2d/features2d.hpp" #include "opencv2/highgui/highgui.hpp" #include <opencv2/nonfree/nonfree.hpp> #include<opencv2/legacy/legacy.hpp> #include <iostream> using namespace cv; using namespace std; int main() { //【1】載入素材圖 Mat srcImage1 = imread("1.jpg", 1); Mat srcImage2 = imread("2.jpg", 1); if (!srcImage1.data || !srcImage2.data) { printf("讀取圖片錯誤,請確定目錄下是否有imread函式指定的圖片存在~! \n"); return false; } //【2】使用SURF運算元檢測關鍵點 int minHessian = 700;//SURF演算法中的hessian閾值 SurfFeatureDetector detector(minHessian);//定義一個SurfFeatureDetector(SURF) 特徵檢測類物件 std::vector<KeyPoint> keyPoint1, keyPoints2;//vector模板類,存放任意型別的動態陣列 //【3】呼叫detect函式檢測出SURF特徵關鍵點,儲存在vector容器中 detector.detect(srcImage1, keyPoint1); detector.detect(srcImage2, keyPoints2); //【4】計算描述符(特徵向量) SurfDescriptorExtractor extractor; Mat descriptors1, descriptors2; extractor.compute(srcImage1, keyPoint1, descriptors1); extractor.compute(srcImage2, keyPoints2, descriptors2); //【5】使用BruteForce進行匹配 // 例項化一個匹配器 BruteForceMatcher< L2<float> > matcher; std::vector< DMatch > matches; //匹配兩幅圖中的描述子(descriptors) matcher.match(descriptors1, descriptors2, matches); //【6】繪製從兩個影象中匹配出的關鍵點 Mat imgMatches; drawMatches(srcImage1, keyPoint1, srcImage2, keyPoints2, matches, imgMatches);//進行繪製 //【7】顯示效果圖 imshow("匹配圖", imgMatches); waitKey(0); return 0; }