OpenCV中feature2D學習——FAST特徵點檢測與SIFT/SURF/BRIEF特徵提取與匹配
阿新 • • 發佈:2019-01-01
在前面的文章《OpenCV中feature2D學習——FAST特徵點檢測》中講了利用FAST運算元進行特徵點檢測,這裡嘗試使用FAST運算元來進行特徵點檢測,並結合SIFT/SURF/BRIEF運算元進行特徵點提取和匹配。
I、結合SIFT運算元進行特徵點提取和匹配
由於資料型別的不同,SIFT和SURF運算元只能採用FlannBasedMatcher或者BruteForceMatcher來進行匹配(參考OpenCV中feature2D學習——BFMatcher和FlannBasedMatcher)。
/** * @概述:採用FAST運算元檢測特徵點,採用SIFT運算元對特徵點進行特徵提取,並使用BruteForce匹配法進行特徵點的匹配 * @類和函式:FastFeatureDetector + SiftDescriptorExtractor + BruteForceMatcher * @author:holybin */ #include <stdio.h> #include <iostream> #include "opencv2/core/core.hpp" #include "opencv2/nonfree/features2d.hpp" //SurfFeatureDetector實際在該標頭檔案中 #include "opencv2/legacy/legacy.hpp" //BruteForceMatcher實際在該標頭檔案中 //#include "opencv2/features2d/features2d.hpp" //FlannBasedMatcher實際在該標頭檔案中 #include "opencv2/highgui/highgui.hpp" using namespace cv; using namespace std; int main( int argc, char** argv ) { Mat src_1 = imread("cat3d120.jpg"); Mat src_2 = imread("cat0.jpg"); if( !src_1.data || !src_2.data ) { cout<< " --(!) Error reading images "<<endl; return -1; } //-- Step 1: 使用FAST運算元檢測特徵點 FastFeatureDetector fast(20); vector<KeyPoint> keypoints_1, keypoints_2; fast.detect( src_1, keypoints_1 ); //FAST(src_1, keypoints_1, 20); fast.detect( src_2, keypoints_2 ); //FAST(src_2, keypoints_2, 20); cout<<"img1--number of keypoints: "<<keypoints_1.size()<<endl; cout<<"img2--number of keypoints: "<<keypoints_2.size()<<endl; //-- Step 2: 使用SIFT運算元提取特徵(計算特徵向量) SiftDescriptorExtractor extractor; //SurfDescriptorExtractor extractor; Mat descriptors_1, descriptors_2; extractor.compute( src_1, keypoints_1, descriptors_1 ); extractor.compute( src_2, keypoints_2, descriptors_2 ); //-- Step 3: 使用BruteForce法進行暴力匹配 BruteForceMatcher< L2<float> > matcher; //FlannBasedMatcher matcher; vector< DMatch > matches; matcher.match( descriptors_1, descriptors_2, matches ); cout<<"number of matches: "<<matches.size()<<endl; //-- 顯示匹配結果 Mat matchImg; drawMatches( src_1, keypoints_1, src_2, keypoints_2, matches, matchImg, Scalar::all(-1), Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS); imshow("matching result", matchImg ); imwrite("match_result.png", matchImg); waitKey(0); return 0; }
執行結果如下:
II、結合BRIEF運算元進行特徵點提取和匹配
/** * @概述:採用FAST運算元檢測特徵點,採用BRIEF運算元對特徵點進行特徵提取,並使用BruteForce匹配法進行特徵點的匹配 * @類和函式:FastFeatureDetector + BriefDescriptorExtractor + BruteForceMatcher * @author:holybin */ #include <stdio.h> #include <iostream> #include "opencv2/core/core.hpp" #include "opencv2/nonfree/features2d.hpp" //SurfFeatureDetector實際在該標頭檔案中 #include "opencv2/legacy/legacy.hpp" //BruteForceMatcher實際在該標頭檔案中 //#include "opencv2/features2d/features2d.hpp" //FlannBasedMatcher實際在該標頭檔案中 #include "opencv2/highgui/highgui.hpp" using namespace cv; using namespace std; int main( int argc, char** argv ) { Mat src_1 = imread("cat3d120.jpg"); Mat src_2 = imread("cat0.jpg"); if( !src_1.data || !src_2.data ) { cout<< " --(!) Error reading images "<<endl; return -1; } //-- Step 1: 使用FAST運算元檢測特徵點 FastFeatureDetector fast(20); vector<KeyPoint> keypoints_1, keypoints_2; fast.detect( src_1, keypoints_1 ); //FAST(src_1, keypoints_1, 20); fast.detect( src_2, keypoints_2 ); //FAST(src_2, keypoints_2, 20); cout<<"img1--number of keypoints: "<<keypoints_1.size()<<endl; cout<<"img2--number of keypoints: "<<keypoints_2.size()<<endl; //-- Step 2: 使用BRIEF運算元提取特徵(計算特徵向量) BriefDescriptorExtractor extractor; Mat descriptors_1, descriptors_2; extractor.compute( src_1, keypoints_1, descriptors_1 ); extractor.compute( src_2, keypoints_2, descriptors_2 ); //-- Step 3: 使用BruteForce法進行暴力匹配 BruteForceMatcher< L2<float> > matcher; //FlannBasedMatcher matcher; vector< DMatch > matches; matcher.match( descriptors_1, descriptors_2, matches ); cout<<"number of matches: "<<matches.size()<<endl; //-- 顯示匹配結果 Mat matchImg; drawMatches( src_1, keypoints_1, src_2, keypoints_2, matches, matchImg, Scalar::all(-1), Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS); imshow("matching result", matchImg ); imwrite("match_result.png", matchImg); waitKey(0); return 0; }
執行結果如下: