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