Merge pull request #1234 from SpecLad:ios-plist
[profile/ivi/opencv.git] / modules / features2d / doc / common_interfaces_of_descriptor_matchers.rst
1 Common Interfaces of Descriptor Matchers
2 ========================================
3
4 .. highlight:: cpp
5
6 Matchers of keypoint descriptors in OpenCV have wrappers with a common interface that enables you to easily switch
7 between different algorithms solving the same problem. This section is devoted to matching descriptors
8 that are represented as vectors in a multidimensional space. All objects that implement ``vector``
9 descriptor matchers inherit the
10 :ocv:class:`DescriptorMatcher` interface.
11
12 .. note::
13
14    * An example explaining keypoint matching can be found at opencv_source_code/samples/cpp/descriptor_extractor_matcher.cpp
15    * An example on descriptor matching evaluation can be found at opencv_source_code/samples/cpp/detector_descriptor_matcher_evaluation.cpp
16    * An example on one to many image matching can be found at opencv_source_code/samples/cpp/matching_to_many_images.cpp
17
18 DMatch
19 ------
20 .. ocv:struct:: DMatch
21
22 Class for matching keypoint descriptors: query descriptor index,
23 train descriptor index, train image index, and distance between descriptors. ::
24
25     struct DMatch
26     {
27         DMatch() : queryIdx(-1), trainIdx(-1), imgIdx(-1),
28                    distance(std::numeric_limits<float>::max()) {}
29         DMatch( int _queryIdx, int _trainIdx, float _distance ) :
30                 queryIdx(_queryIdx), trainIdx(_trainIdx), imgIdx(-1),
31                 distance(_distance) {}
32         DMatch( int _queryIdx, int _trainIdx, int _imgIdx, float _distance ) :
33                 queryIdx(_queryIdx), trainIdx(_trainIdx), imgIdx(_imgIdx),
34                 distance(_distance) {}
35
36         int queryIdx; // query descriptor index
37         int trainIdx; // train descriptor index
38         int imgIdx;   // train image index
39
40         float distance;
41
42         // less is better
43         bool operator<( const DMatch &m ) const;
44     };
45
46
47 DescriptorMatcher
48 -----------------
49 .. ocv:class:: DescriptorMatcher : public Algorithm
50
51 Abstract base class for matching keypoint descriptors. It has two groups
52 of match methods: for matching descriptors of an image with another image or
53 with an image set. ::
54
55     class DescriptorMatcher
56     {
57     public:
58         virtual ~DescriptorMatcher();
59
60         virtual void add( const vector<Mat>& descriptors );
61
62         const vector<Mat>& getTrainDescriptors() const;
63         virtual void clear();
64         bool empty() const;
65         virtual bool isMaskSupported() const = 0;
66
67         virtual void train();
68
69         /*
70          * Group of methods to match descriptors from an image pair.
71          */
72         void match( const Mat& queryDescriptors, const Mat& trainDescriptors,
73                     vector<DMatch>& matches, const Mat& mask=Mat() ) const;
74         void knnMatch( const Mat& queryDescriptors, const Mat& trainDescriptors,
75                        vector<vector<DMatch> >& matches, int k,
76                        const Mat& mask=Mat(), bool compactResult=false ) const;
77         void radiusMatch( const Mat& queryDescriptors, const Mat& trainDescriptors,
78                           vector<vector<DMatch> >& matches, float maxDistance,
79                           const Mat& mask=Mat(), bool compactResult=false ) const;
80         /*
81          * Group of methods to match descriptors from one image to an image set.
82          */
83         void match( const Mat& queryDescriptors, vector<DMatch>& matches,
84                     const vector<Mat>& masks=vector<Mat>() );
85         void knnMatch( const Mat& queryDescriptors, vector<vector<DMatch> >& matches,
86                        int k, const vector<Mat>& masks=vector<Mat>(),
87                        bool compactResult=false );
88         void radiusMatch( const Mat& queryDescriptors, vector<vector<DMatch> >& matches,
89                           float maxDistance, const vector<Mat>& masks=vector<Mat>(),
90                           bool compactResult=false );
91
92         virtual void read( const FileNode& );
93         virtual void write( FileStorage& ) const;
94
95         virtual Ptr<DescriptorMatcher> clone( bool emptyTrainData=false ) const = 0;
96
97         static Ptr<DescriptorMatcher> create( const string& descriptorMatcherType );
98
99     protected:
100         vector<Mat> trainDescCollection;
101         ...
102     };
103
104
105 DescriptorMatcher::add
106 --------------------------
107 Adds descriptors to train a descriptor collection. If the collection ``trainDescCollectionis`` is not empty, the new descriptors are added to existing train descriptors.
108
109 .. ocv:function:: void DescriptorMatcher::add( const vector<Mat>& descriptors )
110
111     :param descriptors: Descriptors to add. Each  ``descriptors[i]``  is a set of descriptors from the same train image.
112
113
114 DescriptorMatcher::getTrainDescriptors
115 ------------------------------------------
116 Returns a constant link to the train descriptor collection ``trainDescCollection`` .
117
118 .. ocv:function:: const vector<Mat>& DescriptorMatcher::getTrainDescriptors() const
119
120
121
122
123
124 DescriptorMatcher::clear
125 ----------------------------
126 Clears the train descriptor collection.
127
128 .. ocv:function:: void DescriptorMatcher::clear()
129
130
131
132 DescriptorMatcher::empty
133 ----------------------------
134 Returns true if there are no train descriptors in the collection.
135
136 .. ocv:function:: bool DescriptorMatcher::empty() const
137
138
139
140 DescriptorMatcher::isMaskSupported
141 --------------------------------------
142 Returns true if the descriptor matcher supports masking permissible matches.
143
144 .. ocv:function:: bool DescriptorMatcher::isMaskSupported()
145
146
147
148 DescriptorMatcher::train
149 ----------------------------
150 Trains a descriptor matcher
151
152 .. ocv:function:: void DescriptorMatcher::train()
153
154 Trains a descriptor matcher (for example, the flann index). In all methods to match, the method ``train()`` is run every time before matching. Some descriptor matchers (for example, ``BruteForceMatcher``) have an empty implementation of this method. Other matchers really train their inner structures (for example, ``FlannBasedMatcher`` trains ``flann::Index`` ).
155
156
157
158 DescriptorMatcher::match
159 ----------------------------
160 Finds the best match for each descriptor from a query set.
161
162 .. ocv:function:: void DescriptorMatcher::match( const Mat& queryDescriptors, const Mat& trainDescriptors, vector<DMatch>& matches, const Mat& mask=Mat() ) const
163
164 .. ocv:function:: void DescriptorMatcher::match( const Mat& queryDescriptors, vector<DMatch>& matches, const vector<Mat>& masks=vector<Mat>() )
165
166     :param queryDescriptors: Query set of descriptors.
167
168     :param trainDescriptors: Train set of descriptors. This set is not added to the train descriptors collection stored in the class object.
169
170     :param matches: Matches. If a query descriptor is masked out in  ``mask`` , no match is added for this descriptor. So, ``matches``  size may be smaller than the query descriptors count.
171
172     :param mask: Mask specifying permissible matches between an input query and train matrices of descriptors.
173
174     :param masks: Set of masks. Each  ``masks[i]``  specifies permissible matches between the input query descriptors and stored train descriptors from the i-th image ``trainDescCollection[i]``.
175
176 In the first variant of this method, the train descriptors are passed as an input argument. In the second variant of the method, train descriptors collection that was set by ``DescriptorMatcher::add`` is used. Optional mask (or masks) can be passed to specify which query and training descriptors can be matched. Namely, ``queryDescriptors[i]`` can be matched with ``trainDescriptors[j]`` only if ``mask.at<uchar>(i,j)`` is non-zero.
177
178
179
180 DescriptorMatcher::knnMatch
181 -------------------------------
182 Finds the k best matches for each descriptor from a query set.
183
184 .. ocv:function:: void DescriptorMatcher::knnMatch( const Mat& queryDescriptors,       const Mat& trainDescriptors,       vector<vector<DMatch> >& matches,       int k, const Mat& mask=Mat(),       bool compactResult=false ) const
185
186 .. ocv:function:: void DescriptorMatcher::knnMatch( const Mat& queryDescriptors,           vector<vector<DMatch> >& matches, int k,      const vector<Mat>& masks=vector<Mat>(),       bool compactResult=false )
187
188     :param queryDescriptors: Query set of descriptors.
189
190     :param trainDescriptors: Train set of descriptors. This set is not added to the train descriptors collection stored in the class object.
191
192     :param mask: Mask specifying permissible matches between an input query and train matrices of descriptors.
193
194     :param masks: Set of masks. Each  ``masks[i]``  specifies permissible matches between the input query descriptors and stored train descriptors from the i-th image ``trainDescCollection[i]``.
195
196     :param matches: Matches. Each  ``matches[i]``  is k or less matches for the same query descriptor.
197
198     :param k: Count of best matches found per each query descriptor or less if a query descriptor has less than k possible matches in total.
199
200     :param compactResult: Parameter used when the mask (or masks) is not empty. If  ``compactResult``  is false, the  ``matches``  vector has the same size as  ``queryDescriptors``  rows. If  ``compactResult``  is true, the  ``matches``  vector does not contain matches for fully masked-out query descriptors.
201
202 These extended variants of :ocv:func:`DescriptorMatcher::match` methods find several best matches for each query descriptor. The matches are returned in the distance increasing order. See :ocv:func:`DescriptorMatcher::match` for the details about query and train descriptors.
203
204
205
206 DescriptorMatcher::radiusMatch
207 ----------------------------------
208 For each query descriptor, finds the training descriptors not farther than the specified distance.
209
210 .. ocv:function:: void DescriptorMatcher::radiusMatch( const Mat& queryDescriptors,           const Mat& trainDescriptors,           vector<vector<DMatch> >& matches,           float maxDistance, const Mat& mask=Mat(),           bool compactResult=false ) const
211
212 .. ocv:function:: void DescriptorMatcher::radiusMatch( const Mat& queryDescriptors,           vector<vector<DMatch> >& matches,           float maxDistance,      const vector<Mat>& masks=vector<Mat>(),       bool compactResult=false )
213
214     :param queryDescriptors: Query set of descriptors.
215
216     :param trainDescriptors: Train set of descriptors. This set is not added to the train descriptors collection stored in the class object.
217
218     :param mask: Mask specifying permissible matches between an input query and train matrices of descriptors.
219
220     :param masks: Set of masks. Each  ``masks[i]``  specifies permissible matches between the input query descriptors and stored train descriptors from the i-th image ``trainDescCollection[i]``.
221
222     :param matches: Found matches.
223
224     :param compactResult: Parameter used when the mask (or masks) is not empty. If  ``compactResult``  is false, the  ``matches``  vector has the same size as  ``queryDescriptors``  rows. If  ``compactResult``  is true, the  ``matches``  vector does not contain matches for fully masked-out query descriptors.
225
226     :param maxDistance: Threshold for the distance between matched descriptors. Distance means here metric distance (e.g. Hamming distance), not the distance between coordinates (which is measured in Pixels)!
227
228 For each query descriptor, the methods find such training descriptors that the distance between the query descriptor and the training descriptor is equal or smaller than ``maxDistance``. Found matches are returned in the distance increasing order.
229
230
231
232 DescriptorMatcher::clone
233 ----------------------------
234 Clones the matcher.
235
236 .. ocv:function:: Ptr<DescriptorMatcher> DescriptorMatcher::clone( bool emptyTrainData=false )
237
238     :param emptyTrainData: If ``emptyTrainData`` is false, the method creates a deep copy of the object, that is, copies both parameters and train data. If ``emptyTrainData`` is true, the method creates an object copy with the current parameters but with empty train data.
239
240
241
242 DescriptorMatcher::create
243 -----------------------------
244 Creates a descriptor matcher of a given type with the default parameters (using default constructor).
245
246 .. ocv:function:: Ptr<DescriptorMatcher> DescriptorMatcher::create( const string& descriptorMatcherType )
247
248     :param descriptorMatcherType: Descriptor matcher type. Now the following matcher types are supported:
249
250         *
251             ``BruteForce`` (it uses ``L2`` )
252         *
253             ``BruteForce-L1``
254         *
255             ``BruteForce-Hamming``
256         *
257             ``BruteForce-Hamming(2)``
258         *
259             ``FlannBased``
260
261
262
263
264
265 BFMatcher
266 -----------------
267 .. ocv:class:: BFMatcher : public DescriptorMatcher
268
269 Brute-force descriptor matcher. For each descriptor in the first set, this matcher finds the closest descriptor in the second set by trying each one. This descriptor matcher supports masking permissible matches of descriptor sets.
270
271
272 BFMatcher::BFMatcher
273 --------------------
274 Brute-force matcher constructor.
275
276 .. ocv:function:: BFMatcher::BFMatcher( int normType=NORM_L2, bool crossCheck=false )
277
278     :param normType: One of ``NORM_L1``, ``NORM_L2``, ``NORM_HAMMING``, ``NORM_HAMMING2``. ``L1`` and ``L2`` norms are preferable choices for SIFT and SURF descriptors, ``NORM_HAMMING`` should be used with ORB, BRISK and BRIEF, ``NORM_HAMMING2`` should be used with ORB when ``WTA_K==3`` or ``4`` (see ORB::ORB constructor description).
279
280     :param crossCheck: If it is false, this is will be default BFMatcher behaviour when it finds the k nearest neighbors for each query descriptor. If ``crossCheck==true``, then the ``knnMatch()`` method with ``k=1`` will only return pairs ``(i,j)`` such that for ``i-th`` query descriptor the ``j-th`` descriptor in the matcher's collection is the nearest and vice versa, i.e. the ``BFMathcher`` will only return consistent pairs. Such technique usually produces best results with minimal number of outliers when there are enough matches. This is alternative to the ratio test, used by D. Lowe in SIFT paper.
281
282
283 FlannBasedMatcher
284 -----------------
285 .. ocv:class:: FlannBasedMatcher : public DescriptorMatcher
286
287 Flann-based descriptor matcher. This matcher trains :ocv:class:`flann::Index_` on a train descriptor collection and calls its nearest search methods to find the best matches. So, this matcher may be faster when matching a large train collection than the brute force matcher. ``FlannBasedMatcher`` does not support masking permissible matches of descriptor sets because ``flann::Index`` does not support this. ::
288
289     class FlannBasedMatcher : public DescriptorMatcher
290     {
291     public:
292         FlannBasedMatcher(
293           const Ptr<flann::IndexParams>& indexParams=new flann::KDTreeIndexParams(),
294           const Ptr<flann::SearchParams>& searchParams=new flann::SearchParams() );
295
296         virtual void add( const vector<Mat>& descriptors );
297         virtual void clear();
298
299         virtual void train();
300         virtual bool isMaskSupported() const;
301
302         virtual Ptr<DescriptorMatcher> clone( bool emptyTrainData=false ) const;
303     protected:
304         ...
305     };
306
307 ..