In the previous article "Feature2d Learning--fast feature point detection in OpenCV", the feature point detection using fast operator is described, and the fast operator is used to detect feature points, and the feature points are extracted and matched by sift/surf/brief operators.
I, combining sift operator to extract and match feature points
Because of the different data types, the SIFT and surf operators can only be matched by Flannbasedmatcher or bruteforcematcher (see OpenCV Learning in Feature2d- Bfmatcher and Flannbasedmatcher).
/*** @ Overview: Using fast operator to detect feature points, using SIFT operator to extract feature points, and using Bruteforce matching method to match feature points * @ class and function: Fastfeaturedetector + Siftdescriptorextractor + bruteforcematcher* @author: holybin*/#include <stdio.h> #include <iostream># The include "Opencv2/core/core.hpp" #include "opencv2/nonfree/features2d.hpp"//surffeaturedetector actually include in the header file " OPENCV2/LEGACY/LEGACY.HPP "//bruteforcematcher actual in the header file//#include" opencv2/features2d/features2d.hpp "// Flannbasedmatcher actual in this header file # 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: Detect feature points using fast operator Fastfeaturedetector fast (;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: Extract features using SIFT operator (calculate eigenvector) 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: Violent matching using Bruteforce method bruteforcematcher< l2<float> > matcher;// Flannbasedmatcher matcher;vector< dmatch > Matches;matcher.match (descriptors_1, descriptors_2, matches); cout << "Number of Matches:" <<matches.size () <<endl;//--Show matching results 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;}
The results of the operation are as follows:
II, combining brief operator to extract and match feature points
Brief operators can only be matched by Bruteforcematcher (refer to Feature2d learning--bfmatcher and Flannbasedmatcher in OpenCV).
/*** @ Overview: Using fast operator to detect feature points, using brief operator to extract feature points, and using Bruteforce matching method to match feature points * @ class and function: Fastfeaturedetector + Briefdescriptorextractor + bruteforcematcher* @author: holybin*/#include <stdio.h> #include <iostream># The include "Opencv2/core/core.hpp" #include "opencv2/nonfree/features2d.hpp"//surffeaturedetector actually include in the header file " OPENCV2/LEGACY/LEGACY.HPP "//bruteforcematcher actual in the header file//#include" opencv2/features2d/features2d.hpp "// Flannbasedmatcher actual in this header file # 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: Detect feature points using fast operator Fastfeaturedetector fast (;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: Extracting features (computational feature vectors) briefdescriptorextractor extractor using the brief operator; Mat Descriptors_1, Descriptors_2;extractor.compute (src_1, Keypoints_1, descriptors_1); Extractor.compute (src_2, Keypoints_2, descriptors_2)//--Step 3: Violent matching using Bruteforce method bruteforcematcher< l2<float> > matcher;// Flannbasedmatcher matcher;vector< dmatch > Matches;matcher.match (descriptors_1, descriptors_2, matches); cout << "Number of Matches:" <<matches.size () <<endl;//--Show matching results 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;}
The results of the operation are as follows:
Feature point detection and Sift/surf/brief feature extraction and matching of feature2d learning--fast in OpenCV