Coverage Report

Created: 2023-06-07 08:11

/work/install-coverage/include/opencv4/opencv2/features2d.hpp
Line
Count
Source (jump to first uncovered line)
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 you to
60
easily switch between different algorithms solving the same problem. This section is devoted to
61
matching descriptors that are represented as vectors in a multidimensional space. All objects that
62
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
//! @} features2d_main
571
572
//! @addtogroup features2d_main
573
//! @{
574
575
/** @brief Wrapping class for feature detection using the FAST method. :
576
 */
577
class CV_EXPORTS_W FastFeatureDetector : public Feature2D
578
{
579
public:
580
    enum DetectorType
581
    {
582
        TYPE_5_8 = 0, TYPE_7_12 = 1, TYPE_9_16 = 2
583
    };
584
    enum
585
    {
586
        THRESHOLD = 10000, NONMAX_SUPPRESSION=10001, FAST_N=10002
587
    };
588
589
590
    CV_WRAP static Ptr<FastFeatureDetector> create( int threshold=10,
591
                                                    bool nonmaxSuppression=true,
592
                                                    FastFeatureDetector::DetectorType type=FastFeatureDetector::TYPE_9_16 );
593
594
    CV_WRAP virtual void setThreshold(int threshold) = 0;
595
    CV_WRAP virtual int getThreshold() const = 0;
596
597
    CV_WRAP virtual void setNonmaxSuppression(bool f) = 0;
598
    CV_WRAP virtual bool getNonmaxSuppression() const = 0;
599
600
    CV_WRAP virtual void setType(FastFeatureDetector::DetectorType type) = 0;
601
    CV_WRAP virtual FastFeatureDetector::DetectorType getType() const = 0;
602
    CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
603
};
604
605
/** @overload */
606
CV_EXPORTS void FAST( InputArray image, CV_OUT std::vector<KeyPoint>& keypoints,
607
                      int threshold, bool nonmaxSuppression=true );
608
609
/** @brief Detects corners using the FAST algorithm
610
611
@param image grayscale image where keypoints (corners) are detected.
612
@param keypoints keypoints detected on the image.
613
@param threshold threshold on difference between intensity of the central pixel and pixels of a
614
circle around this pixel.
615
@param nonmaxSuppression if true, non-maximum suppression is applied to detected corners
616
(keypoints).
617
@param type one of the three neighborhoods as defined in the paper:
618
FastFeatureDetector::TYPE_9_16, FastFeatureDetector::TYPE_7_12,
619
FastFeatureDetector::TYPE_5_8
620
621
Detects corners using the FAST algorithm by @cite Rosten06 .
622
623
@note In Python API, types are given as cv.FAST_FEATURE_DETECTOR_TYPE_5_8,
624
cv.FAST_FEATURE_DETECTOR_TYPE_7_12 and cv.FAST_FEATURE_DETECTOR_TYPE_9_16. For corner
625
detection, use cv.FAST.detect() method.
626
 */
627
CV_EXPORTS void FAST( InputArray image, CV_OUT std::vector<KeyPoint>& keypoints,
628
                      int threshold, bool nonmaxSuppression, FastFeatureDetector::DetectorType type );
629
630
//! @} features2d_main
631
632
//! @addtogroup features2d_main
633
//! @{
634
635
/** @brief Wrapping class for feature detection using the AGAST method. :
636
 */
637
class CV_EXPORTS_W AgastFeatureDetector : public Feature2D
638
{
639
public:
640
    enum DetectorType
641
    {
642
        AGAST_5_8 = 0, AGAST_7_12d = 1, AGAST_7_12s = 2, OAST_9_16 = 3,
643
    };
644
645
    enum
646
    {
647
        THRESHOLD = 10000, NONMAX_SUPPRESSION = 10001,
648
    };
649
650
    CV_WRAP static Ptr<AgastFeatureDetector> create( int threshold=10,
651
                                                     bool nonmaxSuppression=true,
652
                                                     AgastFeatureDetector::DetectorType type = AgastFeatureDetector::OAST_9_16);
653
654
    CV_WRAP virtual void setThreshold(int threshold) = 0;
655
    CV_WRAP virtual int getThreshold() const = 0;
656
657
    CV_WRAP virtual void setNonmaxSuppression(bool f) = 0;
658
    CV_WRAP virtual bool getNonmaxSuppression() const = 0;
659
660
    CV_WRAP virtual void setType(AgastFeatureDetector::DetectorType type) = 0;
661
    CV_WRAP virtual AgastFeatureDetector::DetectorType getType() const = 0;
662
    CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
663
};
664
665
/** @overload */
666
CV_EXPORTS void AGAST( InputArray image, CV_OUT std::vector<KeyPoint>& keypoints,
667
                      int threshold, bool nonmaxSuppression=true );
668
669
/** @brief Detects corners using the AGAST algorithm
670
671
@param image grayscale image where keypoints (corners) are detected.
672
@param keypoints keypoints detected on the image.
673
@param threshold threshold on difference between intensity of the central pixel and pixels of a
674
circle around this pixel.
675
@param nonmaxSuppression if true, non-maximum suppression is applied to detected corners
676
(keypoints).
677
@param type one of the four neighborhoods as defined in the paper:
678
AgastFeatureDetector::AGAST_5_8, AgastFeatureDetector::AGAST_7_12d,
679
AgastFeatureDetector::AGAST_7_12s, AgastFeatureDetector::OAST_9_16
680
681
For non-Intel platforms, there is a tree optimised variant of AGAST with same numerical results.
682
The 32-bit binary tree tables were generated automatically from original code using perl script.
683
The perl script and examples of tree generation are placed in features2d/doc folder.
684
Detects corners using the AGAST algorithm by @cite mair2010_agast .
685
686
 */
687
CV_EXPORTS void AGAST( InputArray image, CV_OUT std::vector<KeyPoint>& keypoints,
688
                      int threshold, bool nonmaxSuppression, AgastFeatureDetector::DetectorType type );
689
690
/** @brief Wrapping class for feature detection using the goodFeaturesToTrack function. :
691
 */
692
class CV_EXPORTS_W GFTTDetector : public Feature2D
693
{
694
public:
695
    CV_WRAP static Ptr<GFTTDetector> create( int maxCorners=1000, double qualityLevel=0.01, double minDistance=1,
696
                                             int blockSize=3, bool useHarrisDetector=false, double k=0.04 );
697
    CV_WRAP static Ptr<GFTTDetector> create( int maxCorners, double qualityLevel, double minDistance,
698
                                             int blockSize, int gradiantSize, bool useHarrisDetector=false, double k=0.04 );
699
    CV_WRAP virtual void setMaxFeatures(int maxFeatures) = 0;
700
    CV_WRAP virtual int getMaxFeatures() const = 0;
701
702
    CV_WRAP virtual void setQualityLevel(double qlevel) = 0;
703
    CV_WRAP virtual double getQualityLevel() const = 0;
704
705
    CV_WRAP virtual void setMinDistance(double minDistance) = 0;
706
    CV_WRAP virtual double getMinDistance() const = 0;
707
708
    CV_WRAP virtual void setBlockSize(int blockSize) = 0;
709
    CV_WRAP virtual int getBlockSize() const = 0;
710
711
    CV_WRAP virtual void setGradientSize(int gradientSize_) = 0;
712
    CV_WRAP virtual int getGradientSize() = 0;
713
714
    CV_WRAP virtual void setHarrisDetector(bool val) = 0;
715
    CV_WRAP virtual bool getHarrisDetector() const = 0;
716
717
    CV_WRAP virtual void setK(double k) = 0;
718
    CV_WRAP virtual double getK() const = 0;
719
    CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
720
};
721
722
/** @brief Class for extracting blobs from an image. :
723
724
The class implements a simple algorithm for extracting blobs from an image:
725
726
1.  Convert the source image to binary images by applying thresholding with several thresholds from
727
    minThreshold (inclusive) to maxThreshold (exclusive) with distance thresholdStep between
728
    neighboring thresholds.
729
2.  Extract connected components from every binary image by findContours and calculate their
730
    centers.
731
3.  Group centers from several binary images by their coordinates. Close centers form one group that
732
    corresponds to one blob, which is controlled by the minDistBetweenBlobs parameter.
733
4.  From the groups, estimate final centers of blobs and their radiuses and return as locations and
734
    sizes of keypoints.
735
736
This class performs several filtrations of returned blobs. You should set filterBy\* to true/false
737
to turn on/off corresponding filtration. Available filtrations:
738
739
-   **By color**. This filter compares the intensity of a binary image at the center of a blob to
740
blobColor. If they differ, the blob is filtered out. Use blobColor = 0 to extract dark blobs
741
and blobColor = 255 to extract light blobs.
742
-   **By area**. Extracted blobs have an area between minArea (inclusive) and maxArea (exclusive).
743
-   **By circularity**. Extracted blobs have circularity
744
(\f$\frac{4*\pi*Area}{perimeter * perimeter}\f$) between minCircularity (inclusive) and
745
maxCircularity (exclusive).
746
-   **By ratio of the minimum inertia to maximum inertia**. Extracted blobs have this ratio
747
between minInertiaRatio (inclusive) and maxInertiaRatio (exclusive).
748
-   **By convexity**. Extracted blobs have convexity (area / area of blob convex hull) between
749
minConvexity (inclusive) and maxConvexity (exclusive).
750
751
Default values of parameters are tuned to extract dark circular blobs.
752
 */
753
class CV_EXPORTS_W SimpleBlobDetector : public Feature2D
754
{
755
public:
756
  struct CV_EXPORTS_W_SIMPLE Params
757
  {
758
      CV_WRAP Params();
759
      CV_PROP_RW float thresholdStep;
760
      CV_PROP_RW float minThreshold;
761
      CV_PROP_RW float maxThreshold;
762
      CV_PROP_RW size_t minRepeatability;
763
      CV_PROP_RW float minDistBetweenBlobs;
764
765
      CV_PROP_RW bool filterByColor;
766
      CV_PROP_RW uchar blobColor;
767
768
      CV_PROP_RW bool filterByArea;
769
      CV_PROP_RW float minArea, maxArea;
770
771
      CV_PROP_RW bool filterByCircularity;
772
      CV_PROP_RW float minCircularity, maxCircularity;
773
774
      CV_PROP_RW bool filterByInertia;
775
      CV_PROP_RW float minInertiaRatio, maxInertiaRatio;
776
777
      CV_PROP_RW bool filterByConvexity;
778
      CV_PROP_RW float minConvexity, maxConvexity;
779
780
      CV_PROP_RW bool collectContours;
781
782
      void read( const FileNode& fn );
783
      void write( FileStorage& fs ) const;
784
  };
785
786
  CV_WRAP static Ptr<SimpleBlobDetector>
787
    create(const SimpleBlobDetector::Params &parameters = SimpleBlobDetector::Params());
788
789
  CV_WRAP virtual void setParams(const SimpleBlobDetector::Params& params ) = 0;
790
  CV_WRAP virtual SimpleBlobDetector::Params getParams() const = 0;
791
792
  CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
793
  CV_WRAP virtual const std::vector<std::vector<cv::Point> >& getBlobContours() const;
794
};
795
796
//! @} features2d_main
797
798
//! @addtogroup features2d_main
799
//! @{
800
801
/** @brief Class implementing the KAZE keypoint detector and descriptor extractor, described in @cite ABD12 .
802
803
@note AKAZE descriptor can only be used with KAZE or AKAZE keypoints .. [ABD12] KAZE Features. Pablo
804
F. Alcantarilla, Adrien Bartoli and Andrew J. Davison. In European Conference on Computer Vision
805
(ECCV), Fiorenze, Italy, October 2012.
806
*/
807
class CV_EXPORTS_W KAZE : public Feature2D
808
{
809
public:
810
    enum DiffusivityType
811
    {
812
        DIFF_PM_G1 = 0,
813
        DIFF_PM_G2 = 1,
814
        DIFF_WEICKERT = 2,
815
        DIFF_CHARBONNIER = 3
816
    };
817
818
    /** @brief The KAZE constructor
819
820
    @param extended Set to enable extraction of extended (128-byte) descriptor.
821
    @param upright Set to enable use of upright descriptors (non rotation-invariant).
822
    @param threshold Detector response threshold to accept point
823
    @param nOctaves Maximum octave evolution of the image
824
    @param nOctaveLayers Default number of sublevels per scale level
825
    @param diffusivity Diffusivity type. DIFF_PM_G1, DIFF_PM_G2, DIFF_WEICKERT or
826
    DIFF_CHARBONNIER
827
     */
828
    CV_WRAP static Ptr<KAZE> create(bool extended=false, bool upright=false,
829
                                    float threshold = 0.001f,
830
                                    int nOctaves = 4, int nOctaveLayers = 4,
831
                                    KAZE::DiffusivityType diffusivity = KAZE::DIFF_PM_G2);
832
833
    CV_WRAP virtual void setExtended(bool extended) = 0;
834
    CV_WRAP virtual bool getExtended() const = 0;
835
836
    CV_WRAP virtual void setUpright(bool upright) = 0;
837
    CV_WRAP virtual bool getUpright() const = 0;
838
839
    CV_WRAP virtual void setThreshold(double threshold) = 0;
840
    CV_WRAP virtual double getThreshold() const = 0;
841
842
    CV_WRAP virtual void setNOctaves(int octaves) = 0;
843
    CV_WRAP virtual int getNOctaves() const = 0;
844
845
    CV_WRAP virtual void setNOctaveLayers(int octaveLayers) = 0;
846
    CV_WRAP virtual int getNOctaveLayers() const = 0;
847
848
    CV_WRAP virtual void setDiffusivity(KAZE::DiffusivityType diff) = 0;
849
    CV_WRAP virtual KAZE::DiffusivityType getDiffusivity() const = 0;
850
    CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
851
};
852
853
/** @brief Class implementing the AKAZE keypoint detector and descriptor extractor, described in @cite ANB13.
854
855
@details AKAZE descriptors can only be used with KAZE or AKAZE keypoints. This class is thread-safe.
856
857
@note When you need descriptors use Feature2D::detectAndCompute, which
858
provides better performance. When using Feature2D::detect followed by
859
Feature2D::compute scale space pyramid is computed twice.
860
861
@note AKAZE implements T-API. When image is passed as UMat some parts of the algorithm
862
will use OpenCL.
863
864
@note [ANB13] Fast Explicit Diffusion for Accelerated Features in Nonlinear
865
Scale Spaces. Pablo F. Alcantarilla, Jesús Nuevo and Adrien Bartoli. In
866
British Machine Vision Conference (BMVC), Bristol, UK, September 2013.
867
868
*/
869
class CV_EXPORTS_W AKAZE : public Feature2D
870
{
871
public:
872
    // AKAZE descriptor type
873
    enum DescriptorType
874
    {
875
        DESCRIPTOR_KAZE_UPRIGHT = 2, ///< Upright descriptors, not invariant to rotation
876
        DESCRIPTOR_KAZE = 3,
877
        DESCRIPTOR_MLDB_UPRIGHT = 4, ///< Upright descriptors, not invariant to rotation
878
        DESCRIPTOR_MLDB = 5
879
    };
880
881
    /** @brief The AKAZE constructor
882
883
    @param descriptor_type Type of the extracted descriptor: DESCRIPTOR_KAZE,
884
    DESCRIPTOR_KAZE_UPRIGHT, DESCRIPTOR_MLDB or DESCRIPTOR_MLDB_UPRIGHT.
885
    @param descriptor_size Size of the descriptor in bits. 0 -\> Full size
886
    @param descriptor_channels Number of channels in the descriptor (1, 2, 3)
887
    @param threshold Detector response threshold to accept point
888
    @param nOctaves Maximum octave evolution of the image
889
    @param nOctaveLayers Default number of sublevels per scale level
890
    @param diffusivity Diffusivity type. DIFF_PM_G1, DIFF_PM_G2, DIFF_WEICKERT or
891
    DIFF_CHARBONNIER
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
898
    CV_WRAP virtual void setDescriptorType(AKAZE::DescriptorType dtype) = 0;
899
    CV_WRAP virtual AKAZE::DescriptorType getDescriptorType() const = 0;
900
901
    CV_WRAP virtual void setDescriptorSize(int dsize) = 0;
902
    CV_WRAP virtual int getDescriptorSize() const = 0;
903
904
    CV_WRAP virtual void setDescriptorChannels(int dch) = 0;
905
    CV_WRAP virtual int getDescriptorChannels() const = 0;
906
907
    CV_WRAP virtual void setThreshold(double threshold) = 0;
908
    CV_WRAP virtual double getThreshold() const = 0;
909
910
    CV_WRAP virtual void setNOctaves(int octaves) = 0;
911
    CV_WRAP virtual int getNOctaves() const = 0;
912
913
    CV_WRAP virtual void setNOctaveLayers(int octaveLayers) = 0;
914
    CV_WRAP virtual int getNOctaveLayers() const = 0;
915
916
    CV_WRAP virtual void setDiffusivity(KAZE::DiffusivityType diff) = 0;
917
    CV_WRAP virtual KAZE::DiffusivityType getDiffusivity() const = 0;
918
    CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
919
};
920
921
//! @} features2d_main
922
923
/****************************************************************************************\
924
*                                      Distance                                          *
925
\****************************************************************************************/
926
927
template<typename T>
928
struct CV_EXPORTS Accumulator
929
{
930
    typedef T Type;
931
};
932
933
template<> struct Accumulator<unsigned char>  { typedef float Type; };
934
template<> struct Accumulator<unsigned short> { typedef float Type; };
935
template<> struct Accumulator<char>   { typedef float Type; };
936
template<> struct Accumulator<short>  { typedef float Type; };
937
938
/*
939
 * Squared Euclidean distance functor
940
 */
941
template<class T>
942
struct CV_EXPORTS SL2
943
{
944
    static const NormTypes normType = NORM_L2SQR;
945
    typedef T ValueType;
946
    typedef typename Accumulator<T>::Type ResultType;
947
948
    ResultType operator()( const T* a, const T* b, int size ) const
949
    {
950
        return normL2Sqr<ValueType, ResultType>(a, b, size);
951
    }
952
};
953
954
/*
955
 * Euclidean distance functor
956
 */
957
template<class T>
958
struct L2
959
{
960
    static const NormTypes normType = NORM_L2;
961
    typedef T ValueType;
962
    typedef typename Accumulator<T>::Type ResultType;
963
964
    ResultType operator()( const T* a, const T* b, int size ) const
965
    {
966
        return (ResultType)std::sqrt((double)normL2Sqr<ValueType, ResultType>(a, b, size));
967
    }
968
};
969
970
/*
971
 * Manhattan distance (city block distance) functor
972
 */
973
template<class T>
974
struct L1
975
{
976
    static const NormTypes normType = NORM_L1;
977
    typedef T ValueType;
978
    typedef typename Accumulator<T>::Type ResultType;
979
980
    ResultType operator()( const T* a, const T* b, int size ) const
981
    {
982
        return normL1<ValueType, ResultType>(a, b, size);
983
    }
984
};
985
986
/****************************************************************************************\
987
*                                  DescriptorMatcher                                     *
988
\****************************************************************************************/
989
990
//! @addtogroup features2d_match
991
//! @{
992
993
/** @brief Abstract base class for matching keypoint descriptors.
994
995
It has two groups of match methods: for matching descriptors of an image with another image or with
996
an image set.
997
 */
998
class CV_EXPORTS_W DescriptorMatcher : public Algorithm
999
{
1000
public:
1001
   enum MatcherType
1002
    {
1003
        FLANNBASED            = 1,
1004
        BRUTEFORCE            = 2,
1005
        BRUTEFORCE_L1         = 3,
1006
        BRUTEFORCE_HAMMING    = 4,
1007
        BRUTEFORCE_HAMMINGLUT = 5,
1008
        BRUTEFORCE_SL2        = 6
1009
    };
1010
1011
    virtual ~DescriptorMatcher();
1012
1013
    /** @brief Adds descriptors to train a CPU(trainDescCollectionis) or GPU(utrainDescCollectionis) descriptor
1014
    collection.
1015
1016
    If the collection is not empty, the new descriptors are added to existing train descriptors.
1017
1018
    @param descriptors Descriptors to add. Each descriptors[i] is a set of descriptors from the same
1019
    train image.
1020
     */
1021
    CV_WRAP virtual void add( InputArrayOfArrays descriptors );
1022
1023
    /** @brief Returns a constant link to the train descriptor collection trainDescCollection .
1024
     */
1025
    CV_WRAP const std::vector<Mat>& getTrainDescriptors() const;
1026
1027
    /** @brief Clears the train descriptor collections.
1028
     */
1029
    CV_WRAP virtual void clear() CV_OVERRIDE;
1030
1031
    /** @brief Returns true if there are no train descriptors in the both collections.
1032
     */
1033
    CV_WRAP virtual bool empty() const CV_OVERRIDE;
1034
1035
    /** @brief Returns true if the descriptor matcher supports masking permissible matches.
1036
     */
1037
    CV_WRAP virtual bool isMaskSupported() const = 0;
1038
1039
    /** @brief Trains a descriptor matcher
1040
1041
    Trains a descriptor matcher (for example, the flann index). In all methods to match, the method
1042
    train() is run every time before matching. Some descriptor matchers (for example, BruteForceMatcher)
1043
    have an empty implementation of this method. Other matchers really train their inner structures (for
1044
    example, FlannBasedMatcher trains flann::Index ).
1045
     */
1046
    CV_WRAP virtual void train();
1047
1048
    /** @brief Finds the best match for each descriptor from a query set.
1049
1050
    @param queryDescriptors Query set of descriptors.
1051
    @param trainDescriptors Train set of descriptors. This set is not added to the train descriptors
1052
    collection stored in the class object.
1053
    @param matches Matches. If a query descriptor is masked out in mask , no match is added for this
1054
    descriptor. So, matches size may be smaller than the query descriptors count.
1055
    @param mask Mask specifying permissible matches between an input query and train matrices of
1056
    descriptors.
1057
1058
    In the first variant of this method, the train descriptors are passed as an input argument. In the
1059
    second variant of the method, train descriptors collection that was set by DescriptorMatcher::add is
1060
    used. Optional mask (or masks) can be passed to specify which query and training descriptors can be
1061
    matched. Namely, queryDescriptors[i] can be matched with trainDescriptors[j] only if
1062
    mask.at\<uchar\>(i,j) is non-zero.
1063
     */
1064
    CV_WRAP void match( InputArray queryDescriptors, InputArray trainDescriptors,
1065
                CV_OUT std::vector<DMatch>& matches, InputArray mask=noArray() ) const;
1066
1067
    /** @brief Finds the k best matches for each descriptor from a query set.
1068
1069
    @param queryDescriptors Query set of descriptors.
1070
    @param trainDescriptors Train set of descriptors. This set is not added to the train descriptors
1071
    collection stored in the class object.
1072
    @param mask Mask specifying permissible matches between an input query and train matrices of
1073
    descriptors.
1074
    @param matches Matches. Each matches[i] is k or less matches for the same query descriptor.
1075
    @param k Count of best matches found per each query descriptor or less if a query descriptor has
1076
    less than k possible matches in total.
1077
    @param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
1078
    false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
1079
    the matches vector does not contain matches for fully masked-out query descriptors.
1080
1081
    These extended variants of DescriptorMatcher::match methods find several best matches for each query
1082
    descriptor. The matches are returned in the distance increasing order. See DescriptorMatcher::match
1083
    for the details about query and train descriptors.
1084
     */
1085
    CV_WRAP void knnMatch( InputArray queryDescriptors, InputArray trainDescriptors,
1086
                   CV_OUT std::vector<std::vector<DMatch> >& matches, int k,
1087
                   InputArray mask=noArray(), bool compactResult=false ) const;
1088
1089
    /** @brief For each query descriptor, finds the training descriptors not farther than the specified distance.
1090
1091
    @param queryDescriptors Query set of descriptors.
1092
    @param trainDescriptors Train set of descriptors. This set is not added to the train descriptors
1093
    collection stored in the class object.
1094
    @param matches Found matches.
1095
    @param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
1096
    false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
1097
    the matches vector does not contain matches for fully masked-out query descriptors.
1098
    @param maxDistance Threshold for the distance between matched descriptors. Distance means here
1099
    metric distance (e.g. Hamming distance), not the distance between coordinates (which is measured
1100
    in Pixels)!
1101
    @param mask Mask specifying permissible matches between an input query and train matrices of
1102
    descriptors.
1103
1104
    For each query descriptor, the methods find such training descriptors that the distance between the
1105
    query descriptor and the training descriptor is equal or smaller than maxDistance. Found matches are
1106
    returned in the distance increasing order.
1107
     */
1108
    CV_WRAP void radiusMatch( InputArray queryDescriptors, InputArray trainDescriptors,
1109
                      CV_OUT std::vector<std::vector<DMatch> >& matches, float maxDistance,
1110
                      InputArray mask=noArray(), bool compactResult=false ) const;
1111
1112
    /** @overload
1113
    @param queryDescriptors Query set of descriptors.
1114
    @param matches Matches. If a query descriptor is masked out in mask , no match is added for this
1115
    descriptor. So, matches size may be smaller than the query descriptors count.
1116
    @param masks Set of masks. Each masks[i] specifies permissible matches between the input query
1117
    descriptors and stored train descriptors from the i-th image trainDescCollection[i].
1118
    */
1119
    CV_WRAP void match( InputArray queryDescriptors, CV_OUT std::vector<DMatch>& matches,
1120
                        InputArrayOfArrays masks=noArray() );
1121
    /** @overload
1122
    @param queryDescriptors Query set of descriptors.
1123
    @param matches Matches. Each matches[i] is k or less matches for the same query descriptor.
1124
    @param k Count of best matches found per each query descriptor or less if a query descriptor has
1125
    less than k possible matches in total.
1126
    @param masks Set of masks. Each masks[i] specifies permissible matches between the input query
1127
    descriptors and stored train descriptors from the i-th image trainDescCollection[i].
1128
    @param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
1129
    false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
1130
    the matches vector does not contain matches for fully masked-out query descriptors.
1131
    */
1132
    CV_WRAP void knnMatch( InputArray queryDescriptors, CV_OUT std::vector<std::vector<DMatch> >& matches, int k,
1133
                           InputArrayOfArrays masks=noArray(), bool compactResult=false );
1134
    /** @overload
1135
    @param queryDescriptors Query set of descriptors.
1136
    @param matches Found matches.
1137
    @param maxDistance Threshold for the distance between matched descriptors. Distance means here
1138
    metric distance (e.g. Hamming distance), not the distance between coordinates (which is measured
1139
    in Pixels)!
1140
    @param masks Set of masks. Each masks[i] specifies permissible matches between the input query
1141
    descriptors and stored train descriptors from the i-th image trainDescCollection[i].
1142
    @param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
1143
    false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
1144
    the matches vector does not contain matches for fully masked-out query descriptors.
1145
    */
1146
    CV_WRAP void radiusMatch( InputArray queryDescriptors, CV_OUT std::vector<std::vector<DMatch> >& matches, float maxDistance,
1147
                      InputArrayOfArrays masks=noArray(), bool compactResult=false );
1148
1149
1150
    CV_WRAP void write( const String& fileName ) const
1151
0
    {
1152
0
        FileStorage fs(fileName, FileStorage::WRITE);
1153
0
        write(fs);
1154
0
    }
1155
1156
    CV_WRAP void read( const String& fileName )
1157
0
    {
1158
0
        FileStorage fs(fileName, FileStorage::READ);
1159
0
        read(fs.root());
1160
0
    }
1161
    // Reads matcher object from a file node
1162
    // see corresponding cv::Algorithm method
1163
    CV_WRAP virtual void read( const FileNode& ) CV_OVERRIDE;
1164
    // Writes matcher object to a file storage
1165
    virtual void write( FileStorage& ) const CV_OVERRIDE;
1166
1167
    /** @brief Clones the matcher.
1168
1169
    @param emptyTrainData If emptyTrainData is false, the method creates a deep copy of the object,
1170
    that is, copies both parameters and train data. If emptyTrainData is true, the method creates an
1171
    object copy with the current parameters but with empty train data.
1172
     */
1173
    CV_WRAP CV_NODISCARD_STD virtual Ptr<DescriptorMatcher> clone( bool emptyTrainData=false ) const = 0;
1174
1175
    /** @brief Creates a descriptor matcher of a given type with the default parameters (using default
1176
    constructor).
1177
1178
    @param descriptorMatcherType Descriptor matcher type. Now the following matcher types are
1179
    supported:
1180
    -   `BruteForce` (it uses L2 )
1181
    -   `BruteForce-L1`
1182
    -   `BruteForce-Hamming`
1183
    -   `BruteForce-Hamming(2)`
1184
    -   `FlannBased`
1185
     */
1186
    CV_WRAP static Ptr<DescriptorMatcher> create( const String& descriptorMatcherType );
1187
1188
    CV_WRAP static Ptr<DescriptorMatcher> create( const DescriptorMatcher::MatcherType& matcherType );
1189
1190
1191
    // see corresponding cv::Algorithm method
1192
0
    CV_WRAP inline void write(FileStorage& fs, const String& name) const { Algorithm::write(fs, name); }
1193
#if CV_VERSION_MAJOR < 5
1194
0
    inline void write(const Ptr<FileStorage>& fs, const String& name) const { CV_Assert(fs); Algorithm::write(*fs, name); }
1195
#endif
1196
1197
protected:
1198
    /**
1199
     * Class to work with descriptors from several images as with one merged matrix.
1200
     * It is used e.g. in FlannBasedMatcher.
1201
     */
1202
    class CV_EXPORTS DescriptorCollection
1203
    {
1204
    public:
1205
        DescriptorCollection();
1206
        DescriptorCollection( const DescriptorCollection& collection );
1207
        virtual ~DescriptorCollection();
1208
1209
        // Vector of matrices "descriptors" will be merged to one matrix "mergedDescriptors" here.
1210
        void set( const std::vector<Mat>& descriptors );
1211
        virtual void clear();
1212
1213
        const Mat& getDescriptors() const;
1214
        Mat getDescriptor( int imgIdx, int localDescIdx ) const;
1215
        Mat getDescriptor( int globalDescIdx ) const;
1216
        void getLocalIdx( int globalDescIdx, int& imgIdx, int& localDescIdx ) const;
1217
1218
        int size() const;
1219
1220
    protected:
1221
        Mat mergedDescriptors;
1222
        std::vector<int> startIdxs;
1223
    };
1224
1225
    //! In fact the matching is implemented only by the following two methods. These methods suppose
1226
    //! that the class object has been trained already. Public match methods call these methods
1227
    //! after calling train().
1228
    virtual void knnMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, int k,
1229
        InputArrayOfArrays masks=noArray(), bool compactResult=false ) = 0;
1230
    virtual void radiusMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance,
1231
        InputArrayOfArrays masks=noArray(), bool compactResult=false ) = 0;
1232
1233
    static bool isPossibleMatch( InputArray mask, int queryIdx, int trainIdx );
1234
    static bool isMaskedOut( InputArrayOfArrays masks, int queryIdx );
1235
1236
0
    CV_NODISCARD_STD static Mat clone_op( Mat m ) { return m.clone(); }
1237
    void checkMasks( InputArrayOfArrays masks, int queryDescriptorsCount ) const;
1238
1239
    //! Collection of descriptors from train images.
1240
    std::vector<Mat> trainDescCollection;
1241
    std::vector<UMat> utrainDescCollection;
1242
};
1243
1244
/** @brief Brute-force descriptor matcher.
1245
1246
For each descriptor in the first set, this matcher finds the closest descriptor in the second set
1247
by trying each one. This descriptor matcher supports masking permissible matches of descriptor
1248
sets.
1249
 */
1250
class CV_EXPORTS_W BFMatcher : public DescriptorMatcher
1251
{
1252
public:
1253
    /** @brief Brute-force matcher constructor (obsolete). Please use BFMatcher.create()
1254
     *
1255
     *
1256
    */
1257
    CV_WRAP BFMatcher( int normType=NORM_L2, bool crossCheck=false );
1258
1259
0
    virtual ~BFMatcher() {}
1260
1261
0
    virtual bool isMaskSupported() const CV_OVERRIDE { return true; }
1262
1263
    /** @brief Brute-force matcher create method.
1264
    @param normType One of NORM_L1, NORM_L2, NORM_HAMMING, NORM_HAMMING2. L1 and L2 norms are
1265
    preferable choices for SIFT and SURF descriptors, NORM_HAMMING should be used with ORB, BRISK and
1266
    BRIEF, NORM_HAMMING2 should be used with ORB when WTA_K==3 or 4 (see ORB::ORB constructor
1267
    description).
1268
    @param crossCheck If it is false, this is will be default BFMatcher behaviour when it finds the k
1269
    nearest neighbors for each query descriptor. If crossCheck==true, then the knnMatch() method with
1270
    k=1 will only return pairs (i,j) such that for i-th query descriptor the j-th descriptor in the
1271
    matcher's collection is the nearest and vice versa, i.e. the BFMatcher will only return consistent
1272
    pairs. Such technique usually produces best results with minimal number of outliers when there are
1273
    enough matches. This is alternative to the ratio test, used by D. Lowe in SIFT paper.
1274
     */
1275
    CV_WRAP static Ptr<BFMatcher> create( int normType=NORM_L2, bool crossCheck=false ) ;
1276
1277
    CV_NODISCARD_STD virtual Ptr<DescriptorMatcher> clone( bool emptyTrainData=false ) const CV_OVERRIDE;
1278
protected:
1279
    virtual void knnMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, int k,
1280
        InputArrayOfArrays masks=noArray(), bool compactResult=false ) CV_OVERRIDE;
1281
    virtual void radiusMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance,
1282
        InputArrayOfArrays masks=noArray(), bool compactResult=false ) CV_OVERRIDE;
1283
1284
    int normType;
1285
    bool crossCheck;
1286
};
1287
1288
#if defined(HAVE_OPENCV_FLANN) || defined(CV_DOXYGEN)
1289
1290
/** @brief Flann-based descriptor matcher.
1291
1292
This matcher trains cv::flann::Index on a train descriptor collection and calls its nearest search
1293
methods to find the best matches. So, this matcher may be faster when matching a large train
1294
collection than the brute force matcher. FlannBasedMatcher does not support masking permissible
1295
matches of descriptor sets because flann::Index does not support this. :
1296
 */
1297
class CV_EXPORTS_W FlannBasedMatcher : public DescriptorMatcher
1298
{
1299
public:
1300
    CV_WRAP FlannBasedMatcher( const Ptr<flann::IndexParams>& indexParams=makePtr<flann::KDTreeIndexParams>(),
1301
                       const Ptr<flann::SearchParams>& searchParams=makePtr<flann::SearchParams>() );
1302
1303
    virtual void add( InputArrayOfArrays descriptors ) CV_OVERRIDE;
1304
    virtual void clear() CV_OVERRIDE;
1305
1306
    // Reads matcher object from a file node
1307
    virtual void read( const FileNode& ) CV_OVERRIDE;
1308
    // Writes matcher object to a file storage
1309
    virtual void write( FileStorage& ) const CV_OVERRIDE;
1310
1311
    virtual void train() CV_OVERRIDE;
1312
    virtual bool isMaskSupported() const CV_OVERRIDE;
1313
1314
    CV_WRAP static Ptr<FlannBasedMatcher> create();
1315
1316
    CV_NODISCARD_STD virtual Ptr<DescriptorMatcher> clone( bool emptyTrainData=false ) const CV_OVERRIDE;
1317
protected:
1318
    static void convertToDMatches( const DescriptorCollection& descriptors,
1319
                                   const Mat& indices, const Mat& distances,
1320
                                   std::vector<std::vector<DMatch> >& matches );
1321
1322
    virtual void knnMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, int k,
1323
        InputArrayOfArrays masks=noArray(), bool compactResult=false ) CV_OVERRIDE;
1324
    virtual void radiusMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance,
1325
        InputArrayOfArrays masks=noArray(), bool compactResult=false ) CV_OVERRIDE;
1326
1327
    Ptr<flann::IndexParams> indexParams;
1328
    Ptr<flann::SearchParams> searchParams;
1329
    Ptr<flann::Index> flannIndex;
1330
1331
    DescriptorCollection mergedDescriptors;
1332
    int addedDescCount;
1333
};
1334
1335
#endif
1336
1337
//! @} features2d_match
1338
1339
/****************************************************************************************\
1340
*                                   Drawing functions                                    *
1341
\****************************************************************************************/
1342
1343
//! @addtogroup features2d_draw
1344
//! @{
1345
1346
enum struct DrawMatchesFlags
1347
{
1348
  DEFAULT = 0, //!< Output image matrix will be created (Mat::create),
1349
               //!< i.e. existing memory of output image may be reused.
1350
               //!< Two source image, matches and single keypoints will be drawn.
1351
               //!< For each keypoint only the center point will be drawn (without
1352
               //!< the circle around keypoint with keypoint size and orientation).
1353
  DRAW_OVER_OUTIMG = 1, //!< Output image matrix will not be created (Mat::create).
1354
                        //!< Matches will be drawn on existing content of output image.
1355
  NOT_DRAW_SINGLE_POINTS = 2, //!< Single keypoints will not be drawn.
1356
  DRAW_RICH_KEYPOINTS = 4 //!< For each keypoint the circle around keypoint with keypoint size and
1357
                          //!< orientation will be drawn.
1358
};
1359
CV_ENUM_FLAGS(DrawMatchesFlags)
1360
1361
/** @brief Draws keypoints.
1362
1363
@param image Source image.
1364
@param keypoints Keypoints from the source image.
1365
@param outImage Output image. Its content depends on the flags value defining what is drawn in the
1366
output image. See possible flags bit values below.
1367
@param color Color of keypoints.
1368
@param flags Flags setting drawing features. Possible flags bit values are defined by
1369
DrawMatchesFlags. See details above in drawMatches .
1370
1371
@note
1372
For Python API, flags are modified as cv.DRAW_MATCHES_FLAGS_DEFAULT,
1373
cv.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS, cv.DRAW_MATCHES_FLAGS_DRAW_OVER_OUTIMG,
1374
cv.DRAW_MATCHES_FLAGS_NOT_DRAW_SINGLE_POINTS
1375
 */
1376
CV_EXPORTS_W void drawKeypoints( InputArray image, const std::vector<KeyPoint>& keypoints, InputOutputArray outImage,
1377
                               const Scalar& color=Scalar::all(-1), DrawMatchesFlags flags=DrawMatchesFlags::DEFAULT );
1378
1379
/** @brief Draws the found matches of keypoints from two images.
1380
1381
@param img1 First source image.
1382
@param keypoints1 Keypoints from the first source image.
1383
@param img2 Second source image.
1384
@param keypoints2 Keypoints from the second source image.
1385
@param matches1to2 Matches from the first image to the second one, which means that keypoints1[i]
1386
has a corresponding point in keypoints2[matches[i]] .
1387
@param outImg Output image. Its content depends on the flags value defining what is drawn in the
1388
output image. See possible flags bit values below.
1389
@param matchColor Color of matches (lines and connected keypoints). If matchColor==Scalar::all(-1)
1390
, the color is generated randomly.
1391
@param singlePointColor Color of single keypoints (circles), which means that keypoints do not
1392
have the matches. If singlePointColor==Scalar::all(-1) , the color is generated randomly.
1393
@param matchesMask Mask determining which matches are drawn. If the mask is empty, all matches are
1394
drawn.
1395
@param flags Flags setting drawing features. Possible flags bit values are defined by
1396
DrawMatchesFlags.
1397
1398
This function draws matches of keypoints from two images in the output image. Match is a line
1399
connecting two keypoints (circles). See cv::DrawMatchesFlags.
1400
 */
1401
CV_EXPORTS_W void drawMatches( InputArray img1, const std::vector<KeyPoint>& keypoints1,
1402
                             InputArray img2, const std::vector<KeyPoint>& keypoints2,
1403
                             const std::vector<DMatch>& matches1to2, InputOutputArray outImg,
1404
                             const Scalar& matchColor=Scalar::all(-1), const Scalar& singlePointColor=Scalar::all(-1),
1405
                             const std::vector<char>& matchesMask=std::vector<char>(), DrawMatchesFlags flags=DrawMatchesFlags::DEFAULT );
1406
1407
/** @overload */
1408
CV_EXPORTS_W void drawMatches( InputArray img1, const std::vector<KeyPoint>& keypoints1,
1409
                             InputArray img2, const std::vector<KeyPoint>& keypoints2,
1410
                             const std::vector<DMatch>& matches1to2, InputOutputArray outImg,
1411
                             const int matchesThickness, const Scalar& matchColor=Scalar::all(-1),
1412
                             const Scalar& singlePointColor=Scalar::all(-1), const std::vector<char>& matchesMask=std::vector<char>(),
1413
                             DrawMatchesFlags flags=DrawMatchesFlags::DEFAULT );
1414
1415
CV_EXPORTS_AS(drawMatchesKnn) void drawMatches( InputArray img1, const std::vector<KeyPoint>& keypoints1,
1416
                             InputArray img2, const std::vector<KeyPoint>& keypoints2,
1417
                             const std::vector<std::vector<DMatch> >& matches1to2, InputOutputArray outImg,
1418
                             const Scalar& matchColor=Scalar::all(-1), const Scalar& singlePointColor=Scalar::all(-1),
1419
                             const std::vector<std::vector<char> >& matchesMask=std::vector<std::vector<char> >(), DrawMatchesFlags flags=DrawMatchesFlags::DEFAULT );
1420
1421
//! @} features2d_draw
1422
1423
/****************************************************************************************\
1424
*   Functions to evaluate the feature detectors and [generic] descriptor extractors      *
1425
\****************************************************************************************/
1426
1427
CV_EXPORTS void evaluateFeatureDetector( const Mat& img1, const Mat& img2, const Mat& H1to2,
1428
                                         std::vector<KeyPoint>* keypoints1, std::vector<KeyPoint>* keypoints2,
1429
                                         float& repeatability, int& correspCount,
1430
                                         const Ptr<FeatureDetector>& fdetector=Ptr<FeatureDetector>() );
1431
1432
CV_EXPORTS void computeRecallPrecisionCurve( const std::vector<std::vector<DMatch> >& matches1to2,
1433
                                             const std::vector<std::vector<uchar> >& correctMatches1to2Mask,
1434
                                             std::vector<Point2f>& recallPrecisionCurve );
1435
1436
CV_EXPORTS float getRecall( const std::vector<Point2f>& recallPrecisionCurve, float l_precision );
1437
CV_EXPORTS int getNearestPoint( const std::vector<Point2f>& recallPrecisionCurve, float l_precision );
1438
1439
/****************************************************************************************\
1440
*                                     Bag of visual words                                *
1441
\****************************************************************************************/
1442
1443
//! @addtogroup features2d_category
1444
//! @{
1445
1446
/** @brief Abstract base class for training the *bag of visual words* vocabulary from a set of descriptors.
1447
1448
For details, see, for example, *Visual Categorization with Bags of Keypoints* by Gabriella Csurka,
1449
Christopher R. Dance, Lixin Fan, Jutta Willamowski, Cedric Bray, 2004. :
1450
 */
1451
class CV_EXPORTS_W BOWTrainer
1452
{
1453
public:
1454
    BOWTrainer();
1455
    virtual ~BOWTrainer();
1456
1457
    /** @brief Adds descriptors to a training set.
1458
1459
    @param descriptors Descriptors to add to a training set. Each row of the descriptors matrix is a
1460
    descriptor.
1461
1462
    The training set is clustered using clustermethod to construct the vocabulary.
1463
     */
1464
    CV_WRAP void add( const Mat& descriptors );
1465
1466
    /** @brief Returns a training set of descriptors.
1467
    */
1468
    CV_WRAP const std::vector<Mat>& getDescriptors() const;
1469
1470
    /** @brief Returns the count of all descriptors stored in the training set.
1471
    */
1472
    CV_WRAP int descriptorsCount() const;
1473
1474
    CV_WRAP virtual void clear();
1475
1476
    /** @overload */
1477
    CV_WRAP virtual Mat cluster() const = 0;
1478
1479
    /** @brief Clusters train descriptors.
1480
1481
    @param descriptors Descriptors to cluster. Each row of the descriptors matrix is a descriptor.
1482
    Descriptors are not added to the inner train descriptor set.
1483
1484
    The vocabulary consists of cluster centers. So, this method returns the vocabulary. In the first
1485
    variant of the method, train descriptors stored in the object are clustered. In the second variant,
1486
    input descriptors are clustered.
1487
     */
1488
    CV_WRAP virtual Mat cluster( const Mat& descriptors ) const = 0;
1489
1490
protected:
1491
    std::vector<Mat> descriptors;
1492
    int size;
1493
};
1494
1495
/** @brief kmeans -based class to train visual vocabulary using the *bag of visual words* approach. :
1496
 */
1497
class CV_EXPORTS_W BOWKMeansTrainer : public BOWTrainer
1498
{
1499
public:
1500
    /** @brief The constructor.
1501
1502
    @see cv::kmeans
1503
    */
1504
    CV_WRAP BOWKMeansTrainer( int clusterCount, const TermCriteria& termcrit=TermCriteria(),
1505
                      int attempts=3, int flags=KMEANS_PP_CENTERS );
1506
    virtual ~BOWKMeansTrainer();
1507
1508
    // Returns trained vocabulary (i.e. cluster centers).
1509
    CV_WRAP virtual Mat cluster() const CV_OVERRIDE;
1510
    CV_WRAP virtual Mat cluster( const Mat& descriptors ) const CV_OVERRIDE;
1511
1512
protected:
1513
1514
    int clusterCount;
1515
    TermCriteria termcrit;
1516
    int attempts;
1517
    int flags;
1518
};
1519
1520
/** @brief Class to compute an image descriptor using the *bag of visual words*.
1521
1522
Such a computation consists of the following steps:
1523
1524
1.  Compute descriptors for a given image and its keypoints set.
1525
2.  Find the nearest visual words from the vocabulary for each keypoint descriptor.
1526
3.  Compute the bag-of-words image descriptor as is a normalized histogram of vocabulary words
1527
encountered in the image. The i-th bin of the histogram is a frequency of i-th word of the
1528
vocabulary in the given image.
1529
 */
1530
class CV_EXPORTS_W BOWImgDescriptorExtractor
1531
{
1532
public:
1533
    /** @brief The constructor.
1534
1535
    @param dextractor Descriptor extractor that is used to compute descriptors for an input image and
1536
    its keypoints.
1537
    @param dmatcher Descriptor matcher that is used to find the nearest word of the trained vocabulary
1538
    for each keypoint descriptor of the image.
1539
     */
1540
    CV_WRAP BOWImgDescriptorExtractor( const Ptr<DescriptorExtractor>& dextractor,
1541
                               const Ptr<DescriptorMatcher>& dmatcher );
1542
    /** @overload */
1543
    BOWImgDescriptorExtractor( const Ptr<DescriptorMatcher>& dmatcher );
1544
    virtual ~BOWImgDescriptorExtractor();
1545
1546
    /** @brief Sets a visual vocabulary.
1547
1548
    @param vocabulary Vocabulary (can be trained using the inheritor of BOWTrainer ). Each row of the
1549
    vocabulary is a visual word (cluster center).
1550
     */
1551
    CV_WRAP void setVocabulary( const Mat& vocabulary );
1552
1553
    /** @brief Returns the set vocabulary.
1554
    */
1555
    CV_WRAP const Mat& getVocabulary() const;
1556
1557
    /** @brief Computes an image descriptor using the set visual vocabulary.
1558
1559
    @param image Image, for which the descriptor is computed.
1560
    @param keypoints Keypoints detected in the input image.
1561
    @param imgDescriptor Computed output image descriptor.
1562
    @param pointIdxsOfClusters Indices of keypoints that belong to the cluster. This means that
1563
    pointIdxsOfClusters[i] are keypoint indices that belong to the i -th cluster (word of vocabulary)
1564
    returned if it is non-zero.
1565
    @param descriptors Descriptors of the image keypoints that are returned if they are non-zero.
1566
     */
1567
    void compute( InputArray image, std::vector<KeyPoint>& keypoints, OutputArray imgDescriptor,
1568
                  std::vector<std::vector<int> >* pointIdxsOfClusters=0, Mat* descriptors=0 );
1569
    /** @overload
1570
    @param keypointDescriptors Computed descriptors to match with vocabulary.
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
    */
1576
    void compute( InputArray keypointDescriptors, OutputArray imgDescriptor,
1577
                  std::vector<std::vector<int> >* pointIdxsOfClusters=0 );
1578
    // compute() is not constant because DescriptorMatcher::match is not constant
1579
1580
    CV_WRAP_AS(compute) void compute2( const Mat& image, std::vector<KeyPoint>& keypoints, CV_OUT Mat& imgDescriptor )
1581
0
    { compute(image,keypoints,imgDescriptor); }
1582
1583
    /** @brief Returns an image descriptor size if the vocabulary is set. Otherwise, it returns 0.
1584
    */
1585
    CV_WRAP int descriptorSize() const;
1586
1587
    /** @brief Returns an image descriptor type.
1588
     */
1589
    CV_WRAP int descriptorType() const;
1590
1591
protected:
1592
    Mat vocabulary;
1593
    Ptr<DescriptorExtractor> dextractor;
1594
    Ptr<DescriptorMatcher> dmatcher;
1595
};
1596
1597
//! @} features2d_category
1598
1599
//! @} features2d
1600
1601
} /* namespace cv */
1602
1603
#endif