Coverage Report

Created: 2026-02-14 07:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/install-coverage/include/opencv4/opencv2/features2d.hpp
Line
Count
Source
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
//  By downloading, copying, installing or using the software you agree to this license.
6
//  If you do not agree to this license, do not download, install,
7
//  copy or use the software.
8
//
9
//
10
//                           License Agreement
11
//                For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
16
//
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
19
//
20
//   * Redistribution's of source code must retain the above copyright notice,
21
//     this list of conditions and the following disclaimer.
22
//
23
//   * Redistribution's in binary form must reproduce the above copyright notice,
24
//     this list of conditions and the following disclaimer in the documentation
25
//     and/or other materials provided with the distribution.
26
//
27
//   * The name of the copyright holders may not be used to endorse or promote products
28
//     derived from this software without specific prior written permission.
29
//
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
40
//
41
//M*/
42
43
#ifndef OPENCV_FEATURES_2D_HPP
44
#define OPENCV_FEATURES_2D_HPP
45
46
#include "opencv2/opencv_modules.hpp"
47
#include "opencv2/core.hpp"
48
49
#ifdef HAVE_OPENCV_FLANN
50
#include "opencv2/flann/miniflann.hpp"
51
#endif
52
53
/**
54
  @defgroup features2d 2D Features Framework
55
  @{
56
    @defgroup features2d_main Feature Detection and Description
57
    @defgroup features2d_match Descriptor Matchers
58
59
    Matchers of keypoint descriptors in OpenCV have wrappers with a common interface that enables
60
    you to easily switch between different algorithms solving the same problem. This section is
61
    devoted to matching descriptors that are represented as vectors in a multidimensional space.
62
    All objects that implement vector descriptor matchers inherit the DescriptorMatcher interface.
63
64
    @defgroup features2d_draw Drawing Function of Keypoints and Matches
65
    @defgroup features2d_category Object Categorization
66
67
    This section describes approaches based on local 2D features and used to categorize objects.
68
69
    @defgroup feature2d_hal Hardware Acceleration Layer
70
    @{
71
        @defgroup features2d_hal_interface Interface
72
    @}
73
  @}
74
 */
75
76
namespace cv
77
{
78
79
//! @addtogroup features2d_main
80
//! @{
81
82
// //! writes vector of keypoints to the file storage
83
// CV_EXPORTS void write(FileStorage& fs, const String& name, const std::vector<KeyPoint>& keypoints);
84
// //! reads vector of keypoints from the specified file storage node
85
// CV_EXPORTS void read(const FileNode& node, CV_OUT std::vector<KeyPoint>& keypoints);
86
87
/** @brief A class filters a vector of keypoints.
88
89
 Because now it is difficult to provide a convenient interface for all usage scenarios of the
90
 keypoints filter class, it has only several needed by now static methods.
91
 */
92
class CV_EXPORTS KeyPointsFilter
93
{
94
public:
95
0
    KeyPointsFilter(){}
96
97
    /*
98
     * Remove keypoints within borderPixels of an image edge.
99
     */
100
    static void runByImageBorder( std::vector<KeyPoint>& keypoints, Size imageSize, int borderSize );
101
    /*
102
     * Remove keypoints of sizes out of range.
103
     */
104
    static void runByKeypointSize( std::vector<KeyPoint>& keypoints, float minSize,
105
                                   float maxSize=FLT_MAX );
106
    /*
107
     * Remove keypoints from some image by mask for pixels of this image.
108
     */
109
    static void runByPixelsMask( std::vector<KeyPoint>& keypoints, const Mat& mask );
110
    /*
111
     * Remove objects from some image and a vector of points by mask for pixels of this image
112
     */
113
    static void runByPixelsMask2VectorPoint(std::vector<KeyPoint> &keypoints, std::vector<std::vector<Point> > &removeFrom, const Mat &mask);
114
    /*
115
     * Remove duplicated keypoints.
116
     */
117
    static void removeDuplicated( std::vector<KeyPoint>& keypoints );
118
    /*
119
     * Remove duplicated keypoints and sort the remaining keypoints
120
     */
121
    static void removeDuplicatedSorted( std::vector<KeyPoint>& keypoints );
122
123
    /*
124
     * Retain the specified number of the best keypoints (according to the response)
125
     */
126
    static void retainBest( std::vector<KeyPoint>& keypoints, int npoints );
127
};
128
129
130
/************************************ Base Classes ************************************/
131
132
/** @brief Abstract base class for 2D image feature detectors and descriptor extractors
133
*/
134
#ifdef __EMSCRIPTEN__
135
class CV_EXPORTS_W Feature2D : public Algorithm
136
#else
137
class CV_EXPORTS_W Feature2D : public virtual Algorithm
138
#endif
139
{
140
public:
141
    virtual ~Feature2D();
142
143
    /** @brief Detects keypoints in an image (first variant) or image set (second variant).
144
145
    @param image Image.
146
    @param keypoints The detected keypoints. In the second variant of the method keypoints[i] is a set
147
    of keypoints detected in images[i] .
148
    @param mask Mask specifying where to look for keypoints (optional). It must be a 8-bit integer
149
    matrix with non-zero values in the region of interest.
150
     */
151
    CV_WRAP virtual void detect( InputArray image,
152
                                 CV_OUT std::vector<KeyPoint>& keypoints,
153
                                 InputArray mask=noArray() );
154
155
    /** @overload
156
    @param images Image set.
157
    @param keypoints The detected keypoints. In the second variant of the method keypoints[i] is a set
158
    of keypoints detected in images[i] .
159
    @param masks Masks for each input image specifying where to look for keypoints (optional).
160
    masks[i] is a mask for images[i].
161
    */
162
    CV_WRAP virtual void detect( InputArrayOfArrays images,
163
                         CV_OUT std::vector<std::vector<KeyPoint> >& keypoints,
164
                         InputArrayOfArrays masks=noArray() );
165
166
    /** @brief Computes the descriptors for a set of keypoints detected in an image (first variant) or image set
167
    (second variant).
168
169
    @param image Image.
170
    @param keypoints Input collection of keypoints. Keypoints for which a descriptor cannot be
171
    computed are removed. Sometimes new keypoints can be added, for example: SIFT duplicates keypoint
172
    with several dominant orientations (for each orientation).
173
    @param descriptors Computed descriptors. In the second variant of the method descriptors[i] are
174
    descriptors computed for a keypoints[i]. Row j is the keypoints (or keypoints[i]) is the
175
    descriptor for keypoint j-th keypoint.
176
     */
177
    CV_WRAP virtual void compute( InputArray image,
178
                                  CV_OUT CV_IN_OUT std::vector<KeyPoint>& keypoints,
179
                                  OutputArray descriptors );
180
181
    /** @overload
182
183
    @param images Image set.
184
    @param keypoints Input collection of keypoints. Keypoints for which a descriptor cannot be
185
    computed are removed. Sometimes new keypoints can be added, for example: SIFT duplicates keypoint
186
    with several dominant orientations (for each orientation).
187
    @param descriptors Computed descriptors. In the second variant of the method descriptors[i] are
188
    descriptors computed for a keypoints[i]. Row j is the keypoints (or keypoints[i]) is the
189
    descriptor for keypoint j-th keypoint.
190
    */
191
    CV_WRAP virtual void compute( InputArrayOfArrays images,
192
                          CV_OUT CV_IN_OUT std::vector<std::vector<KeyPoint> >& keypoints,
193
                          OutputArrayOfArrays descriptors );
194
195
    /** Detects keypoints and computes the descriptors */
196
    CV_WRAP virtual void detectAndCompute( InputArray image, InputArray mask,
197
                                           CV_OUT std::vector<KeyPoint>& keypoints,
198
                                           OutputArray descriptors,
199
                                           bool useProvidedKeypoints=false );
200
201
    CV_WRAP virtual int descriptorSize() const;
202
    CV_WRAP virtual int descriptorType() const;
203
    CV_WRAP virtual int defaultNorm() const;
204
205
    CV_WRAP void write( const String& fileName ) const;
206
207
    CV_WRAP void read( const String& fileName );
208
209
    virtual void write( FileStorage&) const CV_OVERRIDE;
210
211
    // see corresponding cv::Algorithm method
212
    CV_WRAP virtual void read( const FileNode&) CV_OVERRIDE;
213
214
    //! Return true if detector object is empty
215
    CV_WRAP virtual bool empty() const CV_OVERRIDE;
216
    CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
217
218
    // see corresponding cv::Algorithm method
219
0
    CV_WRAP inline void write(FileStorage& fs, const String& name) const { Algorithm::write(fs, name); }
220
#if CV_VERSION_MAJOR < 5
221
0
    inline void write(const Ptr<FileStorage>& fs, const String& name) const { CV_Assert(fs); Algorithm::write(*fs, name); }
222
#endif
223
};
224
225
/** Feature detectors in OpenCV have wrappers with a common interface that enables you to easily switch
226
between different algorithms solving the same problem. All objects that implement keypoint detectors
227
inherit the FeatureDetector interface. */
228
typedef Feature2D FeatureDetector;
229
230
/** Extractors of keypoint descriptors in OpenCV have wrappers with a common interface that enables you
231
to easily switch between different algorithms solving the same problem. This section is devoted to
232
computing descriptors represented as vectors in a multidimensional space. All objects that implement
233
the vector descriptor extractors inherit the DescriptorExtractor interface.
234
 */
235
typedef Feature2D DescriptorExtractor;
236
237
238
/** @brief Class for implementing the wrapper which makes detectors and extractors to be affine invariant,
239
described as ASIFT in @cite YM11 .
240
*/
241
class CV_EXPORTS_W AffineFeature : public Feature2D
242
{
243
public:
244
    /**
245
    @param backend The detector/extractor you want to use as backend.
246
    @param maxTilt The highest power index of tilt factor. 5 is used in the paper as tilt sampling range n.
247
    @param minTilt The lowest power index of tilt factor. 0 is used in the paper.
248
    @param tiltStep Tilt sampling step \f$\delta_t\f$ in Algorithm 1 in the paper.
249
    @param rotateStepBase Rotation sampling step factor b in Algorithm 1 in the paper.
250
    */
251
    CV_WRAP static Ptr<AffineFeature> create(const Ptr<Feature2D>& backend,
252
        int maxTilt = 5, int minTilt = 0, float tiltStep = 1.4142135623730951f, float rotateStepBase = 72);
253
254
    CV_WRAP virtual void setViewParams(const std::vector<float>& tilts, const std::vector<float>& rolls) = 0;
255
    CV_WRAP virtual void getViewParams(std::vector<float>& tilts, std::vector<float>& rolls) const = 0;
256
    CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
257
};
258
259
typedef AffineFeature AffineFeatureDetector;
260
typedef AffineFeature AffineDescriptorExtractor;
261
262
263
/** @brief Class for extracting keypoints and computing descriptors using the Scale Invariant Feature Transform
264
(SIFT) algorithm by D. Lowe @cite Lowe04 .
265
*/
266
class CV_EXPORTS_W SIFT : public Feature2D
267
{
268
public:
269
    /**
270
    @param nfeatures The number of best features to retain. The features are ranked by their scores
271
    (measured in SIFT algorithm as the local contrast)
272
273
    @param nOctaveLayers The number of layers in each octave. 3 is the value used in D. Lowe paper. The
274
    number of octaves is computed automatically from the image resolution.
275
276
    @param contrastThreshold The contrast threshold used to filter out weak features in semi-uniform
277
    (low-contrast) regions. The larger the threshold, the less features are produced by the detector.
278
279
    @note The contrast threshold will be divided by nOctaveLayers when the filtering is applied. When
280
    nOctaveLayers is set to default and if you want to use the value used in D. Lowe paper, 0.03, set
281
    this argument to 0.09.
282
283
    @param edgeThreshold The threshold used to filter out edge-like features. Note that the its meaning
284
    is different from the contrastThreshold, i.e. the larger the edgeThreshold, the less features are
285
    filtered out (more features are retained).
286
287
    @param sigma The sigma of the Gaussian applied to the input image at the octave \#0. If your image
288
    is captured with a weak camera with soft lenses, you might want to reduce the number.
289
290
    @param enable_precise_upscale Whether to enable precise upscaling in the scale pyramid, which maps
291
    index \f$\texttt{x}\f$ to \f$\texttt{2x}\f$. This prevents localization bias. The option
292
    is disabled by default.
293
    */
294
    CV_WRAP static Ptr<SIFT> create(int nfeatures = 0, int nOctaveLayers = 3,
295
        double contrastThreshold = 0.04, double edgeThreshold = 10,
296
        double sigma = 1.6, bool enable_precise_upscale = false);
297
298
    /** @brief Create SIFT with specified descriptorType.
299
    @param nfeatures The number of best features to retain. The features are ranked by their scores
300
    (measured in SIFT algorithm as the local contrast)
301
302
    @param nOctaveLayers The number of layers in each octave. 3 is the value used in D. Lowe paper. The
303
    number of octaves is computed automatically from the image resolution.
304
305
    @param contrastThreshold The contrast threshold used to filter out weak features in semi-uniform
306
    (low-contrast) regions. The larger the threshold, the less features are produced by the detector.
307
308
    @note The contrast threshold will be divided by nOctaveLayers when the filtering is applied. When
309
    nOctaveLayers is set to default and if you want to use the value used in D. Lowe paper, 0.03, set
310
    this argument to 0.09.
311
312
    @param edgeThreshold The threshold used to filter out edge-like features. Note that the its meaning
313
    is different from the contrastThreshold, i.e. the larger the edgeThreshold, the less features are
314
    filtered out (more features are retained).
315
316
    @param sigma The sigma of the Gaussian applied to the input image at the octave \#0. If your image
317
    is captured with a weak camera with soft lenses, you might want to reduce the number.
318
319
    @param descriptorType The type of descriptors. Only CV_32F and CV_8U are supported.
320
321
    @param enable_precise_upscale Whether to enable precise upscaling in the scale pyramid, which maps
322
    index \f$\texttt{x}\f$ to \f$\texttt{2x}\f$. This prevents localization bias. The option
323
    is disabled by default.
324
    */
325
    CV_WRAP static Ptr<SIFT> create(int nfeatures, int nOctaveLayers,
326
        double contrastThreshold, double edgeThreshold,
327
        double sigma, int descriptorType, bool enable_precise_upscale = false);
328
329
    CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
330
331
    CV_WRAP virtual void setNFeatures(int maxFeatures) = 0;
332
    CV_WRAP virtual int getNFeatures() const = 0;
333
334
    CV_WRAP virtual void setNOctaveLayers(int nOctaveLayers) = 0;
335
    CV_WRAP virtual int getNOctaveLayers() const = 0;
336
337
    CV_WRAP virtual void setContrastThreshold(double contrastThreshold) = 0;
338
    CV_WRAP virtual double getContrastThreshold() const = 0;
339
340
    CV_WRAP virtual void setEdgeThreshold(double edgeThreshold) = 0;
341
    CV_WRAP virtual double getEdgeThreshold() const = 0;
342
343
    CV_WRAP virtual void setSigma(double sigma) = 0;
344
    CV_WRAP virtual double getSigma() const = 0;
345
};
346
347
typedef SIFT SiftFeatureDetector;
348
typedef SIFT SiftDescriptorExtractor;
349
350
351
/** @brief Class implementing the BRISK keypoint detector and descriptor extractor, described in @cite LCS11 .
352
 */
353
class CV_EXPORTS_W BRISK : public Feature2D
354
{
355
public:
356
    /** @brief The BRISK constructor
357
358
    @param thresh AGAST detection threshold score.
359
    @param octaves detection octaves. Use 0 to do single scale.
360
    @param patternScale apply this scale to the pattern used for sampling the neighbourhood of a
361
    keypoint.
362
     */
363
    CV_WRAP static Ptr<BRISK> create(int thresh=30, int octaves=3, float patternScale=1.0f);
364
365
    /** @brief The BRISK constructor for a custom pattern
366
367
    @param radiusList defines the radii (in pixels) where the samples around a keypoint are taken (for
368
    keypoint scale 1).
369
    @param numberList defines the number of sampling points on the sampling circle. Must be the same
370
    size as radiusList..
371
    @param dMax threshold for the short pairings used for descriptor formation (in pixels for keypoint
372
    scale 1).
373
    @param dMin threshold for the long pairings used for orientation determination (in pixels for
374
    keypoint scale 1).
375
    @param indexChange index remapping of the bits. */
376
    CV_WRAP static Ptr<BRISK> create(const std::vector<float> &radiusList, const std::vector<int> &numberList,
377
        float dMax=5.85f, float dMin=8.2f, const std::vector<int>& indexChange=std::vector<int>());
378
379
    /** @brief The BRISK constructor for a custom pattern, detection threshold and octaves
380
381
    @param thresh AGAST detection threshold score.
382
    @param octaves detection octaves. Use 0 to do single scale.
383
    @param radiusList defines the radii (in pixels) where the samples around a keypoint are taken (for
384
    keypoint scale 1).
385
    @param numberList defines the number of sampling points on the sampling circle. Must be the same
386
    size as radiusList..
387
    @param dMax threshold for the short pairings used for descriptor formation (in pixels for keypoint
388
    scale 1).
389
    @param dMin threshold for the long pairings used for orientation determination (in pixels for
390
    keypoint scale 1).
391
    @param indexChange index remapping of the bits. */
392
    CV_WRAP static Ptr<BRISK> create(int thresh, int octaves, const std::vector<float> &radiusList,
393
        const std::vector<int> &numberList, float dMax=5.85f, float dMin=8.2f,
394
        const std::vector<int>& indexChange=std::vector<int>());
395
    CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
396
397
    /** @brief Set detection threshold.
398
    @param threshold AGAST detection threshold score.
399
    */
400
    CV_WRAP virtual void setThreshold(int threshold) = 0;
401
    CV_WRAP virtual int getThreshold() const = 0;
402
403
    /** @brief Set detection octaves.
404
    @param octaves detection octaves. Use 0 to do single scale.
405
    */
406
    CV_WRAP virtual void setOctaves(int octaves) = 0;
407
    CV_WRAP virtual int getOctaves() const = 0;
408
    /** @brief Set detection patternScale.
409
    @param patternScale apply this scale to the pattern used for sampling the neighbourhood of a
410
    keypoint.
411
    */
412
    CV_WRAP virtual void setPatternScale(float patternScale) = 0;
413
    CV_WRAP virtual float getPatternScale() const = 0;
414
};
415
416
/** @brief Class implementing the ORB (*oriented BRIEF*) keypoint detector and descriptor extractor
417
418
described in @cite RRKB11 . The algorithm uses FAST in pyramids to detect stable keypoints, selects
419
the strongest features using FAST or Harris response, finds their orientation using first-order
420
moments and computes the descriptors using BRIEF (where the coordinates of random point pairs (or
421
k-tuples) are rotated according to the measured orientation).
422
 */
423
class CV_EXPORTS_W ORB : public Feature2D
424
{
425
public:
426
    enum ScoreType { HARRIS_SCORE=0, FAST_SCORE=1 };
427
    static const int kBytes = 32;
428
429
    /** @brief The ORB constructor
430
431
    @param nfeatures The maximum number of features to retain.
432
    @param scaleFactor Pyramid decimation ratio, greater than 1. scaleFactor==2 means the classical
433
    pyramid, where each next level has 4x less pixels than the previous, but such a big scale factor
434
    will degrade feature matching scores dramatically. On the other hand, too close to 1 scale factor
435
    will mean that to cover certain scale range you will need more pyramid levels and so the speed
436
    will suffer.
437
    @param nlevels The number of pyramid levels. The smallest level will have linear size equal to
438
    input_image_linear_size/pow(scaleFactor, nlevels - firstLevel).
439
    @param edgeThreshold This is size of the border where the features are not detected. It should
440
    roughly match the patchSize parameter.
441
    @param firstLevel The level of pyramid to put source image to. Previous layers are filled
442
    with upscaled source image.
443
    @param WTA_K The number of points that produce each element of the oriented BRIEF descriptor. The
444
    default value 2 means the BRIEF where we take a random point pair and compare their brightnesses,
445
    so we get 0/1 response. Other possible values are 3 and 4. For example, 3 means that we take 3
446
    random points (of course, those point coordinates are random, but they are generated from the
447
    pre-defined seed, so each element of BRIEF descriptor is computed deterministically from the pixel
448
    rectangle), find point of maximum brightness and output index of the winner (0, 1 or 2). Such
449
    output will occupy 2 bits, and therefore it will need a special variant of Hamming distance,
450
    denoted as NORM_HAMMING2 (2 bits per bin). When WTA_K=4, we take 4 random points to compute each
451
    bin (that will also occupy 2 bits with possible values 0, 1, 2 or 3).
452
    @param scoreType The default HARRIS_SCORE means that Harris algorithm is used to rank features
453
    (the score is written to KeyPoint::score and is used to retain best nfeatures features);
454
    FAST_SCORE is alternative value of the parameter that produces slightly less stable keypoints,
455
    but it is a little faster to compute.
456
    @param patchSize size of the patch used by the oriented BRIEF descriptor. Of course, on smaller
457
    pyramid layers the perceived image area covered by a feature will be larger.
458
    @param fastThreshold the fast threshold
459
     */
460
    CV_WRAP static Ptr<ORB> create(int nfeatures=500, float scaleFactor=1.2f, int nlevels=8, int edgeThreshold=31,
461
        int firstLevel=0, int WTA_K=2, ORB::ScoreType scoreType=ORB::HARRIS_SCORE, int patchSize=31, int fastThreshold=20);
462
463
    CV_WRAP virtual void setMaxFeatures(int maxFeatures) = 0;
464
    CV_WRAP virtual int getMaxFeatures() const = 0;
465
466
    CV_WRAP virtual void setScaleFactor(double scaleFactor) = 0;
467
    CV_WRAP virtual double getScaleFactor() const = 0;
468
469
    CV_WRAP virtual void setNLevels(int nlevels) = 0;
470
    CV_WRAP virtual int getNLevels() const = 0;
471
472
    CV_WRAP virtual void setEdgeThreshold(int edgeThreshold) = 0;
473
    CV_WRAP virtual int getEdgeThreshold() const = 0;
474
475
    CV_WRAP virtual void setFirstLevel(int firstLevel) = 0;
476
    CV_WRAP virtual int getFirstLevel() const = 0;
477
478
    CV_WRAP virtual void setWTA_K(int wta_k) = 0;
479
    CV_WRAP virtual int getWTA_K() const = 0;
480
481
    CV_WRAP virtual void setScoreType(ORB::ScoreType scoreType) = 0;
482
    CV_WRAP virtual ORB::ScoreType getScoreType() const = 0;
483
484
    CV_WRAP virtual void setPatchSize(int patchSize) = 0;
485
    CV_WRAP virtual int getPatchSize() const = 0;
486
487
    CV_WRAP virtual void setFastThreshold(int fastThreshold) = 0;
488
    CV_WRAP virtual int getFastThreshold() const = 0;
489
    CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
490
};
491
492
/** @brief Maximally stable extremal region extractor
493
494
The class encapsulates all the parameters of the %MSER extraction algorithm (see [wiki
495
article](http://en.wikipedia.org/wiki/Maximally_stable_extremal_regions)).
496
497
- there are two different implementation of %MSER: one for grey image, one for color image
498
499
- the grey image algorithm is taken from: @cite nister2008linear ;  the paper claims to be faster
500
than union-find method; it actually get 1.5~2m/s on my centrino L7200 1.2GHz laptop.
501
502
- the color image algorithm is taken from: @cite forssen2007maximally ; it should be much slower
503
than grey image method ( 3~4 times )
504
505
- (Python) A complete example showing the use of the %MSER detector can be found at samples/python/mser.py
506
*/
507
class CV_EXPORTS_W MSER : public Feature2D
508
{
509
public:
510
    /** @brief Full constructor for %MSER detector
511
512
    @param delta it compares \f$(size_{i}-size_{i-delta})/size_{i-delta}\f$
513
    @param min_area prune the area which smaller than minArea
514
    @param max_area prune the area which bigger than maxArea
515
    @param max_variation prune the area have similar size to its children
516
    @param min_diversity for color image, trace back to cut off mser with diversity less than min_diversity
517
    @param max_evolution  for color image, the evolution steps
518
    @param area_threshold for color image, the area threshold to cause re-initialize
519
    @param min_margin for color image, ignore too small margin
520
    @param edge_blur_size for color image, the aperture size for edge blur
521
     */
522
    CV_WRAP static Ptr<MSER> create( int delta=5, int min_area=60, int max_area=14400,
523
          double max_variation=0.25, double min_diversity=.2,
524
          int max_evolution=200, double area_threshold=1.01,
525
          double min_margin=0.003, int edge_blur_size=5 );
526
527
    /** @brief Detect %MSER regions
528
529
    @param image input image (8UC1, 8UC3 or 8UC4, must be greater or equal than 3x3)
530
    @param msers resulting list of point sets
531
    @param bboxes resulting bounding boxes
532
    */
533
    CV_WRAP virtual void detectRegions( InputArray image,
534
                                        CV_OUT std::vector<std::vector<Point> >& msers,
535
                                        CV_OUT std::vector<Rect>& bboxes ) = 0;
536
537
    CV_WRAP virtual void setDelta(int delta) = 0;
538
    CV_WRAP virtual int getDelta() const = 0;
539
540
    CV_WRAP virtual void setMinArea(int minArea) = 0;
541
    CV_WRAP virtual int getMinArea() const = 0;
542
543
    CV_WRAP virtual void setMaxArea(int maxArea) = 0;
544
    CV_WRAP virtual int getMaxArea() const = 0;
545
546
    CV_WRAP virtual void setMaxVariation(double maxVariation) = 0;
547
    CV_WRAP virtual double getMaxVariation() const = 0;
548
549
    CV_WRAP virtual void setMinDiversity(double minDiversity) = 0;
550
    CV_WRAP virtual double getMinDiversity() const = 0;
551
552
    CV_WRAP virtual void setMaxEvolution(int maxEvolution) = 0;
553
    CV_WRAP virtual int getMaxEvolution() const = 0;
554
555
    CV_WRAP virtual void setAreaThreshold(double areaThreshold) = 0;
556
    CV_WRAP virtual double getAreaThreshold() const = 0;
557
558
    CV_WRAP virtual void setMinMargin(double min_margin) = 0;
559
    CV_WRAP virtual double getMinMargin() const = 0;
560
561
    CV_WRAP virtual void setEdgeBlurSize(int edge_blur_size) = 0;
562
    CV_WRAP virtual int getEdgeBlurSize() const = 0;
563
564
    CV_WRAP virtual void setPass2Only(bool f) = 0;
565
    CV_WRAP virtual bool getPass2Only() const = 0;
566
567
    CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
568
};
569
570
571
/** @brief Wrapping class for feature detection using the FAST method. :
572
 */
573
class CV_EXPORTS_W FastFeatureDetector : public Feature2D
574
{
575
public:
576
    enum DetectorType
577
    {
578
        TYPE_5_8 = 0, TYPE_7_12 = 1, TYPE_9_16 = 2
579
    };
580
    enum
581
    {
582
        THRESHOLD = 10000, NONMAX_SUPPRESSION=10001, FAST_N=10002
583
    };
584
585
586
    CV_WRAP static Ptr<FastFeatureDetector> create( int threshold=10,
587
                                                    bool nonmaxSuppression=true,
588
                                                    FastFeatureDetector::DetectorType type=FastFeatureDetector::TYPE_9_16 );
589
590
    CV_WRAP virtual void setThreshold(int threshold) = 0;
591
    CV_WRAP virtual int getThreshold() const = 0;
592
593
    CV_WRAP virtual void setNonmaxSuppression(bool f) = 0;
594
    CV_WRAP virtual bool getNonmaxSuppression() const = 0;
595
596
    CV_WRAP virtual void setType(FastFeatureDetector::DetectorType type) = 0;
597
    CV_WRAP virtual FastFeatureDetector::DetectorType getType() const = 0;
598
    CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
599
};
600
601
/** @overload */
602
CV_EXPORTS void FAST( InputArray image, CV_OUT std::vector<KeyPoint>& keypoints,
603
                      int threshold, bool nonmaxSuppression=true );
604
605
/** @brief Detects corners using the FAST algorithm
606
607
@param image grayscale image where keypoints (corners) are detected.
608
@param keypoints keypoints detected on the image.
609
@param threshold threshold on difference between intensity of the central pixel and pixels of a
610
circle around this pixel.
611
@param nonmaxSuppression if true, non-maximum suppression is applied to detected corners
612
(keypoints).
613
@param type one of the three neighborhoods as defined in the paper:
614
FastFeatureDetector::TYPE_9_16, FastFeatureDetector::TYPE_7_12,
615
FastFeatureDetector::TYPE_5_8
616
617
Detects corners using the FAST algorithm by @cite Rosten06 .
618
619
@note In Python API, types are given as cv.FAST_FEATURE_DETECTOR_TYPE_5_8,
620
cv.FAST_FEATURE_DETECTOR_TYPE_7_12 and cv.FAST_FEATURE_DETECTOR_TYPE_9_16. For corner
621
detection, use cv.FAST.detect() method.
622
 */
623
CV_EXPORTS void FAST( InputArray image, CV_OUT std::vector<KeyPoint>& keypoints,
624
                      int threshold, bool nonmaxSuppression, FastFeatureDetector::DetectorType type );
625
626
627
/** @brief Wrapping class for feature detection using the AGAST method. :
628
 */
629
class CV_EXPORTS_W AgastFeatureDetector : public Feature2D
630
{
631
public:
632
    enum DetectorType
633
    {
634
        AGAST_5_8 = 0, AGAST_7_12d = 1, AGAST_7_12s = 2, OAST_9_16 = 3,
635
    };
636
637
    enum
638
    {
639
        THRESHOLD = 10000, NONMAX_SUPPRESSION = 10001,
640
    };
641
642
    CV_WRAP static Ptr<AgastFeatureDetector> create( int threshold=10,
643
                                                     bool nonmaxSuppression=true,
644
                                                     AgastFeatureDetector::DetectorType type = AgastFeatureDetector::OAST_9_16);
645
646
    CV_WRAP virtual void setThreshold(int threshold) = 0;
647
    CV_WRAP virtual int getThreshold() const = 0;
648
649
    CV_WRAP virtual void setNonmaxSuppression(bool f) = 0;
650
    CV_WRAP virtual bool getNonmaxSuppression() const = 0;
651
652
    CV_WRAP virtual void setType(AgastFeatureDetector::DetectorType type) = 0;
653
    CV_WRAP virtual AgastFeatureDetector::DetectorType getType() const = 0;
654
    CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
655
};
656
657
/** @overload */
658
CV_EXPORTS void AGAST( InputArray image, CV_OUT std::vector<KeyPoint>& keypoints,
659
                      int threshold, bool nonmaxSuppression=true );
660
661
/** @brief Detects corners using the AGAST algorithm
662
663
@param image grayscale image where keypoints (corners) are detected.
664
@param keypoints keypoints detected on the image.
665
@param threshold threshold on difference between intensity of the central pixel and pixels of a
666
circle around this pixel.
667
@param nonmaxSuppression if true, non-maximum suppression is applied to detected corners
668
(keypoints).
669
@param type one of the four neighborhoods as defined in the paper:
670
AgastFeatureDetector::AGAST_5_8, AgastFeatureDetector::AGAST_7_12d,
671
AgastFeatureDetector::AGAST_7_12s, AgastFeatureDetector::OAST_9_16
672
673
For non-Intel platforms, there is a tree optimised variant of AGAST with same numerical results.
674
The 32-bit binary tree tables were generated automatically from original code using perl script.
675
The perl script and examples of tree generation are placed in features2d/doc folder.
676
Detects corners using the AGAST algorithm by @cite mair2010_agast .
677
678
 */
679
CV_EXPORTS void AGAST( InputArray image, CV_OUT std::vector<KeyPoint>& keypoints,
680
                      int threshold, bool nonmaxSuppression, AgastFeatureDetector::DetectorType type );
681
682
/** @brief Wrapping class for feature detection using the goodFeaturesToTrack function. :
683
 */
684
class CV_EXPORTS_W GFTTDetector : public Feature2D
685
{
686
public:
687
    CV_WRAP static Ptr<GFTTDetector> create( int maxCorners=1000, double qualityLevel=0.01, double minDistance=1,
688
                                             int blockSize=3, bool useHarrisDetector=false, double k=0.04 );
689
    CV_WRAP static Ptr<GFTTDetector> create( int maxCorners, double qualityLevel, double minDistance,
690
                                             int blockSize, int gradientSize, bool useHarrisDetector=false, double k=0.04 );
691
    CV_WRAP virtual void setMaxFeatures(int maxFeatures) = 0;
692
    CV_WRAP virtual int getMaxFeatures() const = 0;
693
694
    CV_WRAP virtual void setQualityLevel(double qlevel) = 0;
695
    CV_WRAP virtual double getQualityLevel() const = 0;
696
697
    CV_WRAP virtual void setMinDistance(double minDistance) = 0;
698
    CV_WRAP virtual double getMinDistance() const = 0;
699
700
    CV_WRAP virtual void setBlockSize(int blockSize) = 0;
701
    CV_WRAP virtual int getBlockSize() const = 0;
702
703
    CV_WRAP virtual void setGradientSize(int gradientSize_) = 0;
704
    CV_WRAP virtual int getGradientSize() = 0;
705
706
    CV_WRAP virtual void setHarrisDetector(bool val) = 0;
707
    CV_WRAP virtual bool getHarrisDetector() const = 0;
708
709
    CV_WRAP virtual void setK(double k) = 0;
710
    CV_WRAP virtual double getK() const = 0;
711
    CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
712
};
713
714
/** @brief Class for extracting blobs from an image. :
715
716
The class implements a simple algorithm for extracting blobs from an image:
717
718
1.  Convert the source image to binary images by applying thresholding with several thresholds from
719
    minThreshold (inclusive) to maxThreshold (exclusive) with distance thresholdStep between
720
    neighboring thresholds.
721
2.  Extract connected components from every binary image by findContours and calculate their
722
    centers.
723
3.  Group centers from several binary images by their coordinates. Close centers form one group that
724
    corresponds to one blob, which is controlled by the minDistBetweenBlobs parameter.
725
4.  From the groups, estimate final centers of blobs and their radiuses and return as locations and
726
    sizes of keypoints.
727
728
This class performs several filtrations of returned blobs. You should set filterBy\* to true/false
729
to turn on/off corresponding filtration. Available filtrations:
730
731
-   **By color**. This filter compares the intensity of a binary image at the center of a blob to
732
blobColor. If they differ, the blob is filtered out. Use blobColor = 0 to extract dark blobs
733
and blobColor = 255 to extract light blobs.
734
-   **By area**. Extracted blobs have an area between minArea (inclusive) and maxArea (exclusive).
735
-   **By circularity**. Extracted blobs have circularity
736
(\f$\frac{4*\pi*Area}{perimeter * perimeter}\f$) between minCircularity (inclusive) and
737
maxCircularity (exclusive).
738
-   **By ratio of the minimum inertia to maximum inertia**. Extracted blobs have this ratio
739
between minInertiaRatio (inclusive) and maxInertiaRatio (exclusive).
740
-   **By convexity**. Extracted blobs have convexity (area / area of blob convex hull) between
741
minConvexity (inclusive) and maxConvexity (exclusive).
742
743
Default values of parameters are tuned to extract dark circular blobs.
744
 */
745
class CV_EXPORTS_W SimpleBlobDetector : public Feature2D
746
{
747
public:
748
  struct CV_EXPORTS_W_SIMPLE Params
749
  {
750
      CV_WRAP Params();
751
      CV_PROP_RW float thresholdStep;
752
      CV_PROP_RW float minThreshold;
753
      CV_PROP_RW float maxThreshold;
754
      CV_PROP_RW size_t minRepeatability;
755
      CV_PROP_RW float minDistBetweenBlobs;
756
757
      CV_PROP_RW bool filterByColor;
758
      CV_PROP_RW uchar blobColor;
759
760
      CV_PROP_RW bool filterByArea;
761
      CV_PROP_RW float minArea, maxArea;
762
763
      CV_PROP_RW bool filterByCircularity;
764
      CV_PROP_RW float minCircularity, maxCircularity;
765
766
      CV_PROP_RW bool filterByInertia;
767
      CV_PROP_RW float minInertiaRatio, maxInertiaRatio;
768
769
      CV_PROP_RW bool filterByConvexity;
770
      CV_PROP_RW float minConvexity, maxConvexity;
771
772
      /** @brief Flag to enable contour collection.
773
      If set to true, the detector will store the contours of the detected blobs in memory,
774
      which can be retrieved after the detect() call using getBlobContours().
775
      @note Default value is false.
776
      */
777
      CV_PROP_RW bool collectContours;
778
779
      void read( const FileNode& fn );
780
      void write( FileStorage& fs ) const;
781
  };
782
783
  CV_WRAP static Ptr<SimpleBlobDetector>
784
    create(const SimpleBlobDetector::Params &parameters = SimpleBlobDetector::Params());
785
786
  CV_WRAP virtual void setParams(const SimpleBlobDetector::Params& params ) = 0;
787
  CV_WRAP virtual SimpleBlobDetector::Params getParams() const = 0;
788
789
  CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
790
791
  /** @brief Returns the contours of the blobs detected during the last call to detect().
792
  @note The @ref Params::collectContours parameter must be set to true before calling
793
  detect() for this method to return any data.
794
  */
795
  CV_WRAP virtual const std::vector<std::vector<cv::Point> >& getBlobContours() const;
796
};
797
798
/** @brief Class implementing the KAZE keypoint detector and descriptor extractor, described in @cite ABD12 .
799
800
@note AKAZE descriptor can only be used with KAZE or AKAZE keypoints .. [ABD12] KAZE Features. Pablo
801
F. Alcantarilla, Adrien Bartoli and Andrew J. Davison. In European Conference on Computer Vision
802
(ECCV), Fiorenze, Italy, October 2012.
803
*/
804
class CV_EXPORTS_W KAZE : public Feature2D
805
{
806
public:
807
    enum DiffusivityType
808
    {
809
        DIFF_PM_G1 = 0,
810
        DIFF_PM_G2 = 1,
811
        DIFF_WEICKERT = 2,
812
        DIFF_CHARBONNIER = 3
813
    };
814
815
    /** @brief The KAZE constructor
816
817
    @param extended Set to enable extraction of extended (128-byte) descriptor.
818
    @param upright Set to enable use of upright descriptors (non rotation-invariant).
819
    @param threshold Detector response threshold to accept point
820
    @param nOctaves Maximum octave evolution of the image
821
    @param nOctaveLayers Default number of sublevels per scale level
822
    @param diffusivity Diffusivity type. DIFF_PM_G1, DIFF_PM_G2, DIFF_WEICKERT or
823
    DIFF_CHARBONNIER
824
     */
825
    CV_WRAP static Ptr<KAZE> create(bool extended=false, bool upright=false,
826
                                    float threshold = 0.001f,
827
                                    int nOctaves = 4, int nOctaveLayers = 4,
828
                                    KAZE::DiffusivityType diffusivity = KAZE::DIFF_PM_G2);
829
830
    CV_WRAP virtual void setExtended(bool extended) = 0;
831
    CV_WRAP virtual bool getExtended() const = 0;
832
833
    CV_WRAP virtual void setUpright(bool upright) = 0;
834
    CV_WRAP virtual bool getUpright() const = 0;
835
836
    CV_WRAP virtual void setThreshold(double threshold) = 0;
837
    CV_WRAP virtual double getThreshold() const = 0;
838
839
    CV_WRAP virtual void setNOctaves(int octaves) = 0;
840
    CV_WRAP virtual int getNOctaves() const = 0;
841
842
    CV_WRAP virtual void setNOctaveLayers(int octaveLayers) = 0;
843
    CV_WRAP virtual int getNOctaveLayers() const = 0;
844
845
    CV_WRAP virtual void setDiffusivity(KAZE::DiffusivityType diff) = 0;
846
    CV_WRAP virtual KAZE::DiffusivityType getDiffusivity() const = 0;
847
    CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
848
};
849
850
/** @brief Class implementing the AKAZE keypoint detector and descriptor extractor, described in @cite ANB13.
851
852
@details AKAZE descriptors can only be used with KAZE or AKAZE keypoints. This class is thread-safe.
853
854
@note When you need descriptors use Feature2D::detectAndCompute, which
855
provides better performance. When using Feature2D::detect followed by
856
Feature2D::compute scale space pyramid is computed twice.
857
858
@note AKAZE implements T-API. When image is passed as UMat some parts of the algorithm
859
will use OpenCL.
860
861
@note [ANB13] Fast Explicit Diffusion for Accelerated Features in Nonlinear
862
Scale Spaces. Pablo F. Alcantarilla, Jesús Nuevo and Adrien Bartoli. In
863
British Machine Vision Conference (BMVC), Bristol, UK, September 2013.
864
865
*/
866
class CV_EXPORTS_W AKAZE : public Feature2D
867
{
868
public:
869
    // AKAZE descriptor type
870
    enum DescriptorType
871
    {
872
        DESCRIPTOR_KAZE_UPRIGHT = 2, ///< Upright descriptors, not invariant to rotation
873
        DESCRIPTOR_KAZE = 3,
874
        DESCRIPTOR_MLDB_UPRIGHT = 4, ///< Upright descriptors, not invariant to rotation
875
        DESCRIPTOR_MLDB = 5
876
    };
877
878
    /** @brief The AKAZE constructor
879
880
    @param descriptor_type Type of the extracted descriptor: DESCRIPTOR_KAZE,
881
    DESCRIPTOR_KAZE_UPRIGHT, DESCRIPTOR_MLDB or DESCRIPTOR_MLDB_UPRIGHT.
882
    @param descriptor_size Size of the descriptor in bits. 0 -\> Full size
883
    @param descriptor_channels Number of channels in the descriptor (1, 2, 3)
884
    @param threshold Detector response threshold to accept point
885
    @param nOctaves Maximum octave evolution of the image
886
    @param nOctaveLayers Default number of sublevels per scale level
887
    @param diffusivity Diffusivity type. DIFF_PM_G1, DIFF_PM_G2, DIFF_WEICKERT or
888
    DIFF_CHARBONNIER
889
    @param max_points Maximum amount of returned points. In case if image contains
890
    more features, then the features with highest response are returned.
891
    Negative value means no limitation.
892
     */
893
    CV_WRAP static Ptr<AKAZE> create(AKAZE::DescriptorType descriptor_type = AKAZE::DESCRIPTOR_MLDB,
894
                                     int descriptor_size = 0, int descriptor_channels = 3,
895
                                     float threshold = 0.001f, int nOctaves = 4,
896
                                     int nOctaveLayers = 4, KAZE::DiffusivityType diffusivity = KAZE::DIFF_PM_G2,
897
                                     int max_points = -1);
898
899
    CV_WRAP virtual void setDescriptorType(AKAZE::DescriptorType dtype) = 0;
900
    CV_WRAP virtual AKAZE::DescriptorType getDescriptorType() const = 0;
901
902
    CV_WRAP virtual void setDescriptorSize(int dsize) = 0;
903
    CV_WRAP virtual int getDescriptorSize() const = 0;
904
905
    CV_WRAP virtual void setDescriptorChannels(int dch) = 0;
906
    CV_WRAP virtual int getDescriptorChannels() const = 0;
907
908
    CV_WRAP virtual void setThreshold(double threshold) = 0;
909
    CV_WRAP virtual double getThreshold() const = 0;
910
911
    CV_WRAP virtual void setNOctaves(int octaves) = 0;
912
    CV_WRAP virtual int getNOctaves() const = 0;
913
914
    CV_WRAP virtual void setNOctaveLayers(int octaveLayers) = 0;
915
    CV_WRAP virtual int getNOctaveLayers() const = 0;
916
917
    CV_WRAP virtual void setDiffusivity(KAZE::DiffusivityType diff) = 0;
918
    CV_WRAP virtual KAZE::DiffusivityType getDiffusivity() const = 0;
919
    CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
920
921
    CV_WRAP virtual void setMaxPoints(int max_points) = 0;
922
    CV_WRAP virtual int getMaxPoints() const = 0;
923
};
924
925
926
/****************************************************************************************\
927
*                                      Distance                                          *
928
\****************************************************************************************/
929
930
template<typename T>
931
struct CV_EXPORTS Accumulator
932
{
933
    typedef T Type;
934
};
935
936
template<> struct Accumulator<unsigned char>  { typedef float Type; };
937
template<> struct Accumulator<unsigned short> { typedef float Type; };
938
template<> struct Accumulator<char>   { typedef float Type; };
939
template<> struct Accumulator<short>  { typedef float Type; };
940
941
/*
942
 * Squared Euclidean distance functor
943
 */
944
template<class T>
945
struct CV_EXPORTS SL2
946
{
947
    static const NormTypes normType = NORM_L2SQR;
948
    typedef T ValueType;
949
    typedef typename Accumulator<T>::Type ResultType;
950
951
    ResultType operator()( const T* a, const T* b, int size ) const
952
    {
953
        return normL2Sqr<ValueType, ResultType>(a, b, size);
954
    }
955
};
956
957
/*
958
 * Euclidean distance functor
959
 */
960
template<class T>
961
struct L2
962
{
963
    static const NormTypes normType = NORM_L2;
964
    typedef T ValueType;
965
    typedef typename Accumulator<T>::Type ResultType;
966
967
    ResultType operator()( const T* a, const T* b, int size ) const
968
    {
969
        return (ResultType)std::sqrt((double)normL2Sqr<ValueType, ResultType>(a, b, size));
970
    }
971
};
972
973
/*
974
 * Manhattan distance (city block distance) functor
975
 */
976
template<class T>
977
struct L1
978
{
979
    static const NormTypes normType = NORM_L1;
980
    typedef T ValueType;
981
    typedef typename Accumulator<T>::Type ResultType;
982
983
    ResultType operator()( const T* a, const T* b, int size ) const
984
    {
985
        return normL1<ValueType, ResultType>(a, b, size);
986
    }
987
};
988
989
//! @} features2d_main
990
991
/****************************************************************************************\
992
*                                  DescriptorMatcher                                     *
993
\****************************************************************************************/
994
995
//! @addtogroup features2d_match
996
//! @{
997
998
/** @brief Abstract base class for matching keypoint descriptors.
999
1000
It has two groups of match methods: for matching descriptors of an image with another image or with
1001
an image set.
1002
 */
1003
class CV_EXPORTS_W DescriptorMatcher : public Algorithm
1004
{
1005
public:
1006
   enum MatcherType
1007
    {
1008
        FLANNBASED            = 1,
1009
        BRUTEFORCE            = 2,
1010
        BRUTEFORCE_L1         = 3,
1011
        BRUTEFORCE_HAMMING    = 4,
1012
        BRUTEFORCE_HAMMINGLUT = 5,
1013
        BRUTEFORCE_SL2        = 6
1014
    };
1015
1016
    virtual ~DescriptorMatcher();
1017
1018
    /** @brief Adds descriptors to train a CPU(trainDescCollectionis) or GPU(utrainDescCollectionis) descriptor
1019
    collection.
1020
1021
    If the collection is not empty, the new descriptors are added to existing train descriptors.
1022
1023
    @param descriptors Descriptors to add. Each descriptors[i] is a set of descriptors from the same
1024
    train image.
1025
     */
1026
    CV_WRAP virtual void add( InputArrayOfArrays descriptors );
1027
1028
    /** @brief Returns a constant link to the train descriptor collection trainDescCollection .
1029
     */
1030
    CV_WRAP const std::vector<Mat>& getTrainDescriptors() const;
1031
1032
    /** @brief Clears the train descriptor collections.
1033
     */
1034
    CV_WRAP virtual void clear() CV_OVERRIDE;
1035
1036
    /** @brief Returns true if there are no train descriptors in the both collections.
1037
     */
1038
    CV_WRAP virtual bool empty() const CV_OVERRIDE;
1039
1040
    /** @brief Returns true if the descriptor matcher supports masking permissible matches.
1041
     */
1042
    CV_WRAP virtual bool isMaskSupported() const = 0;
1043
1044
    /** @brief Trains a descriptor matcher
1045
1046
    Trains a descriptor matcher (for example, the flann index). In all methods to match, the method
1047
    train() is run every time before matching. Some descriptor matchers (for example, BruteForceMatcher)
1048
    have an empty implementation of this method. Other matchers really train their inner structures (for
1049
    example, FlannBasedMatcher trains flann::Index ).
1050
     */
1051
    CV_WRAP virtual void train();
1052
1053
    /** @brief Finds the best match for each descriptor from a query set.
1054
1055
    @param queryDescriptors Query set of descriptors.
1056
    @param trainDescriptors Train set of descriptors. This set is not added to the train descriptors
1057
    collection stored in the class object.
1058
    @param matches Matches. If a query descriptor is masked out in mask , no match is added for this
1059
    descriptor. So, matches size may be smaller than the query descriptors count.
1060
    @param mask Mask specifying permissible matches between an input query and train matrices of
1061
    descriptors.
1062
1063
    In the first variant of this method, the train descriptors are passed as an input argument. In the
1064
    second variant of the method, train descriptors collection that was set by DescriptorMatcher::add is
1065
    used. Optional mask (or masks) can be passed to specify which query and training descriptors can be
1066
    matched. Namely, queryDescriptors[i] can be matched with trainDescriptors[j] only if
1067
    mask.at\<uchar\>(i,j) is non-zero.
1068
     */
1069
    CV_WRAP void match( InputArray queryDescriptors, InputArray trainDescriptors,
1070
                CV_OUT std::vector<DMatch>& matches, InputArray mask=noArray() ) const;
1071
1072
    /** @brief Finds the k best matches for each descriptor from a query set.
1073
1074
    @param queryDescriptors Query set of descriptors.
1075
    @param trainDescriptors Train set of descriptors. This set is not added to the train descriptors
1076
    collection stored in the class object.
1077
    @param mask Mask specifying permissible matches between an input query and train matrices of
1078
    descriptors.
1079
    @param matches Matches. Each matches[i] is k or less matches for the same query descriptor.
1080
    @param k Count of best matches found per each query descriptor or less if a query descriptor has
1081
    less than k possible matches in total.
1082
    @param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
1083
    false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
1084
    the matches vector does not contain matches for fully masked-out query descriptors.
1085
1086
    These extended variants of DescriptorMatcher::match methods find several best matches for each query
1087
    descriptor. The matches are returned in the distance increasing order. See DescriptorMatcher::match
1088
    for the details about query and train descriptors.
1089
     */
1090
    CV_WRAP void knnMatch( InputArray queryDescriptors, InputArray trainDescriptors,
1091
                   CV_OUT std::vector<std::vector<DMatch> >& matches, int k,
1092
                   InputArray mask=noArray(), bool compactResult=false ) const;
1093
1094
    /** @brief For each query descriptor, finds the training descriptors not farther than the specified distance.
1095
1096
    @param queryDescriptors Query set of descriptors.
1097
    @param trainDescriptors Train set of descriptors. This set is not added to the train descriptors
1098
    collection stored in the class object.
1099
    @param matches Found matches.
1100
    @param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
1101
    false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
1102
    the matches vector does not contain matches for fully masked-out query descriptors.
1103
    @param maxDistance Threshold for the distance between matched descriptors. Distance means here
1104
    metric distance (e.g. Hamming distance), not the distance between coordinates (which is measured
1105
    in Pixels)!
1106
    @param mask Mask specifying permissible matches between an input query and train matrices of
1107
    descriptors.
1108
1109
    For each query descriptor, the methods find such training descriptors that the distance between the
1110
    query descriptor and the training descriptor is equal or smaller than maxDistance. Found matches are
1111
    returned in the distance increasing order.
1112
     */
1113
    CV_WRAP void radiusMatch( InputArray queryDescriptors, InputArray trainDescriptors,
1114
                      CV_OUT std::vector<std::vector<DMatch> >& matches, float maxDistance,
1115
                      InputArray mask=noArray(), bool compactResult=false ) const;
1116
1117
    /** @overload
1118
    @param queryDescriptors Query set of descriptors.
1119
    @param matches Matches. If a query descriptor is masked out in mask , no match is added for this
1120
    descriptor. So, matches size may be smaller than the query descriptors count.
1121
    @param masks Set of masks. Each masks[i] specifies permissible matches between the input query
1122
    descriptors and stored train descriptors from the i-th image trainDescCollection[i].
1123
    */
1124
    CV_WRAP void match( InputArray queryDescriptors, CV_OUT std::vector<DMatch>& matches,
1125
                        InputArrayOfArrays masks=noArray() );
1126
    /** @overload
1127
    @param queryDescriptors Query set of descriptors.
1128
    @param matches Matches. Each matches[i] is k or less matches for the same query descriptor.
1129
    @param k Count of best matches found per each query descriptor or less if a query descriptor has
1130
    less than k possible matches in total.
1131
    @param masks Set of masks. Each masks[i] specifies permissible matches between the input query
1132
    descriptors and stored train descriptors from the i-th image trainDescCollection[i].
1133
    @param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
1134
    false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
1135
    the matches vector does not contain matches for fully masked-out query descriptors.
1136
    */
1137
    CV_WRAP void knnMatch( InputArray queryDescriptors, CV_OUT std::vector<std::vector<DMatch> >& matches, int k,
1138
                           InputArrayOfArrays masks=noArray(), bool compactResult=false );
1139
    /** @overload
1140
    @param queryDescriptors Query set of descriptors.
1141
    @param matches Found matches.
1142
    @param maxDistance Threshold for the distance between matched descriptors. Distance means here
1143
    metric distance (e.g. Hamming distance), not the distance between coordinates (which is measured
1144
    in Pixels)!
1145
    @param masks Set of masks. Each masks[i] specifies permissible matches between the input query
1146
    descriptors and stored train descriptors from the i-th image trainDescCollection[i].
1147
    @param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
1148
    false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
1149
    the matches vector does not contain matches for fully masked-out query descriptors.
1150
    */
1151
    CV_WRAP void radiusMatch( InputArray queryDescriptors, CV_OUT std::vector<std::vector<DMatch> >& matches, float maxDistance,
1152
                      InputArrayOfArrays masks=noArray(), bool compactResult=false );
1153
1154
1155
    CV_WRAP void write( const String& fileName ) const
1156
0
    {
1157
0
        FileStorage fs(fileName, FileStorage::WRITE);
1158
0
        write(fs);
1159
0
    }
1160
1161
    CV_WRAP void read( const String& fileName )
1162
0
    {
1163
0
        FileStorage fs(fileName, FileStorage::READ);
1164
0
        read(fs.root());
1165
0
    }
1166
    // Reads matcher object from a file node
1167
    // see corresponding cv::Algorithm method
1168
    CV_WRAP virtual void read( const FileNode& ) CV_OVERRIDE;
1169
    // Writes matcher object to a file storage
1170
    virtual void write( FileStorage& ) const CV_OVERRIDE;
1171
1172
    /** @brief Clones the matcher.
1173
1174
    @param emptyTrainData If emptyTrainData is false, the method creates a deep copy of the object,
1175
    that is, copies both parameters and train data. If emptyTrainData is true, the method creates an
1176
    object copy with the current parameters but with empty train data.
1177
     */
1178
    CV_WRAP CV_NODISCARD_STD virtual Ptr<DescriptorMatcher> clone( bool emptyTrainData=false ) const = 0;
1179
1180
    /** @brief Creates a descriptor matcher of a given type with the default parameters (using default
1181
    constructor).
1182
1183
    @param descriptorMatcherType Descriptor matcher type. Now the following matcher types are
1184
    supported:
1185
    -   `BruteForce` (it uses L2 )
1186
    -   `BruteForce-L1`
1187
    -   `BruteForce-Hamming`
1188
    -   `BruteForce-Hamming(2)`
1189
    -   `FlannBased`
1190
     */
1191
    CV_WRAP static Ptr<DescriptorMatcher> create( const String& descriptorMatcherType );
1192
1193
    CV_WRAP static Ptr<DescriptorMatcher> create( const DescriptorMatcher::MatcherType& matcherType );
1194
1195
1196
    // see corresponding cv::Algorithm method
1197
0
    CV_WRAP inline void write(FileStorage& fs, const String& name) const { Algorithm::write(fs, name); }
1198
#if CV_VERSION_MAJOR < 5
1199
0
    inline void write(const Ptr<FileStorage>& fs, const String& name) const { CV_Assert(fs); Algorithm::write(*fs, name); }
1200
#endif
1201
1202
protected:
1203
    /**
1204
     * Class to work with descriptors from several images as with one merged matrix.
1205
     * It is used e.g. in FlannBasedMatcher.
1206
     */
1207
    class CV_EXPORTS DescriptorCollection
1208
    {
1209
    public:
1210
        DescriptorCollection();
1211
        DescriptorCollection( const DescriptorCollection& collection );
1212
        virtual ~DescriptorCollection();
1213
1214
        // Vector of matrices "descriptors" will be merged to one matrix "mergedDescriptors" here.
1215
        void set( const std::vector<Mat>& descriptors );
1216
        virtual void clear();
1217
1218
        const Mat& getDescriptors() const;
1219
        Mat getDescriptor( int imgIdx, int localDescIdx ) const;
1220
        Mat getDescriptor( int globalDescIdx ) const;
1221
        void getLocalIdx( int globalDescIdx, int& imgIdx, int& localDescIdx ) const;
1222
1223
        int size() const;
1224
1225
    protected:
1226
        Mat mergedDescriptors;
1227
        std::vector<int> startIdxs;
1228
    };
1229
1230
    //! In fact the matching is implemented only by the following two methods. These methods suppose
1231
    //! that the class object has been trained already. Public match methods call these methods
1232
    //! after calling train().
1233
    virtual void knnMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, int k,
1234
        InputArrayOfArrays masks=noArray(), bool compactResult=false ) = 0;
1235
    virtual void radiusMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance,
1236
        InputArrayOfArrays masks=noArray(), bool compactResult=false ) = 0;
1237
1238
    static bool isPossibleMatch( InputArray mask, int queryIdx, int trainIdx );
1239
    static bool isMaskedOut( InputArrayOfArrays masks, int queryIdx );
1240
1241
0
    CV_NODISCARD_STD static Mat clone_op( Mat m ) { return m.clone(); }
1242
    void checkMasks( InputArrayOfArrays masks, int queryDescriptorsCount ) const;
1243
1244
    //! Collection of descriptors from train images.
1245
    std::vector<Mat> trainDescCollection;
1246
    std::vector<UMat> utrainDescCollection;
1247
};
1248
1249
/** @brief Brute-force descriptor matcher.
1250
1251
For each descriptor in the first set, this matcher finds the closest descriptor in the second set
1252
by trying each one. This descriptor matcher supports masking permissible matches of descriptor
1253
sets.
1254
 */
1255
class CV_EXPORTS_W BFMatcher : public DescriptorMatcher
1256
{
1257
public:
1258
    /** @brief Brute-force matcher constructor (obsolete). Please use BFMatcher.create()
1259
     *
1260
     *
1261
    */
1262
    CV_WRAP BFMatcher( int normType=NORM_L2, bool crossCheck=false );
1263
1264
0
    virtual ~BFMatcher() {}
1265
1266
0
    virtual bool isMaskSupported() const CV_OVERRIDE { return true; }
1267
1268
    /** @brief Brute-force matcher create method.
1269
    @param normType One of NORM_L1, NORM_L2, NORM_HAMMING, NORM_HAMMING2. L1 and L2 norms are
1270
    preferable choices for SIFT and SURF descriptors, NORM_HAMMING should be used with ORB, BRISK and
1271
    BRIEF, NORM_HAMMING2 should be used with ORB when WTA_K==3 or 4 (see ORB::ORB constructor
1272
    description).
1273
    @param crossCheck If it is false, this is will be default BFMatcher behaviour when it finds the k
1274
    nearest neighbors for each query descriptor. If crossCheck==true, then the knnMatch() method with
1275
    k=1 will only return pairs (i,j) such that for i-th query descriptor the j-th descriptor in the
1276
    matcher's collection is the nearest and vice versa, i.e. the BFMatcher will only return consistent
1277
    pairs. Such technique usually produces best results with minimal number of outliers when there are
1278
    enough matches. This is alternative to the ratio test, used by D. Lowe in SIFT paper.
1279
     */
1280
    CV_WRAP static Ptr<BFMatcher> create( int normType=NORM_L2, bool crossCheck=false ) ;
1281
1282
    CV_NODISCARD_STD virtual Ptr<DescriptorMatcher> clone( bool emptyTrainData=false ) const CV_OVERRIDE;
1283
protected:
1284
    virtual void knnMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, int k,
1285
        InputArrayOfArrays masks=noArray(), bool compactResult=false ) CV_OVERRIDE;
1286
    virtual void radiusMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance,
1287
        InputArrayOfArrays masks=noArray(), bool compactResult=false ) CV_OVERRIDE;
1288
1289
    int normType;
1290
    bool crossCheck;
1291
};
1292
1293
#if defined(HAVE_OPENCV_FLANN) || defined(CV_DOXYGEN)
1294
1295
/** @brief Flann-based descriptor matcher.
1296
1297
This matcher trains cv::flann::Index on a train descriptor collection and calls its nearest search
1298
methods to find the best matches. So, this matcher may be faster when matching a large train
1299
collection than the brute force matcher. FlannBasedMatcher does not support masking permissible
1300
matches of descriptor sets because flann::Index does not support this. :
1301
 */
1302
class CV_EXPORTS_W FlannBasedMatcher : public DescriptorMatcher
1303
{
1304
public:
1305
    CV_WRAP FlannBasedMatcher( const Ptr<flann::IndexParams>& indexParams=makePtr<flann::KDTreeIndexParams>(),
1306
                       const Ptr<flann::SearchParams>& searchParams=makePtr<flann::SearchParams>() );
1307
1308
    virtual void add( InputArrayOfArrays descriptors ) CV_OVERRIDE;
1309
    virtual void clear() CV_OVERRIDE;
1310
1311
    // Reads matcher object from a file node
1312
    virtual void read( const FileNode& ) CV_OVERRIDE;
1313
    // Writes matcher object to a file storage
1314
    virtual void write( FileStorage& ) const CV_OVERRIDE;
1315
1316
    virtual void train() CV_OVERRIDE;
1317
    virtual bool isMaskSupported() const CV_OVERRIDE;
1318
1319
    CV_WRAP static Ptr<FlannBasedMatcher> create();
1320
1321
    CV_NODISCARD_STD virtual Ptr<DescriptorMatcher> clone( bool emptyTrainData=false ) const CV_OVERRIDE;
1322
protected:
1323
    static void convertToDMatches( const DescriptorCollection& descriptors,
1324
                                   const Mat& indices, const Mat& distances,
1325
                                   std::vector<std::vector<DMatch> >& matches );
1326
1327
    virtual void knnMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, int k,
1328
        InputArrayOfArrays masks=noArray(), bool compactResult=false ) CV_OVERRIDE;
1329
    virtual void radiusMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance,
1330
        InputArrayOfArrays masks=noArray(), bool compactResult=false ) CV_OVERRIDE;
1331
1332
    Ptr<flann::IndexParams> indexParams;
1333
    Ptr<flann::SearchParams> searchParams;
1334
    Ptr<flann::Index> flannIndex;
1335
1336
    DescriptorCollection mergedDescriptors;
1337
    int addedDescCount;
1338
};
1339
1340
#endif
1341
1342
//! @} features2d_match
1343
1344
/****************************************************************************************\
1345
*                                   Drawing functions                                    *
1346
\****************************************************************************************/
1347
1348
//! @addtogroup features2d_draw
1349
//! @{
1350
1351
enum struct DrawMatchesFlags
1352
{
1353
  DEFAULT = 0, //!< Output image matrix will be created (Mat::create),
1354
               //!< i.e. existing memory of output image may be reused.
1355
               //!< Two source image, matches and single keypoints will be drawn.
1356
               //!< For each keypoint only the center point will be drawn (without
1357
               //!< the circle around keypoint with keypoint size and orientation).
1358
  DRAW_OVER_OUTIMG = 1, //!< Output image matrix will not be created (Mat::create).
1359
                        //!< Matches will be drawn on existing content of output image.
1360
  NOT_DRAW_SINGLE_POINTS = 2, //!< Single keypoints will not be drawn.
1361
  DRAW_RICH_KEYPOINTS = 4 //!< For each keypoint the circle around keypoint with keypoint size and
1362
                          //!< orientation will be drawn.
1363
};
1364
CV_ENUM_FLAGS(DrawMatchesFlags)
1365
1366
/** @brief Draws keypoints.
1367
1368
@param image Source image.
1369
@param keypoints Keypoints from the source image.
1370
@param outImage Output image. Its content depends on the flags value defining what is drawn in the
1371
output image. See possible flags bit values below.
1372
@param color Color of keypoints.
1373
@param flags Flags setting drawing features. Possible flags bit values are defined by
1374
DrawMatchesFlags. See details above in drawMatches .
1375
1376
@note
1377
For Python API, flags are modified as cv.DRAW_MATCHES_FLAGS_DEFAULT,
1378
cv.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS, cv.DRAW_MATCHES_FLAGS_DRAW_OVER_OUTIMG,
1379
cv.DRAW_MATCHES_FLAGS_NOT_DRAW_SINGLE_POINTS
1380
 */
1381
CV_EXPORTS_W void drawKeypoints( InputArray image, const std::vector<KeyPoint>& keypoints, InputOutputArray outImage,
1382
                               const Scalar& color=Scalar::all(-1), DrawMatchesFlags flags=DrawMatchesFlags::DEFAULT );
1383
1384
/** @brief Draws the found matches of keypoints from two images.
1385
1386
@param img1 First source image.
1387
@param keypoints1 Keypoints from the first source image.
1388
@param img2 Second source image.
1389
@param keypoints2 Keypoints from the second source image.
1390
@param matches1to2 Matches from the first image to the second one, which means that keypoints1[i]
1391
has a corresponding point in keypoints2[matches[i]] .
1392
@param outImg Output image. Its content depends on the flags value defining what is drawn in the
1393
output image. See possible flags bit values below.
1394
@param matchColor Color of matches (lines and connected keypoints). If matchColor==Scalar::all(-1)
1395
, the color is generated randomly.
1396
@param singlePointColor Color of single keypoints (circles), which means that keypoints do not
1397
have the matches. If singlePointColor==Scalar::all(-1) , the color is generated randomly.
1398
@param matchesMask Mask determining which matches are drawn. If the mask is empty, all matches are
1399
drawn.
1400
@param flags Flags setting drawing features. Possible flags bit values are defined by
1401
DrawMatchesFlags.
1402
1403
This function draws matches of keypoints from two images in the output image. Match is a line
1404
connecting two keypoints (circles). See cv::DrawMatchesFlags.
1405
 */
1406
CV_EXPORTS_W void drawMatches( InputArray img1, const std::vector<KeyPoint>& keypoints1,
1407
                             InputArray img2, const std::vector<KeyPoint>& keypoints2,
1408
                             const std::vector<DMatch>& matches1to2, InputOutputArray outImg,
1409
                             const Scalar& matchColor=Scalar::all(-1), const Scalar& singlePointColor=Scalar::all(-1),
1410
                             const std::vector<char>& matchesMask=std::vector<char>(), DrawMatchesFlags flags=DrawMatchesFlags::DEFAULT );
1411
1412
/** @overload */
1413
CV_EXPORTS_W void drawMatches( InputArray img1, const std::vector<KeyPoint>& keypoints1,
1414
                             InputArray img2, const std::vector<KeyPoint>& keypoints2,
1415
                             const std::vector<DMatch>& matches1to2, InputOutputArray outImg,
1416
                             const int matchesThickness, const Scalar& matchColor=Scalar::all(-1),
1417
                             const Scalar& singlePointColor=Scalar::all(-1), const std::vector<char>& matchesMask=std::vector<char>(),
1418
                             DrawMatchesFlags flags=DrawMatchesFlags::DEFAULT );
1419
1420
CV_EXPORTS_AS(drawMatchesKnn) void drawMatches( InputArray img1, const std::vector<KeyPoint>& keypoints1,
1421
                             InputArray img2, const std::vector<KeyPoint>& keypoints2,
1422
                             const std::vector<std::vector<DMatch> >& matches1to2, InputOutputArray outImg,
1423
                             const Scalar& matchColor=Scalar::all(-1), const Scalar& singlePointColor=Scalar::all(-1),
1424
                             const std::vector<std::vector<char> >& matchesMask=std::vector<std::vector<char> >(), DrawMatchesFlags flags=DrawMatchesFlags::DEFAULT );
1425
1426
//! @} features2d_draw
1427
1428
/****************************************************************************************\
1429
*   Functions to evaluate the feature detectors and [generic] descriptor extractors      *
1430
\****************************************************************************************/
1431
1432
//! @addtogroup features2d_main
1433
//! @{
1434
1435
CV_EXPORTS void evaluateFeatureDetector( const Mat& img1, const Mat& img2, const Mat& H1to2,
1436
                                         std::vector<KeyPoint>* keypoints1, std::vector<KeyPoint>* keypoints2,
1437
                                         float& repeatability, int& correspCount,
1438
                                         const Ptr<FeatureDetector>& fdetector=Ptr<FeatureDetector>() );
1439
1440
CV_EXPORTS void computeRecallPrecisionCurve( const std::vector<std::vector<DMatch> >& matches1to2,
1441
                                             const std::vector<std::vector<uchar> >& correctMatches1to2Mask,
1442
                                             std::vector<Point2f>& recallPrecisionCurve );
1443
1444
CV_EXPORTS float getRecall( const std::vector<Point2f>& recallPrecisionCurve, float l_precision );
1445
CV_EXPORTS int getNearestPoint( const std::vector<Point2f>& recallPrecisionCurve, float l_precision );
1446
1447
//! @}
1448
1449
/****************************************************************************************\
1450
*                                     Bag of visual words                                *
1451
\****************************************************************************************/
1452
1453
//! @addtogroup features2d_category
1454
//! @{
1455
1456
/** @brief Abstract base class for training the *bag of visual words* vocabulary from a set of descriptors.
1457
1458
For details, see, for example, *Visual Categorization with Bags of Keypoints* by Gabriella Csurka,
1459
Christopher R. Dance, Lixin Fan, Jutta Willamowski, Cedric Bray, 2004. :
1460
 */
1461
class CV_EXPORTS_W BOWTrainer
1462
{
1463
public:
1464
    BOWTrainer();
1465
    virtual ~BOWTrainer();
1466
1467
    /** @brief Adds descriptors to a training set.
1468
1469
    @param descriptors Descriptors to add to a training set. Each row of the descriptors matrix is a
1470
    descriptor.
1471
1472
    The training set is clustered using clustermethod to construct the vocabulary.
1473
     */
1474
    CV_WRAP void add( const Mat& descriptors );
1475
1476
    /** @brief Returns a training set of descriptors.
1477
    */
1478
    CV_WRAP const std::vector<Mat>& getDescriptors() const;
1479
1480
    /** @brief Returns the count of all descriptors stored in the training set.
1481
    */
1482
    CV_WRAP int descriptorsCount() const;
1483
1484
    CV_WRAP virtual void clear();
1485
1486
    /** @overload */
1487
    CV_WRAP virtual Mat cluster() const = 0;
1488
1489
    /** @brief Clusters train descriptors.
1490
1491
    @param descriptors Descriptors to cluster. Each row of the descriptors matrix is a descriptor.
1492
    Descriptors are not added to the inner train descriptor set.
1493
1494
    The vocabulary consists of cluster centers. So, this method returns the vocabulary. In the first
1495
    variant of the method, train descriptors stored in the object are clustered. In the second variant,
1496
    input descriptors are clustered.
1497
     */
1498
    CV_WRAP virtual Mat cluster( const Mat& descriptors ) const = 0;
1499
1500
protected:
1501
    std::vector<Mat> descriptors;
1502
    int size;
1503
};
1504
1505
/** @brief kmeans -based class to train visual vocabulary using the *bag of visual words* approach. :
1506
 */
1507
class CV_EXPORTS_W BOWKMeansTrainer : public BOWTrainer
1508
{
1509
public:
1510
    /** @brief The constructor.
1511
1512
    @see cv::kmeans
1513
    */
1514
    CV_WRAP BOWKMeansTrainer( int clusterCount, const TermCriteria& termcrit=TermCriteria(),
1515
                      int attempts=3, int flags=KMEANS_PP_CENTERS );
1516
    virtual ~BOWKMeansTrainer();
1517
1518
    // Returns trained vocabulary (i.e. cluster centers).
1519
    CV_WRAP virtual Mat cluster() const CV_OVERRIDE;
1520
    CV_WRAP virtual Mat cluster( const Mat& descriptors ) const CV_OVERRIDE;
1521
1522
protected:
1523
1524
    int clusterCount;
1525
    TermCriteria termcrit;
1526
    int attempts;
1527
    int flags;
1528
};
1529
1530
/** @brief Class to compute an image descriptor using the *bag of visual words*.
1531
1532
Such a computation consists of the following steps:
1533
1534
1.  Compute descriptors for a given image and its keypoints set.
1535
2.  Find the nearest visual words from the vocabulary for each keypoint descriptor.
1536
3.  Compute the bag-of-words image descriptor as is a normalized histogram of vocabulary words
1537
encountered in the image. The i-th bin of the histogram is a frequency of i-th word of the
1538
vocabulary in the given image.
1539
 */
1540
class CV_EXPORTS_W BOWImgDescriptorExtractor
1541
{
1542
public:
1543
    /** @brief The constructor.
1544
1545
    @param dextractor Descriptor extractor that is used to compute descriptors for an input image and
1546
    its keypoints.
1547
    @param dmatcher Descriptor matcher that is used to find the nearest word of the trained vocabulary
1548
    for each keypoint descriptor of the image.
1549
     */
1550
    CV_WRAP BOWImgDescriptorExtractor( const Ptr<Feature2D>& dextractor,
1551
                                       const Ptr<DescriptorMatcher>& dmatcher );
1552
    /** @overload */
1553
    BOWImgDescriptorExtractor( const Ptr<DescriptorMatcher>& dmatcher );
1554
    virtual ~BOWImgDescriptorExtractor();
1555
1556
    /** @brief Sets a visual vocabulary.
1557
1558
    @param vocabulary Vocabulary (can be trained using the inheritor of BOWTrainer ). Each row of the
1559
    vocabulary is a visual word (cluster center).
1560
     */
1561
    CV_WRAP void setVocabulary( const Mat& vocabulary );
1562
1563
    /** @brief Returns the set vocabulary.
1564
    */
1565
    CV_WRAP const Mat& getVocabulary() const;
1566
1567
    /** @brief Computes an image descriptor using the set visual vocabulary.
1568
1569
    @param image Image, for which the descriptor is computed.
1570
    @param keypoints Keypoints detected in the input image.
1571
    @param imgDescriptor Computed output image descriptor.
1572
    @param pointIdxsOfClusters Indices of keypoints that belong to the cluster. This means that
1573
    pointIdxsOfClusters[i] are keypoint indices that belong to the i -th cluster (word of vocabulary)
1574
    returned if it is non-zero.
1575
    @param descriptors Descriptors of the image keypoints that are returned if they are non-zero.
1576
     */
1577
    void compute( InputArray image, std::vector<KeyPoint>& keypoints, OutputArray imgDescriptor,
1578
                  std::vector<std::vector<int> >* pointIdxsOfClusters=0, Mat* descriptors=0 );
1579
    /** @overload
1580
    @param keypointDescriptors Computed descriptors to match with vocabulary.
1581
    @param imgDescriptor Computed output image descriptor.
1582
    @param pointIdxsOfClusters Indices of keypoints that belong to the cluster. This means that
1583
    pointIdxsOfClusters[i] are keypoint indices that belong to the i -th cluster (word of vocabulary)
1584
    returned if it is non-zero.
1585
    */
1586
    void compute( InputArray keypointDescriptors, OutputArray imgDescriptor,
1587
                  std::vector<std::vector<int> >* pointIdxsOfClusters=0 );
1588
    // compute() is not constant because DescriptorMatcher::match is not constant
1589
1590
    CV_WRAP_AS(compute) void compute2( const Mat& image, std::vector<KeyPoint>& keypoints, CV_OUT Mat& imgDescriptor )
1591
0
    { compute(image,keypoints,imgDescriptor); }
1592
1593
    /** @brief Returns an image descriptor size if the vocabulary is set. Otherwise, it returns 0.
1594
    */
1595
    CV_WRAP int descriptorSize() const;
1596
1597
    /** @brief Returns an image descriptor type.
1598
     */
1599
    CV_WRAP int descriptorType() const;
1600
1601
protected:
1602
    Mat vocabulary;
1603
    Ptr<DescriptorExtractor> dextractor;
1604
    Ptr<DescriptorMatcher> dmatcher;
1605
};
1606
1607
//! @} features2d_category
1608
1609
} /* namespace cv */
1610
1611
#endif