moved nonfree and a part of features2d to opencv_contrib/xfeatures2d
[profile/ivi/opencv.git] / doc / tutorials / features2d / feature_flann_matcher / feature_flann_matcher.rst
1 .. _feature_flann_matcher:
2
3 Feature Matching with FLANN
4 ****************************
5
6 Goal
7 =====
8
9 In this tutorial you will learn how to:
10
11 .. container:: enumeratevisibleitemswithsquare
12
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* )
14
15
16 Theory
17 ======
18
19 Code
20 ====
21
22 This tutorial code's is shown lines below.
23
24 .. code-block:: cpp
25
26     /**
27      * @file SURF_FlannMatcher
28      * @brief SURF detector + descriptor + FLANN Matcher
29      * @author A. Huaman
30      */
31
32     #include <stdio.h>
33     #include <iostream>
34     #include <stdio.h>
35     #include <iostream>
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"
41
42     using namespace std;
43     using namespace cv;
44     using namespace cv::xfeatures2d;
45
46     void readme();
47
48     /**
49      * @function main
50      * @brief Main function
51      */
52     int main( int argc, char** argv )
53     {
54       if( argc != 3 )
55       { readme(); return -1; }
56
57       Mat img_1 = imread( argv[1], IMREAD_GRAYSCALE );
58       Mat img_2 = imread( argv[2], IMREAD_GRAYSCALE );
59
60       if( !img_1.data || !img_2.data )
61       { std::cout<< " --(!) Error reading images " << std::endl; return -1; }
62
63       //-- Step 1: Detect the keypoints using SURF Detector
64       int minHessian = 400;
65
66       SurfFeatureDetector detector( minHessian );
67
68       std::vector<KeyPoint> keypoints_1, keypoints_2;
69
70       detector.detect( img_1, keypoints_1 );
71       detector.detect( img_2, keypoints_2 );
72
73       //-- Step 2: Calculate descriptors (feature vectors)
74       SurfDescriptorExtractor extractor;
75
76       Mat descriptors_1, descriptors_2;
77
78       extractor.compute( img_1, keypoints_1, descriptors_1 );
79       extractor.compute( img_2, keypoints_2, descriptors_2 );
80
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 );
85
86       double max_dist = 0; double min_dist = 100;
87
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;
93       }
94
95       printf("-- Max dist : %f \n", max_dist );
96       printf("-- Min dist : %f \n", min_dist );
97
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
100       //-- small)
101       //-- PS.- radiusMatch can also be used here.
102       std::vector< DMatch > good_matches;
103
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]); }
107       }
108
109       //-- Draw only "good" matches
110       Mat img_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 );
114
115       //-- Show detected matches
116       imshow( "Good Matches", img_matches );
117
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 ); }
120
121       waitKey(0);
122
123       return 0;
124     }
125
126     /**
127      * @function readme
128      */
129     void readme()
130     { std::cout << " Usage: ./SURF_FlannMatcher <img1> <img2>" << std::endl; }
131
132
133 Explanation
134 ============
135
136 Result
137 ======
138
139 #. Here is the result of the feature detection applied to the first image:
140
141    .. image:: images/Featur_FlannMatcher_Result.jpg
142       :align: center
143       :height: 250pt
144
145 #. Additionally, we get as console output the keypoints filtered:
146
147    .. image:: images/Feature_FlannMatcher_Keypoints_Result.jpg
148       :align: center
149       :height: 250pt