Coverage Report

Created: 2025-07-23 07:06

/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
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 gradiantSize, 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
      CV_PROP_RW bool collectContours;
773
774
      void read( const FileNode& fn );
775
      void write( FileStorage& fs ) const;
776
  };
777
778
  CV_WRAP static Ptr<SimpleBlobDetector>
779
    create(const SimpleBlobDetector::Params &parameters = SimpleBlobDetector::Params());
780
781
  CV_WRAP virtual void setParams(const SimpleBlobDetector::Params& params ) = 0;
782
  CV_WRAP virtual SimpleBlobDetector::Params getParams() const = 0;
783
784
  CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
785
  CV_WRAP virtual const std::vector<std::vector<cv::Point> >& getBlobContours() const;
786
};
787
788
789
/** @brief Class implementing the KAZE keypoint detector and descriptor extractor, described in @cite ABD12 .
790
791
@note AKAZE descriptor can only be used with KAZE or AKAZE keypoints .. [ABD12] KAZE Features. Pablo
792
F. Alcantarilla, Adrien Bartoli and Andrew J. Davison. In European Conference on Computer Vision
793
(ECCV), Fiorenze, Italy, October 2012.
794
*/
795
class CV_EXPORTS_W KAZE : public Feature2D
796
{
797
public:
798
    enum DiffusivityType
799
    {
800
        DIFF_PM_G1 = 0,
801
        DIFF_PM_G2 = 1,
802
        DIFF_WEICKERT = 2,
803
        DIFF_CHARBONNIER = 3
804
    };
805
806
    /** @brief The KAZE constructor
807
808
    @param extended Set to enable extraction of extended (128-byte) descriptor.
809
    @param upright Set to enable use of upright descriptors (non rotation-invariant).
810
    @param threshold Detector response threshold to accept point
811
    @param nOctaves Maximum octave evolution of the image
812
    @param nOctaveLayers Default number of sublevels per scale level
813
    @param diffusivity Diffusivity type. DIFF_PM_G1, DIFF_PM_G2, DIFF_WEICKERT or
814
    DIFF_CHARBONNIER
815
     */
816
    CV_WRAP static Ptr<KAZE> create(bool extended=false, bool upright=false,
817
                                    float threshold = 0.001f,
818
                                    int nOctaves = 4, int nOctaveLayers = 4,
819
                                    KAZE::DiffusivityType diffusivity = KAZE::DIFF_PM_G2);
820
821
    CV_WRAP virtual void setExtended(bool extended) = 0;
822
    CV_WRAP virtual bool getExtended() const = 0;
823
824
    CV_WRAP virtual void setUpright(bool upright) = 0;
825
    CV_WRAP virtual bool getUpright() const = 0;
826
827
    CV_WRAP virtual void setThreshold(double threshold) = 0;
828
    CV_WRAP virtual double getThreshold() const = 0;
829
830
    CV_WRAP virtual void setNOctaves(int octaves) = 0;
831
    CV_WRAP virtual int getNOctaves() const = 0;
832
833
    CV_WRAP virtual void setNOctaveLayers(int octaveLayers) = 0;
834
    CV_WRAP virtual int getNOctaveLayers() const = 0;
835
836
    CV_WRAP virtual void setDiffusivity(KAZE::DiffusivityType diff) = 0;
837
    CV_WRAP virtual KAZE::DiffusivityType getDiffusivity() const = 0;
838
    CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
839
};
840
841
/** @brief Class implementing the AKAZE keypoint detector and descriptor extractor, described in @cite ANB13.
842
843
@details AKAZE descriptors can only be used with KAZE or AKAZE keypoints. This class is thread-safe.
844
845
@note When you need descriptors use Feature2D::detectAndCompute, which
846
provides better performance. When using Feature2D::detect followed by
847
Feature2D::compute scale space pyramid is computed twice.
848
849
@note AKAZE implements T-API. When image is passed as UMat some parts of the algorithm
850
will use OpenCL.
851
852
@note [ANB13] Fast Explicit Diffusion for Accelerated Features in Nonlinear
853
Scale Spaces. Pablo F. Alcantarilla, Jesús Nuevo and Adrien Bartoli. In
854
British Machine Vision Conference (BMVC), Bristol, UK, September 2013.
855
856
*/
857
class CV_EXPORTS_W AKAZE : public Feature2D
858
{
859
public:
860
    // AKAZE descriptor type
861
    enum DescriptorType
862
    {
863
        DESCRIPTOR_KAZE_UPRIGHT = 2, ///< Upright descriptors, not invariant to rotation
864
        DESCRIPTOR_KAZE = 3,
865
        DESCRIPTOR_MLDB_UPRIGHT = 4, ///< Upright descriptors, not invariant to rotation
866
        DESCRIPTOR_MLDB = 5
867
    };
868
869
    /** @brief The AKAZE constructor
870
871
    @param descriptor_type Type of the extracted descriptor: DESCRIPTOR_KAZE,
872
    DESCRIPTOR_KAZE_UPRIGHT, DESCRIPTOR_MLDB or DESCRIPTOR_MLDB_UPRIGHT.
873
    @param descriptor_size Size of the descriptor in bits. 0 -\> Full size
874
    @param descriptor_channels Number of channels in the descriptor (1, 2, 3)
875
    @param threshold Detector response threshold to accept point
876
    @param nOctaves Maximum octave evolution of the image
877
    @param nOctaveLayers Default number of sublevels per scale level
878
    @param diffusivity Diffusivity type. DIFF_PM_G1, DIFF_PM_G2, DIFF_WEICKERT or
879
    DIFF_CHARBONNIER
880
    @param max_points Maximum amount of returned points. In case if image contains
881
    more features, then the features with highest response are returned.
882
    Negative value means no limitation.
883
     */
884
    CV_WRAP static Ptr<AKAZE> create(AKAZE::DescriptorType descriptor_type = AKAZE::DESCRIPTOR_MLDB,
885
                                     int descriptor_size = 0, int descriptor_channels = 3,
886
                                     float threshold = 0.001f, int nOctaves = 4,
887
                                     int nOctaveLayers = 4, KAZE::DiffusivityType diffusivity = KAZE::DIFF_PM_G2,
888
                                     int max_points = -1);
889
890
    CV_WRAP virtual void setDescriptorType(AKAZE::DescriptorType dtype) = 0;
891
    CV_WRAP virtual AKAZE::DescriptorType getDescriptorType() const = 0;
892
893
    CV_WRAP virtual void setDescriptorSize(int dsize) = 0;
894
    CV_WRAP virtual int getDescriptorSize() const = 0;
895
896
    CV_WRAP virtual void setDescriptorChannels(int dch) = 0;
897
    CV_WRAP virtual int getDescriptorChannels() const = 0;
898
899
    CV_WRAP virtual void setThreshold(double threshold) = 0;
900
    CV_WRAP virtual double getThreshold() const = 0;
901
902
    CV_WRAP virtual void setNOctaves(int octaves) = 0;
903
    CV_WRAP virtual int getNOctaves() const = 0;
904
905
    CV_WRAP virtual void setNOctaveLayers(int octaveLayers) = 0;
906
    CV_WRAP virtual int getNOctaveLayers() const = 0;
907
908
    CV_WRAP virtual void setDiffusivity(KAZE::DiffusivityType diff) = 0;
909
    CV_WRAP virtual KAZE::DiffusivityType getDiffusivity() const = 0;
910
    CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
911
912
    CV_WRAP virtual void setMaxPoints(int max_points) = 0;
913
    CV_WRAP virtual int getMaxPoints() const = 0;
914
};
915
916
917
/****************************************************************************************\
918
*                                      Distance                                          *
919
\****************************************************************************************/
920
921
template<typename T>
922
struct CV_EXPORTS Accumulator
923
{
924
    typedef T Type;
925
};
926
927
template<> struct Accumulator<unsigned char>  { typedef float Type; };
928
template<> struct Accumulator<unsigned short> { typedef float Type; };
929
template<> struct Accumulator<char>   { typedef float Type; };
930
template<> struct Accumulator<short>  { typedef float Type; };
931
932
/*
933
 * Squared Euclidean distance functor
934
 */
935
template<class T>
936
struct CV_EXPORTS SL2
937
{
938
    static const NormTypes normType = NORM_L2SQR;
939
    typedef T ValueType;
940
    typedef typename Accumulator<T>::Type ResultType;
941
942
    ResultType operator()( const T* a, const T* b, int size ) const
943
    {
944
        return normL2Sqr<ValueType, ResultType>(a, b, size);
945
    }
946
};
947
948
/*
949
 * Euclidean distance functor
950
 */
951
template<class T>
952
struct L2
953
{
954
    static const NormTypes normType = NORM_L2;
955
    typedef T ValueType;
956
    typedef typename Accumulator<T>::Type ResultType;
957
958
    ResultType operator()( const T* a, const T* b, int size ) const
959
    {
960
        return (ResultType)std::sqrt((double)normL2Sqr<ValueType, ResultType>(a, b, size));
961
    }
962
};
963
964
/*
965
 * Manhattan distance (city block distance) functor
966
 */
967
template<class T>
968
struct L1
969
{
970
    static const NormTypes normType = NORM_L1;
971
    typedef T ValueType;
972
    typedef typename Accumulator<T>::Type ResultType;
973
974
    ResultType operator()( const T* a, const T* b, int size ) const
975
    {
976
        return normL1<ValueType, ResultType>(a, b, size);
977
    }
978
};
979
980
//! @} features2d_main
981
982
/****************************************************************************************\
983
*                                  DescriptorMatcher                                     *
984
\****************************************************************************************/
985
986
//! @addtogroup features2d_match
987
//! @{
988
989
/** @brief Abstract base class for matching keypoint descriptors.
990
991
It has two groups of match methods: for matching descriptors of an image with another image or with
992
an image set.
993
 */
994
class CV_EXPORTS_W DescriptorMatcher : public Algorithm
995
{
996
public:
997
   enum MatcherType
998
    {
999
        FLANNBASED            = 1,
1000
        BRUTEFORCE            = 2,
1001
        BRUTEFORCE_L1         = 3,
1002
        BRUTEFORCE_HAMMING    = 4,
1003
        BRUTEFORCE_HAMMINGLUT = 5,
1004
        BRUTEFORCE_SL2        = 6
1005
    };
1006
1007
    virtual ~DescriptorMatcher();
1008
1009
    /** @brief Adds descriptors to train a CPU(trainDescCollectionis) or GPU(utrainDescCollectionis) descriptor
1010
    collection.
1011
1012
    If the collection is not empty, the new descriptors are added to existing train descriptors.
1013
1014
    @param descriptors Descriptors to add. Each descriptors[i] is a set of descriptors from the same
1015
    train image.
1016
     */
1017
    CV_WRAP virtual void add( InputArrayOfArrays descriptors );
1018
1019
    /** @brief Returns a constant link to the train descriptor collection trainDescCollection .
1020
     */
1021
    CV_WRAP const std::vector<Mat>& getTrainDescriptors() const;
1022
1023
    /** @brief Clears the train descriptor collections.
1024
     */
1025
    CV_WRAP virtual void clear() CV_OVERRIDE;
1026
1027
    /** @brief Returns true if there are no train descriptors in the both collections.
1028
     */
1029
    CV_WRAP virtual bool empty() const CV_OVERRIDE;
1030
1031
    /** @brief Returns true if the descriptor matcher supports masking permissible matches.
1032
     */
1033
    CV_WRAP virtual bool isMaskSupported() const = 0;
1034
1035
    /** @brief Trains a descriptor matcher
1036
1037
    Trains a descriptor matcher (for example, the flann index). In all methods to match, the method
1038
    train() is run every time before matching. Some descriptor matchers (for example, BruteForceMatcher)
1039
    have an empty implementation of this method. Other matchers really train their inner structures (for
1040
    example, FlannBasedMatcher trains flann::Index ).
1041
     */
1042
    CV_WRAP virtual void train();
1043
1044
    /** @brief Finds the best match for each descriptor from a query set.
1045
1046
    @param queryDescriptors Query set of descriptors.
1047
    @param trainDescriptors Train set of descriptors. This set is not added to the train descriptors
1048
    collection stored in the class object.
1049
    @param matches Matches. If a query descriptor is masked out in mask , no match is added for this
1050
    descriptor. So, matches size may be smaller than the query descriptors count.
1051
    @param mask Mask specifying permissible matches between an input query and train matrices of
1052
    descriptors.
1053
1054
    In the first variant of this method, the train descriptors are passed as an input argument. In the
1055
    second variant of the method, train descriptors collection that was set by DescriptorMatcher::add is
1056
    used. Optional mask (or masks) can be passed to specify which query and training descriptors can be
1057
    matched. Namely, queryDescriptors[i] can be matched with trainDescriptors[j] only if
1058
    mask.at\<uchar\>(i,j) is non-zero.
1059
     */
1060
    CV_WRAP void match( InputArray queryDescriptors, InputArray trainDescriptors,
1061
                CV_OUT std::vector<DMatch>& matches, InputArray mask=noArray() ) const;
1062
1063
    /** @brief Finds the k best matches for each descriptor from a query set.
1064
1065
    @param queryDescriptors Query set of descriptors.
1066
    @param trainDescriptors Train set of descriptors. This set is not added to the train descriptors
1067
    collection stored in the class object.
1068
    @param mask Mask specifying permissible matches between an input query and train matrices of
1069
    descriptors.
1070
    @param matches Matches. Each matches[i] is k or less matches for the same query descriptor.
1071
    @param k Count of best matches found per each query descriptor or less if a query descriptor has
1072
    less than k possible matches in total.
1073
    @param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
1074
    false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
1075
    the matches vector does not contain matches for fully masked-out query descriptors.
1076
1077
    These extended variants of DescriptorMatcher::match methods find several best matches for each query
1078
    descriptor. The matches are returned in the distance increasing order. See DescriptorMatcher::match
1079
    for the details about query and train descriptors.
1080
     */
1081
    CV_WRAP void knnMatch( InputArray queryDescriptors, InputArray trainDescriptors,
1082
                   CV_OUT std::vector<std::vector<DMatch> >& matches, int k,
1083
                   InputArray mask=noArray(), bool compactResult=false ) const;
1084
1085
    /** @brief For each query descriptor, finds the training descriptors not farther than the specified distance.
1086
1087
    @param queryDescriptors Query set of descriptors.
1088
    @param trainDescriptors Train set of descriptors. This set is not added to the train descriptors
1089
    collection stored in the class object.
1090
    @param matches Found matches.
1091
    @param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
1092
    false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
1093
    the matches vector does not contain matches for fully masked-out query descriptors.
1094
    @param maxDistance Threshold for the distance between matched descriptors. Distance means here
1095
    metric distance (e.g. Hamming distance), not the distance between coordinates (which is measured
1096
    in Pixels)!
1097
    @param mask Mask specifying permissible matches between an input query and train matrices of
1098
    descriptors.
1099
1100
    For each query descriptor, the methods find such training descriptors that the distance between the
1101
    query descriptor and the training descriptor is equal or smaller than maxDistance. Found matches are
1102
    returned in the distance increasing order.
1103
     */
1104
    CV_WRAP void radiusMatch( InputArray queryDescriptors, InputArray trainDescriptors,
1105
                      CV_OUT std::vector<std::vector<DMatch> >& matches, float maxDistance,
1106
                      InputArray mask=noArray(), bool compactResult=false ) const;
1107
1108
    /** @overload
1109
    @param queryDescriptors Query set of descriptors.
1110
    @param matches Matches. If a query descriptor is masked out in mask , no match is added for this
1111
    descriptor. So, matches size may be smaller than the query descriptors count.
1112
    @param masks Set of masks. Each masks[i] specifies permissible matches between the input query
1113
    descriptors and stored train descriptors from the i-th image trainDescCollection[i].
1114
    */
1115
    CV_WRAP void match( InputArray queryDescriptors, CV_OUT std::vector<DMatch>& matches,
1116
                        InputArrayOfArrays masks=noArray() );
1117
    /** @overload
1118
    @param queryDescriptors Query set of descriptors.
1119
    @param matches Matches. Each matches[i] is k or less matches for the same query descriptor.
1120
    @param k Count of best matches found per each query descriptor or less if a query descriptor has
1121
    less than k possible matches in total.
1122
    @param masks Set of masks. Each masks[i] specifies permissible matches between the input query
1123
    descriptors and stored train descriptors from the i-th image trainDescCollection[i].
1124
    @param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
1125
    false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
1126
    the matches vector does not contain matches for fully masked-out query descriptors.
1127
    */
1128
    CV_WRAP void knnMatch( InputArray queryDescriptors, CV_OUT std::vector<std::vector<DMatch> >& matches, int k,
1129
                           InputArrayOfArrays masks=noArray(), bool compactResult=false );
1130
    /** @overload
1131
    @param queryDescriptors Query set of descriptors.
1132
    @param matches Found matches.
1133
    @param maxDistance Threshold for the distance between matched descriptors. Distance means here
1134
    metric distance (e.g. Hamming distance), not the distance between coordinates (which is measured
1135
    in Pixels)!
1136
    @param masks Set of masks. Each masks[i] specifies permissible matches between the input query
1137
    descriptors and stored train descriptors from the i-th image trainDescCollection[i].
1138
    @param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
1139
    false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
1140
    the matches vector does not contain matches for fully masked-out query descriptors.
1141
    */
1142
    CV_WRAP void radiusMatch( InputArray queryDescriptors, CV_OUT std::vector<std::vector<DMatch> >& matches, float maxDistance,
1143
                      InputArrayOfArrays masks=noArray(), bool compactResult=false );
1144
1145
1146
    CV_WRAP void write( const String& fileName ) const
1147
0
    {
1148
0
        FileStorage fs(fileName, FileStorage::WRITE);
1149
0
        write(fs);
1150
0
    }
1151
1152
    CV_WRAP void read( const String& fileName )
1153
0
    {
1154
0
        FileStorage fs(fileName, FileStorage::READ);
1155
0
        read(fs.root());
1156
0
    }
1157
    // Reads matcher object from a file node
1158
    // see corresponding cv::Algorithm method
1159
    CV_WRAP virtual void read( const FileNode& ) CV_OVERRIDE;
1160
    // Writes matcher object to a file storage
1161
    virtual void write( FileStorage& ) const CV_OVERRIDE;
1162
1163
    /** @brief Clones the matcher.
1164
1165
    @param emptyTrainData If emptyTrainData is false, the method creates a deep copy of the object,
1166
    that is, copies both parameters and train data. If emptyTrainData is true, the method creates an
1167
    object copy with the current parameters but with empty train data.
1168
     */
1169
    CV_WRAP CV_NODISCARD_STD virtual Ptr<DescriptorMatcher> clone( bool emptyTrainData=false ) const = 0;
1170
1171
    /** @brief Creates a descriptor matcher of a given type with the default parameters (using default
1172
    constructor).
1173
1174
    @param descriptorMatcherType Descriptor matcher type. Now the following matcher types are
1175
    supported:
1176
    -   `BruteForce` (it uses L2 )
1177
    -   `BruteForce-L1`
1178
    -   `BruteForce-Hamming`
1179
    -   `BruteForce-Hamming(2)`
1180
    -   `FlannBased`
1181
     */
1182
    CV_WRAP static Ptr<DescriptorMatcher> create( const String& descriptorMatcherType );
1183
1184
    CV_WRAP static Ptr<DescriptorMatcher> create( const DescriptorMatcher::MatcherType& matcherType );
1185
1186
1187
    // see corresponding cv::Algorithm method
1188
0
    CV_WRAP inline void write(FileStorage& fs, const String& name) const { Algorithm::write(fs, name); }
1189
#if CV_VERSION_MAJOR < 5
1190
0
    inline void write(const Ptr<FileStorage>& fs, const String& name) const { CV_Assert(fs); Algorithm::write(*fs, name); }
1191
#endif
1192
1193
protected:
1194
    /**
1195
     * Class to work with descriptors from several images as with one merged matrix.
1196
     * It is used e.g. in FlannBasedMatcher.
1197
     */
1198
    class CV_EXPORTS DescriptorCollection
1199
    {
1200
    public:
1201
        DescriptorCollection();
1202
        DescriptorCollection( const DescriptorCollection& collection );
1203
        virtual ~DescriptorCollection();
1204
1205
        // Vector of matrices "descriptors" will be merged to one matrix "mergedDescriptors" here.
1206
        void set( const std::vector<Mat>& descriptors );
1207
        virtual void clear();
1208
1209
        const Mat& getDescriptors() const;
1210
        Mat getDescriptor( int imgIdx, int localDescIdx ) const;
1211
        Mat getDescriptor( int globalDescIdx ) const;
1212
        void getLocalIdx( int globalDescIdx, int& imgIdx, int& localDescIdx ) const;
1213
1214
        int size() const;
1215
1216
    protected:
1217
        Mat mergedDescriptors;
1218
        std::vector<int> startIdxs;
1219
    };
1220
1221
    //! In fact the matching is implemented only by the following two methods. These methods suppose
1222
    //! that the class object has been trained already. Public match methods call these methods
1223
    //! after calling train().
1224
    virtual void knnMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, int k,
1225
        InputArrayOfArrays masks=noArray(), bool compactResult=false ) = 0;
1226
    virtual void radiusMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance,
1227
        InputArrayOfArrays masks=noArray(), bool compactResult=false ) = 0;
1228
1229
    static bool isPossibleMatch( InputArray mask, int queryIdx, int trainIdx );
1230
    static bool isMaskedOut( InputArrayOfArrays masks, int queryIdx );
1231
1232
0
    CV_NODISCARD_STD static Mat clone_op( Mat m ) { return m.clone(); }
1233
    void checkMasks( InputArrayOfArrays masks, int queryDescriptorsCount ) const;
1234
1235
    //! Collection of descriptors from train images.
1236
    std::vector<Mat> trainDescCollection;
1237
    std::vector<UMat> utrainDescCollection;
1238
};
1239
1240
/** @brief Brute-force descriptor matcher.
1241
1242
For each descriptor in the first set, this matcher finds the closest descriptor in the second set
1243
by trying each one. This descriptor matcher supports masking permissible matches of descriptor
1244
sets.
1245
 */
1246
class CV_EXPORTS_W BFMatcher : public DescriptorMatcher
1247
{
1248
public:
1249
    /** @brief Brute-force matcher constructor (obsolete). Please use BFMatcher.create()
1250
     *
1251
     *
1252
    */
1253
    CV_WRAP BFMatcher( int normType=NORM_L2, bool crossCheck=false );
1254
1255
0
    virtual ~BFMatcher() {}
1256
1257
0
    virtual bool isMaskSupported() const CV_OVERRIDE { return true; }
1258
1259
    /** @brief Brute-force matcher create method.
1260
    @param normType One of NORM_L1, NORM_L2, NORM_HAMMING, NORM_HAMMING2. L1 and L2 norms are
1261
    preferable choices for SIFT and SURF descriptors, NORM_HAMMING should be used with ORB, BRISK and
1262
    BRIEF, NORM_HAMMING2 should be used with ORB when WTA_K==3 or 4 (see ORB::ORB constructor
1263
    description).
1264
    @param crossCheck If it is false, this is will be default BFMatcher behaviour when it finds the k
1265
    nearest neighbors for each query descriptor. If crossCheck==true, then the knnMatch() method with
1266
    k=1 will only return pairs (i,j) such that for i-th query descriptor the j-th descriptor in the
1267
    matcher's collection is the nearest and vice versa, i.e. the BFMatcher will only return consistent
1268
    pairs. Such technique usually produces best results with minimal number of outliers when there are
1269
    enough matches. This is alternative to the ratio test, used by D. Lowe in SIFT paper.
1270
     */
1271
    CV_WRAP static Ptr<BFMatcher> create( int normType=NORM_L2, bool crossCheck=false ) ;
1272
1273
    CV_NODISCARD_STD virtual Ptr<DescriptorMatcher> clone( bool emptyTrainData=false ) const CV_OVERRIDE;
1274
protected:
1275
    virtual void knnMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, int k,
1276
        InputArrayOfArrays masks=noArray(), bool compactResult=false ) CV_OVERRIDE;
1277
    virtual void radiusMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance,
1278
        InputArrayOfArrays masks=noArray(), bool compactResult=false ) CV_OVERRIDE;
1279
1280
    int normType;
1281
    bool crossCheck;
1282
};
1283
1284
#if defined(HAVE_OPENCV_FLANN) || defined(CV_DOXYGEN)
1285
1286
/** @brief Flann-based descriptor matcher.
1287
1288
This matcher trains cv::flann::Index on a train descriptor collection and calls its nearest search
1289
methods to find the best matches. So, this matcher may be faster when matching a large train
1290
collection than the brute force matcher. FlannBasedMatcher does not support masking permissible
1291
matches of descriptor sets because flann::Index does not support this. :
1292
 */
1293
class CV_EXPORTS_W FlannBasedMatcher : public DescriptorMatcher
1294
{
1295
public:
1296
    CV_WRAP FlannBasedMatcher( const Ptr<flann::IndexParams>& indexParams=makePtr<flann::KDTreeIndexParams>(),
1297
                       const Ptr<flann::SearchParams>& searchParams=makePtr<flann::SearchParams>() );
1298
1299
    virtual void add( InputArrayOfArrays descriptors ) CV_OVERRIDE;
1300
    virtual void clear() CV_OVERRIDE;
1301
1302
    // Reads matcher object from a file node
1303
    virtual void read( const FileNode& ) CV_OVERRIDE;
1304
    // Writes matcher object to a file storage
1305
    virtual void write( FileStorage& ) const CV_OVERRIDE;
1306
1307
    virtual void train() CV_OVERRIDE;
1308
    virtual bool isMaskSupported() const CV_OVERRIDE;
1309
1310
    CV_WRAP static Ptr<FlannBasedMatcher> create();
1311
1312
    CV_NODISCARD_STD virtual Ptr<DescriptorMatcher> clone( bool emptyTrainData=false ) const CV_OVERRIDE;
1313
protected:
1314
    static void convertToDMatches( const DescriptorCollection& descriptors,
1315
                                   const Mat& indices, const Mat& distances,
1316
                                   std::vector<std::vector<DMatch> >& matches );
1317
1318
    virtual void knnMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, int k,
1319
        InputArrayOfArrays masks=noArray(), bool compactResult=false ) CV_OVERRIDE;
1320
    virtual void radiusMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance,
1321
        InputArrayOfArrays masks=noArray(), bool compactResult=false ) CV_OVERRIDE;
1322
1323
    Ptr<flann::IndexParams> indexParams;
1324
    Ptr<flann::SearchParams> searchParams;
1325
    Ptr<flann::Index> flannIndex;
1326
1327
    DescriptorCollection mergedDescriptors;
1328
    int addedDescCount;
1329
};
1330
1331
#endif
1332
1333
//! @} features2d_match
1334
1335
/****************************************************************************************\
1336
*                                   Drawing functions                                    *
1337
\****************************************************************************************/
1338
1339
//! @addtogroup features2d_draw
1340
//! @{
1341
1342
enum struct DrawMatchesFlags
1343
{
1344
  DEFAULT = 0, //!< Output image matrix will be created (Mat::create),
1345
               //!< i.e. existing memory of output image may be reused.
1346
               //!< Two source image, matches and single keypoints will be drawn.
1347
               //!< For each keypoint only the center point will be drawn (without
1348
               //!< the circle around keypoint with keypoint size and orientation).
1349
  DRAW_OVER_OUTIMG = 1, //!< Output image matrix will not be created (Mat::create).
1350
                        //!< Matches will be drawn on existing content of output image.
1351
  NOT_DRAW_SINGLE_POINTS = 2, //!< Single keypoints will not be drawn.
1352
  DRAW_RICH_KEYPOINTS = 4 //!< For each keypoint the circle around keypoint with keypoint size and
1353
                          //!< orientation will be drawn.
1354
};
1355
CV_ENUM_FLAGS(DrawMatchesFlags)
1356
1357
/** @brief Draws keypoints.
1358
1359
@param image Source image.
1360
@param keypoints Keypoints from the source image.
1361
@param outImage Output image. Its content depends on the flags value defining what is drawn in the
1362
output image. See possible flags bit values below.
1363
@param color Color of keypoints.
1364
@param flags Flags setting drawing features. Possible flags bit values are defined by
1365
DrawMatchesFlags. See details above in drawMatches .
1366
1367
@note
1368
For Python API, flags are modified as cv.DRAW_MATCHES_FLAGS_DEFAULT,
1369
cv.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS, cv.DRAW_MATCHES_FLAGS_DRAW_OVER_OUTIMG,
1370
cv.DRAW_MATCHES_FLAGS_NOT_DRAW_SINGLE_POINTS
1371
 */
1372
CV_EXPORTS_W void drawKeypoints( InputArray image, const std::vector<KeyPoint>& keypoints, InputOutputArray outImage,
1373
                               const Scalar& color=Scalar::all(-1), DrawMatchesFlags flags=DrawMatchesFlags::DEFAULT );
1374
1375
/** @brief Draws the found matches of keypoints from two images.
1376
1377
@param img1 First source image.
1378
@param keypoints1 Keypoints from the first source image.
1379
@param img2 Second source image.
1380
@param keypoints2 Keypoints from the second source image.
1381
@param matches1to2 Matches from the first image to the second one, which means that keypoints1[i]
1382
has a corresponding point in keypoints2[matches[i]] .
1383
@param outImg Output image. Its content depends on the flags value defining what is drawn in the
1384
output image. See possible flags bit values below.
1385
@param matchColor Color of matches (lines and connected keypoints). If matchColor==Scalar::all(-1)
1386
, the color is generated randomly.
1387
@param singlePointColor Color of single keypoints (circles), which means that keypoints do not
1388
have the matches. If singlePointColor==Scalar::all(-1) , the color is generated randomly.
1389
@param matchesMask Mask determining which matches are drawn. If the mask is empty, all matches are
1390
drawn.
1391
@param flags Flags setting drawing features. Possible flags bit values are defined by
1392
DrawMatchesFlags.
1393
1394
This function draws matches of keypoints from two images in the output image. Match is a line
1395
connecting two keypoints (circles). See cv::DrawMatchesFlags.
1396
 */
1397
CV_EXPORTS_W void drawMatches( InputArray img1, const std::vector<KeyPoint>& keypoints1,
1398
                             InputArray img2, const std::vector<KeyPoint>& keypoints2,
1399
                             const std::vector<DMatch>& matches1to2, InputOutputArray outImg,
1400
                             const Scalar& matchColor=Scalar::all(-1), const Scalar& singlePointColor=Scalar::all(-1),
1401
                             const std::vector<char>& matchesMask=std::vector<char>(), DrawMatchesFlags flags=DrawMatchesFlags::DEFAULT );
1402
1403
/** @overload */
1404
CV_EXPORTS_W void drawMatches( InputArray img1, const std::vector<KeyPoint>& keypoints1,
1405
                             InputArray img2, const std::vector<KeyPoint>& keypoints2,
1406
                             const std::vector<DMatch>& matches1to2, InputOutputArray outImg,
1407
                             const int matchesThickness, const Scalar& matchColor=Scalar::all(-1),
1408
                             const Scalar& singlePointColor=Scalar::all(-1), const std::vector<char>& matchesMask=std::vector<char>(),
1409
                             DrawMatchesFlags flags=DrawMatchesFlags::DEFAULT );
1410
1411
CV_EXPORTS_AS(drawMatchesKnn) void drawMatches( InputArray img1, const std::vector<KeyPoint>& keypoints1,
1412
                             InputArray img2, const std::vector<KeyPoint>& keypoints2,
1413
                             const std::vector<std::vector<DMatch> >& matches1to2, InputOutputArray outImg,
1414
                             const Scalar& matchColor=Scalar::all(-1), const Scalar& singlePointColor=Scalar::all(-1),
1415
                             const std::vector<std::vector<char> >& matchesMask=std::vector<std::vector<char> >(), DrawMatchesFlags flags=DrawMatchesFlags::DEFAULT );
1416
1417
//! @} features2d_draw
1418
1419
/****************************************************************************************\
1420
*   Functions to evaluate the feature detectors and [generic] descriptor extractors      *
1421
\****************************************************************************************/
1422
1423
//! @addtogroup features2d_main
1424
//! @{
1425
1426
CV_EXPORTS void evaluateFeatureDetector( const Mat& img1, const Mat& img2, const Mat& H1to2,
1427
                                         std::vector<KeyPoint>* keypoints1, std::vector<KeyPoint>* keypoints2,
1428
                                         float& repeatability, int& correspCount,
1429
                                         const Ptr<FeatureDetector>& fdetector=Ptr<FeatureDetector>() );
1430
1431
CV_EXPORTS void computeRecallPrecisionCurve( const std::vector<std::vector<DMatch> >& matches1to2,
1432
                                             const std::vector<std::vector<uchar> >& correctMatches1to2Mask,
1433
                                             std::vector<Point2f>& recallPrecisionCurve );
1434
1435
CV_EXPORTS float getRecall( const std::vector<Point2f>& recallPrecisionCurve, float l_precision );
1436
CV_EXPORTS int getNearestPoint( const std::vector<Point2f>& recallPrecisionCurve, float l_precision );
1437
1438
//! @}
1439
1440
/****************************************************************************************\
1441
*                                     Bag of visual words                                *
1442
\****************************************************************************************/
1443
1444
//! @addtogroup features2d_category
1445
//! @{
1446
1447
/** @brief Abstract base class for training the *bag of visual words* vocabulary from a set of descriptors.
1448
1449
For details, see, for example, *Visual Categorization with Bags of Keypoints* by Gabriella Csurka,
1450
Christopher R. Dance, Lixin Fan, Jutta Willamowski, Cedric Bray, 2004. :
1451
 */
1452
class CV_EXPORTS_W BOWTrainer
1453
{
1454
public:
1455
    BOWTrainer();
1456
    virtual ~BOWTrainer();
1457
1458
    /** @brief Adds descriptors to a training set.
1459
1460
    @param descriptors Descriptors to add to a training set. Each row of the descriptors matrix is a
1461
    descriptor.
1462
1463
    The training set is clustered using clustermethod to construct the vocabulary.
1464
     */
1465
    CV_WRAP void add( const Mat& descriptors );
1466
1467
    /** @brief Returns a training set of descriptors.
1468
    */
1469
    CV_WRAP const std::vector<Mat>& getDescriptors() const;
1470
1471
    /** @brief Returns the count of all descriptors stored in the training set.
1472
    */
1473
    CV_WRAP int descriptorsCount() const;
1474
1475
    CV_WRAP virtual void clear();
1476
1477
    /** @overload */
1478
    CV_WRAP virtual Mat cluster() const = 0;
1479
1480
    /** @brief Clusters train descriptors.
1481
1482
    @param descriptors Descriptors to cluster. Each row of the descriptors matrix is a descriptor.
1483
    Descriptors are not added to the inner train descriptor set.
1484
1485
    The vocabulary consists of cluster centers. So, this method returns the vocabulary. In the first
1486
    variant of the method, train descriptors stored in the object are clustered. In the second variant,
1487
    input descriptors are clustered.
1488
     */
1489
    CV_WRAP virtual Mat cluster( const Mat& descriptors ) const = 0;
1490
1491
protected:
1492
    std::vector<Mat> descriptors;
1493
    int size;
1494
};
1495
1496
/** @brief kmeans -based class to train visual vocabulary using the *bag of visual words* approach. :
1497
 */
1498
class CV_EXPORTS_W BOWKMeansTrainer : public BOWTrainer
1499
{
1500
public:
1501
    /** @brief The constructor.
1502
1503
    @see cv::kmeans
1504
    */
1505
    CV_WRAP BOWKMeansTrainer( int clusterCount, const TermCriteria& termcrit=TermCriteria(),
1506
                      int attempts=3, int flags=KMEANS_PP_CENTERS );
1507
    virtual ~BOWKMeansTrainer();
1508
1509
    // Returns trained vocabulary (i.e. cluster centers).
1510
    CV_WRAP virtual Mat cluster() const CV_OVERRIDE;
1511
    CV_WRAP virtual Mat cluster( const Mat& descriptors ) const CV_OVERRIDE;
1512
1513
protected:
1514
1515
    int clusterCount;
1516
    TermCriteria termcrit;
1517
    int attempts;
1518
    int flags;
1519
};
1520
1521
/** @brief Class to compute an image descriptor using the *bag of visual words*.
1522
1523
Such a computation consists of the following steps:
1524
1525
1.  Compute descriptors for a given image and its keypoints set.
1526
2.  Find the nearest visual words from the vocabulary for each keypoint descriptor.
1527
3.  Compute the bag-of-words image descriptor as is a normalized histogram of vocabulary words
1528
encountered in the image. The i-th bin of the histogram is a frequency of i-th word of the
1529
vocabulary in the given image.
1530
 */
1531
class CV_EXPORTS_W BOWImgDescriptorExtractor
1532
{
1533
public:
1534
    /** @brief The constructor.
1535
1536
    @param dextractor Descriptor extractor that is used to compute descriptors for an input image and
1537
    its keypoints.
1538
    @param dmatcher Descriptor matcher that is used to find the nearest word of the trained vocabulary
1539
    for each keypoint descriptor of the image.
1540
     */
1541
    CV_WRAP BOWImgDescriptorExtractor( const Ptr<Feature2D>& dextractor,
1542
                                       const Ptr<DescriptorMatcher>& dmatcher );
1543
    /** @overload */
1544
    BOWImgDescriptorExtractor( const Ptr<DescriptorMatcher>& dmatcher );
1545
    virtual ~BOWImgDescriptorExtractor();
1546
1547
    /** @brief Sets a visual vocabulary.
1548
1549
    @param vocabulary Vocabulary (can be trained using the inheritor of BOWTrainer ). Each row of the
1550
    vocabulary is a visual word (cluster center).
1551
     */
1552
    CV_WRAP void setVocabulary( const Mat& vocabulary );
1553
1554
    /** @brief Returns the set vocabulary.
1555
    */
1556
    CV_WRAP const Mat& getVocabulary() const;
1557
1558
    /** @brief Computes an image descriptor using the set visual vocabulary.
1559
1560
    @param image Image, for which the descriptor is computed.
1561
    @param keypoints Keypoints detected in the input image.
1562
    @param imgDescriptor Computed output image descriptor.
1563
    @param pointIdxsOfClusters Indices of keypoints that belong to the cluster. This means that
1564
    pointIdxsOfClusters[i] are keypoint indices that belong to the i -th cluster (word of vocabulary)
1565
    returned if it is non-zero.
1566
    @param descriptors Descriptors of the image keypoints that are returned if they are non-zero.
1567
     */
1568
    void compute( InputArray image, std::vector<KeyPoint>& keypoints, OutputArray imgDescriptor,
1569
                  std::vector<std::vector<int> >* pointIdxsOfClusters=0, Mat* descriptors=0 );
1570
    /** @overload
1571
    @param keypointDescriptors Computed descriptors to match with vocabulary.
1572
    @param imgDescriptor Computed output image descriptor.
1573
    @param pointIdxsOfClusters Indices of keypoints that belong to the cluster. This means that
1574
    pointIdxsOfClusters[i] are keypoint indices that belong to the i -th cluster (word of vocabulary)
1575
    returned if it is non-zero.
1576
    */
1577
    void compute( InputArray keypointDescriptors, OutputArray imgDescriptor,
1578
                  std::vector<std::vector<int> >* pointIdxsOfClusters=0 );
1579
    // compute() is not constant because DescriptorMatcher::match is not constant
1580
1581
    CV_WRAP_AS(compute) void compute2( const Mat& image, std::vector<KeyPoint>& keypoints, CV_OUT Mat& imgDescriptor )
1582
0
    { compute(image,keypoints,imgDescriptor); }
1583
1584
    /** @brief Returns an image descriptor size if the vocabulary is set. Otherwise, it returns 0.
1585
    */
1586
    CV_WRAP int descriptorSize() const;
1587
1588
    /** @brief Returns an image descriptor type.
1589
     */
1590
    CV_WRAP int descriptorType() const;
1591
1592
protected:
1593
    Mat vocabulary;
1594
    Ptr<DescriptorExtractor> dextractor;
1595
    Ptr<DescriptorMatcher> dmatcher;
1596
};
1597
1598
//! @} features2d_category
1599
1600
} /* namespace cv */
1601
1602
#endif