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;}