1 .. _feature_flann_matcher:
3 Feature Matching with FLANN
4 ****************************
9 In this tutorial you will learn how to:
11 .. container:: enumeratevisibleitemswithsquare
13 * Use the :flann_based_matcher:`FlannBasedMatcher<>` interface in order to perform a quick and efficient matching by using the :flann:`FLANN<>` ( *Fast Approximate Nearest Neighbor Search Library* )
22 This tutorial code's is shown lines below.
27 * @file SURF_FlannMatcher
28 * @brief SURF detector + descriptor + FLANN Matcher
36 #include "opencv2/core.hpp"
37 #include "opencv2/features2d.hpp"
38 #include "opencv2/imgcodecs.hpp"
39 #include "opencv2/highgui.hpp"
40 #include "opencv2/xfeatures2d.hpp"
44 using namespace cv::xfeatures2d;
50 * @brief Main function
52 int main( int argc, char** argv )
55 { readme(); return -1; }
57 Mat img_1 = imread( argv[1], IMREAD_GRAYSCALE );
58 Mat img_2 = imread( argv[2], IMREAD_GRAYSCALE );
60 if( !img_1.data || !img_2.data )
61 { std::cout<< " --(!) Error reading images " << std::endl; return -1; }
63 //-- Step 1: Detect the keypoints using SURF Detector
66 SurfFeatureDetector detector( minHessian );
68 std::vector<KeyPoint> keypoints_1, keypoints_2;
70 detector.detect( img_1, keypoints_1 );
71 detector.detect( img_2, keypoints_2 );
73 //-- Step 2: Calculate descriptors (feature vectors)
74 SurfDescriptorExtractor extractor;
76 Mat descriptors_1, descriptors_2;
78 extractor.compute( img_1, keypoints_1, descriptors_1 );
79 extractor.compute( img_2, keypoints_2, descriptors_2 );
81 //-- Step 3: Matching descriptor vectors using FLANN matcher
82 FlannBasedMatcher matcher;
83 std::vector< DMatch > matches;
84 matcher.match( descriptors_1, descriptors_2, matches );
86 double max_dist = 0; double min_dist = 100;
88 //-- Quick calculation of max and min distances between keypoints
89 for( int i = 0; i < descriptors_1.rows; i++ )
90 { double dist = matches[i].distance;
91 if( dist < min_dist ) min_dist = dist;
92 if( dist > max_dist ) max_dist = dist;
95 printf("-- Max dist : %f \n", max_dist );
96 printf("-- Min dist : %f \n", min_dist );
98 //-- Draw only "good" matches (i.e. whose distance is less than 2*min_dist,
99 //-- or a small arbitary value ( 0.02 ) in the event that min_dist is very
101 //-- PS.- radiusMatch can also be used here.
102 std::vector< DMatch > good_matches;
104 for( int i = 0; i < descriptors_1.rows; i++ )
105 { if( matches[i].distance <= max(2*min_dist, 0.02) )
106 { good_matches.push_back( matches[i]); }
109 //-- Draw only "good" matches
111 drawMatches( img_1, keypoints_1, img_2, keypoints_2,
112 good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
113 vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
115 //-- Show detected matches
116 imshow( "Good Matches", img_matches );
118 for( int i = 0; i < (int)good_matches.size(); i++ )
119 { printf( "-- Good Match [%d] Keypoint 1: %d -- Keypoint 2: %d \n", i, good_matches[i].queryIdx, good_matches[i].trainIdx ); }
130 { std::cout << " Usage: ./SURF_FlannMatcher <img1> <img2>" << std::endl; }
139 #. Here is the result of the feature detection applied to the first image:
141 .. image:: images/Featur_FlannMatcher_Result.jpg
145 #. Additionally, we get as console output the keypoints filtered:
147 .. image:: images/Feature_FlannMatcher_Keypoints_Result.jpg