common_interfaces_of_feature_detectors.rst 18.4 KB
Newer Older
1 2 3
Common Interfaces of Feature Detectors
======================================

4 5
.. highlight:: cpp

6
Feature detectors in OpenCV have wrappers with a common interface that enables you to easily switch
7
between different algorithms solving the same problem. All objects that implement keypoint detectors
8
inherit the
9
:ocv:class:`FeatureDetector` interface.
10

11
.. note::
12

13
   * An example explaining keypoint detection can be found at opencv_source_code/samples/cpp/descriptor_extractor_matcher.cpp
14

15 16
FeatureDetector
---------------
17
.. ocv:class:: FeatureDetector : public Algorithm
18

19
Abstract base class for 2D image feature detectors. ::
20 21 22 23 24

    class CV_EXPORTS FeatureDetector
    {
    public:
        virtual ~FeatureDetector();
25

Ilya Lavrenov's avatar
Ilya Lavrenov committed
26 27
        void detect( InputArray image, vector<KeyPoint>& keypoints,
                     InputArray mask=noArray() ) const;
28

Ilya Lavrenov's avatar
Ilya Lavrenov committed
29
        void detect( InputArrayOfArrays images,
30
                     vector<vector<KeyPoint> >& keypoints,
Ilya Lavrenov's avatar
Ilya Lavrenov committed
31
                     InputArrayOfArrays masks=noArray() ) const;
32

33 34
        virtual void read(const FileNode&);
        virtual void write(FileStorage&) const;
35

Andrey Kamaev's avatar
Andrey Kamaev committed
36
        static Ptr<FeatureDetector> create( const String& detectorType );
37

38 39 40
    protected:
    ...
    };
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
41

42
FeatureDetector::detect
43
---------------------------
44
Detects keypoints in an image (first variant) or image set (second variant).
45

Ilya Lavrenov's avatar
Ilya Lavrenov committed
46
.. ocv:function:: void FeatureDetector::detect( InputArray image, vector<KeyPoint>& keypoints, InputArray mask=noArray() ) const
47

Ilya Lavrenov's avatar
Ilya Lavrenov committed
48
.. ocv:function:: void FeatureDetector::detect( InputArrayOfArrays images, vector<vector<KeyPoint> >& keypoints, InputArrayOfArrays masks=noArray() ) const
49

50 51
.. ocv:pyfunction:: cv2.FeatureDetector_create.detect(image[, mask]) -> keypoints

52
    :param image: Image.
53

54
    :param images: Image set.
55

56 57 58
    :param keypoints: The detected keypoints. In the second variant of the method ``keypoints[i]`` is a set of keypoints detected in ``images[i]`` .

    :param mask: Mask specifying where to look for keypoints (optional). It must be a 8-bit integer matrix with non-zero values in the region of interest.
59

60
    :param masks: Masks for each input image specifying where to look for keypoints (optional). ``masks[i]`` is a mask for ``images[i]``.
61

62
FeatureDetector::create
63
-----------------------
64
Creates a feature detector by its name.
65

Andrey Kamaev's avatar
Andrey Kamaev committed
66
.. ocv:function:: Ptr<FeatureDetector> FeatureDetector::create( const String& detectorType )
67

68 69
.. ocv:pyfunction:: cv2.FeatureDetector_create(detectorType) -> retval

70
    :param detectorType: Feature detector type.
71

72 73
The following detector types are supported:

74 75
* ``"FAST"`` -- :ocv:class:`FastFeatureDetector`
* ``"STAR"`` -- :ocv:class:`StarFeatureDetector`
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
76 77 78
* ``"SIFT"`` -- :ocv:class:`SIFT` (nonfree module)
* ``"SURF"`` -- :ocv:class:`SURF` (nonfree module)
* ``"ORB"`` -- :ocv:class:`ORB`
79
* ``"BRISK"`` -- :ocv:class:`BRISK`
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
80
* ``"MSER"`` -- :ocv:class:`MSER`
81 82 83 84
* ``"GFTT"`` -- :ocv:class:`GoodFeaturesToTrackDetector`
* ``"HARRIS"`` -- :ocv:class:`GoodFeaturesToTrackDetector` with Harris detector enabled
* ``"Dense"`` -- :ocv:class:`DenseFeatureDetector`
* ``"SimpleBlob"`` -- :ocv:class:`SimpleBlobDetector`
85 86

Also a combined format is supported: feature detector adapter name ( ``"Grid"`` --
87 88
:ocv:class:`GridAdaptedFeatureDetector`, ``"Pyramid"`` --
:ocv:class:`PyramidAdaptedFeatureDetector` ) + feature detector name (see above),
89
for example: ``"GridFAST"``, ``"PyramidSTAR"`` .
90 91 92

FastFeatureDetector
-------------------
93
.. ocv:class:: FastFeatureDetector : public FeatureDetector
94

95
Wrapping class for feature detection using the
96
:ocv:func:`FAST` method. ::
97 98 99 100

    class FastFeatureDetector : public FeatureDetector
    {
    public:
101
        FastFeatureDetector( int threshold=1, bool nonmaxSuppression=true, type=FastFeatureDetector::TYPE_9_16 );
102 103 104 105 106
        virtual void read( const FileNode& fn );
        virtual void write( FileStorage& fs ) const;
    protected:
        ...
    };
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
107

108 109
GoodFeaturesToTrackDetector
---------------------------
110
.. ocv:class:: GoodFeaturesToTrackDetector : public FeatureDetector
111

112
Wrapping class for feature detection using the
113
:ocv:func:`goodFeaturesToTrack` function. ::
114 115 116 117 118 119 120

    class GoodFeaturesToTrackDetector : public FeatureDetector
    {
    public:
        class Params
        {
        public:
121 122
            Params( int maxCorners=1000, double qualityLevel=0.01,
                    double minDistance=1., int blockSize=3,
123 124 125
                    bool useHarrisDetector=false, double k=0.04 );
            void read( const FileNode& fn );
            void write( FileStorage& fs ) const;
126

127 128 129 130 131 132 133
            int maxCorners;
            double qualityLevel;
            double minDistance;
            int blockSize;
            bool useHarrisDetector;
            double k;
        };
134

135 136
        GoodFeaturesToTrackDetector( const GoodFeaturesToTrackDetector::Params& params=
                                                GoodFeaturesToTrackDetector::Params() );
137 138
        GoodFeaturesToTrackDetector( int maxCorners, double qualityLevel,
                                     double minDistance, int blockSize=3,
139 140 141 142 143 144
                                     bool useHarrisDetector=false, double k=0.04 );
        virtual void read( const FileNode& fn );
        virtual void write( FileStorage& fs ) const;
    protected:
        ...
    };
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
145

146 147
MserFeatureDetector
-------------------
148
.. ocv:class:: MserFeatureDetector : public FeatureDetector
149

150
Wrapping class for feature detection using the
151
:ocv:class:`MSER` class. ::
152 153 154 155 156

    class MserFeatureDetector : public FeatureDetector
    {
    public:
        MserFeatureDetector( CvMSERParams params=cvMSERParams() );
157
        MserFeatureDetector( int delta, int minArea, int maxArea,
158
                             double maxVariation, double minDiversity,
159
                             int maxEvolution, double areaThreshold,
160 161 162 163 164 165
                             double minMargin, int edgeBlurSize );
        virtual void read( const FileNode& fn );
        virtual void write( FileStorage& fs ) const;
    protected:
        ...
    };
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
166

167 168 169

StarFeatureDetector
-------------------
170
.. ocv:class:: StarFeatureDetector : public FeatureDetector
171

172
The class implements the keypoint detector introduced by [Agrawal08]_, synonym of ``StarDetector``.  ::
173 174 175 176

    class StarFeatureDetector : public FeatureDetector
    {
    public:
177
        StarFeatureDetector( int maxSize=16, int responseThreshold=30,
178 179 180 181 182 183 184
                             int lineThresholdProjected = 10,
                             int lineThresholdBinarized=8, int suppressNonmaxSize=5 );
        virtual void read( const FileNode& fn );
        virtual void write( FileStorage& fs ) const;
    protected:
        ...
    };
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
185

186 187 188
.. [Agrawal08] Agrawal, M., Konolige, K., & Blas, M. R. (2008). Censure: Center surround extremas for realtime feature detection and matching. In Computer Vision–ECCV 2008 (pp. 102-115). Springer Berlin Heidelberg.


189 190
DenseFeatureDetector
--------------------
191
.. ocv:class:: DenseFeatureDetector : public FeatureDetector
192 193 194

Class for generation of image features which are distributed densely and regularly over the image. ::

195 196 197 198
        class DenseFeatureDetector : public FeatureDetector
        {
        public:
                DenseFeatureDetector( float initFeatureScale=1.f, int featureScaleLevels=1,
199 200 201 202
                              float featureScaleMul=0.1f,
                              int initXyStep=6, int initImgBound=0,
                              bool varyXyStepWithScale=true,
                              bool varyImgBoundWithScale=false );
203
        protected:
204 205
        ...
    };
206

207 208 209 210 211 212 213 214
The detector generates several levels (in the amount of ``featureScaleLevels``) of features. Features of each level are located in the nodes of a regular grid over the image (excluding the image boundary of given size). The level parameters (a feature scale, a node size, a size of boundary) are multiplied by ``featureScaleMul`` with level index growing depending on input flags, viz.:

* Feature scale is multiplied always.

* The grid node size is multiplied if ``varyXyStepWithScale`` is ``true``.

* Size of image boundary is multiplied if ``varyImgBoundWithScale`` is ``true``.

Vincent Rabaud's avatar
Vincent Rabaud committed
215

216 217
SimpleBlobDetector
-------------------
218
.. ocv:class:: SimpleBlobDetector : public FeatureDetector
219

220
Class for extracting blobs from an image. ::
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

    class SimpleBlobDetector : public FeatureDetector
    {
    public:
    struct Params
    {
        Params();
        float thresholdStep;
        float minThreshold;
        float maxThreshold;
        size_t minRepeatability;
        float minDistBetweenBlobs;

        bool filterByColor;
        uchar blobColor;

        bool filterByArea;
        float minArea, maxArea;

        bool filterByCircularity;
        float minCircularity, maxCircularity;

        bool filterByInertia;
        float minInertiaRatio, maxInertiaRatio;

        bool filterByConvexity;
        float minConvexity, maxConvexity;
    };

    SimpleBlobDetector(const SimpleBlobDetector::Params &parameters = SimpleBlobDetector::Params());

    protected:
        ...
    };

256 257
The class implements a simple algorithm for extracting blobs from an image:

258
#. Convert the source image to binary images by applying thresholding with several thresholds from ``minThreshold`` (inclusive) to ``maxThreshold`` (exclusive) with distance ``thresholdStep`` between neighboring thresholds.
259

260
#. Extract connected components from every binary image by  :ocv:func:`findContours`  and calculate their centers.
261

262
#. Group centers from several binary images by their coordinates. Close centers form one group that corresponds to one blob, which is controlled by the ``minDistBetweenBlobs`` parameter.
263 264

#. From the groups, estimate final centers of blobs and their radiuses and return as locations and sizes of keypoints.
265 266 267

This class performs several filtrations of returned blobs. You should set ``filterBy*`` to true/false to turn on/off corresponding filtration. Available filtrations:

268
 * **By color**. This filter compares the intensity of a binary image at the center of a blob to ``blobColor``. If they differ, the blob is filtered out. Use ``blobColor = 0`` to extract dark blobs and ``blobColor = 255`` to extract light blobs.
269

270
 * **By area**. Extracted blobs have an area between ``minArea`` (inclusive) and ``maxArea`` (exclusive).
271

272
 * **By circularity**. Extracted blobs have circularity (:math:`\frac{4*\pi*Area}{perimeter * perimeter}`) between ``minCircularity`` (inclusive) and ``maxCircularity`` (exclusive).
273

274
 * **By ratio of the minimum inertia to maximum inertia**. Extracted blobs have this ratio between ``minInertiaRatio`` (inclusive) and ``maxInertiaRatio`` (exclusive).
275

276
 * **By convexity**. Extracted blobs have convexity (area / area of blob convex hull) between ``minConvexity`` (inclusive) and ``maxConvexity`` (exclusive).
277 278 279 280


Default values of parameters are tuned to extract dark circular blobs.

281 282
GridAdaptedFeatureDetector
--------------------------
283
.. ocv:class:: GridAdaptedFeatureDetector : public FeatureDetector
284

285
Class adapting a detector to partition the source image into a grid and detect points in each cell. ::
286 287 288 289 290 291

    class GridAdaptedFeatureDetector : public FeatureDetector
    {
    public:
        /*
         * detector            Detector that will be adapted.
292
         * maxTotalKeypoints   Maximum count of keypoints detected on the image.
293 294
         *                     Only the strongest keypoints will be kept.
         * gridRows            Grid row count.
295 296
         * gridCols            Grid column count.
         */
297 298
        GridAdaptedFeatureDetector( const Ptr<FeatureDetector>& detector,
                                    int maxTotalKeypoints, int gridRows=4,
299 300 301 302 303 304
                                    int gridCols=4 );
        virtual void read( const FileNode& fn );
        virtual void write( FileStorage& fs ) const;
    protected:
        ...
    };
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
305

306 307
PyramidAdaptedFeatureDetector
-----------------------------
308
.. ocv:class:: PyramidAdaptedFeatureDetector : public FeatureDetector
309

310
Class adapting a detector to detect points over multiple levels of a Gaussian pyramid. Consider using this class for detectors that are not inherently scaled. ::
311 312 313 314

    class PyramidAdaptedFeatureDetector : public FeatureDetector
    {
    public:
315
        PyramidAdaptedFeatureDetector( const Ptr<FeatureDetector>& detector,
316 317 318 319 320 321 322 323 324 325
                                       int levels=2 );
        virtual void read( const FileNode& fn );
        virtual void write( FileStorage& fs ) const;
    protected:
        ...
    };


DynamicAdaptedFeatureDetector
-----------------------------
326
.. ocv:class:: DynamicAdaptedFeatureDetector : public FeatureDetector
327

328
Adaptively adjusting detector that iteratively detects features until the desired number is found. ::
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
329 330 331 332

       class DynamicAdaptedFeatureDetector: public FeatureDetector
       {
       public:
333
           DynamicAdaptedFeatureDetector( const Ptr<AdjusterAdapter>& adjuster,
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
334 335 336
               int min_features=400, int max_features=500, int max_iters=5 );
           ...
       };
337

338
If the detector is persisted, it "remembers" the parameters
339 340
used for the last detection. In this case, the detector may be used for consistent numbers
of keypoints in a set of temporally related images, such as video streams or
341 342
panorama series.

343
``DynamicAdaptedFeatureDetector``  uses another detector, such as FAST or SURF, to do the dirty work,
344
with the help of ``AdjusterAdapter`` .
345
If the detected number of features is not large enough,
346
``AdjusterAdapter`` adjusts the detection parameters so that the next detection
347
results in a bigger or smaller number of features.  This is repeated until either the number of desired features are found
348 349
or the parameters are maxed out.

350 351
Adapters can be easily implemented for any detector via the
``AdjusterAdapter`` interface.
352

353
Beware that this is not thread-safe since the adjustment of parameters requires modification of the feature detector class instance.
354

355
Example of creating ``DynamicAdaptedFeatureDetector`` : ::
356 357

    //sample usage:
358
    //will create a detector that attempts to find
359
    //100 - 110 FAST Keypoints, and will at most run
360
    //FAST feature detection 10 times until that
361 362 363
    //number of keypoints are found
    Ptr<FeatureDetector> detector(new DynamicAdaptedFeatureDetector (100, 110, 10,
                                  new FastAdjuster(20,true)));
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
364

365

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
366
DynamicAdaptedFeatureDetector::DynamicAdaptedFeatureDetector
367
------------------------------------------------------------
368
The constructor
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
369

370
.. ocv:function:: DynamicAdaptedFeatureDetector::DynamicAdaptedFeatureDetector( const Ptr<AdjusterAdapter>& adjuster, int min_features=400, int max_features=500, int max_iters=5 )
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
371

372
    :param adjuster:  :ocv:class:`AdjusterAdapter`  that detects features and adjusts parameters.
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
373

374
    :param min_features: Minimum desired number of features.
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
375

376
    :param max_features: Maximum desired number of features.
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
377

378
    :param max_iters: Maximum number of times to try adjusting the feature detector parameters. For :ocv:class:`FastAdjuster` , this number can be high, but with ``Star`` or ``Surf``  many iterations can be time-consuming.  At each iteration the detector is rerun.
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
379 380 381

AdjusterAdapter
---------------
382
.. ocv:class:: AdjusterAdapter : public FeatureDetector
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
383

384
Class providing an interface for adjusting parameters of a feature detector. This interface is used by :ocv:class:`DynamicAdaptedFeatureDetector` . It is a wrapper for :ocv:class:`FeatureDetector` that enables adjusting parameters after feature detection. ::
385

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
386 387 388
     class AdjusterAdapter: public FeatureDetector
     {
     public:
389 390 391 392 393
        virtual ~AdjusterAdapter() {}
        virtual void tooFew(int min, int n_detected) = 0;
        virtual void tooMany(int max, int n_detected) = 0;
        virtual bool good() const = 0;
        virtual Ptr<AdjusterAdapter> clone() const = 0;
Andrey Kamaev's avatar
Andrey Kamaev committed
394
        static Ptr<AdjusterAdapter> create( const String& detectorType );
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
395 396 397 398
     };


See
399 400 401
:ocv:class:`FastAdjuster`,
:ocv:class:`StarAdjuster`, and
:ocv:class:`SurfAdjuster` for concrete implementations.
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
402 403 404

AdjusterAdapter::tooFew
---------------------------
405
Adjusts the detector parameters to detect more features.
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
406

407
.. ocv:function:: void AdjusterAdapter::tooFew(int min, int n_detected)
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
408

409
    :param min: Minimum desired number of features.
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
410

411
    :param n_detected: Number of features detected during the latest run.
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
412

413
Example: ::
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
414 415 416 417 418 419 420 421

    void FastAdjuster::tooFew(int min, int n_detected)
    {
            thresh_--;
    }

AdjusterAdapter::tooMany
----------------------------
422
Adjusts the detector parameters to detect less features.
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
423

424
.. ocv:function:: void AdjusterAdapter::tooMany(int max, int n_detected)
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
425

426
    :param max: Maximum desired number of features.
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
427

428
    :param n_detected: Number of features detected during the latest run.
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
429

430
Example: ::
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
431 432 433 434 435 436 437 438

    void FastAdjuster::tooMany(int min, int n_detected)
    {
            thresh_++;
    }


AdjusterAdapter::good
439
---------------------
440
Returns false if the detector parameters cannot be adjusted any more.
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
441

442
.. ocv:function:: bool AdjusterAdapter::good() const
443 444

Example: ::
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
445 446 447 448 449 450

        bool FastAdjuster::good() const
        {
                return (thresh_ > 1) && (thresh_ < 200);
        }

451
AdjusterAdapter::create
452
-----------------------
453 454
Creates an adjuster adapter by name

Andrey Kamaev's avatar
Andrey Kamaev committed
455
.. ocv:function:: Ptr<AdjusterAdapter> AdjusterAdapter::create( const String& detectorType )
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
456

457
    Creates an adjuster adapter by name ``detectorType``. The detector name is the same as in :ocv:func:`FeatureDetector::create`, but now supports ``"FAST"``, ``"STAR"``, and ``"SURF"`` only.
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
458 459 460

FastAdjuster
------------
461
.. ocv:class:: FastAdjuster : public AdjusterAdapter
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
462

463
:ocv:class:`AdjusterAdapter` for :ocv:class:`FastFeatureDetector`. This class decreases or increases the threshold value by 1. ::
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
464 465 466 467 468 469 470 471 472 473

        class FastAdjuster FastAdjuster: public AdjusterAdapter
        {
        public:
                FastAdjuster(int init_thresh = 20, bool nonmax = true);
                ...
        };

StarAdjuster
------------
474
.. ocv:class:: StarAdjuster : public AdjusterAdapter
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
475

476
:ocv:class:`AdjusterAdapter` for :ocv:class:`StarFeatureDetector`. This class adjusts the ``responseThreshhold`` of ``StarFeatureDetector``.  ::
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
477 478 479 480 481 482

        class StarAdjuster: public AdjusterAdapter
        {
                StarAdjuster(double initial_thresh = 30.0);
                ...
        };
483 484 485

SurfAdjuster
------------
486
.. ocv:class:: SurfAdjuster : public AdjusterAdapter
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502

:ocv:class:`AdjusterAdapter` for ``SurfFeatureDetector``.  ::

    class CV_EXPORTS SurfAdjuster: public AdjusterAdapter
    {
    public:
        SurfAdjuster( double initial_thresh=400.f, double min_thresh=2, double max_thresh=1000 );

        virtual void tooFew(int minv, int n_detected);
        virtual void tooMany(int maxv, int n_detected);
        virtual bool good() const;

        virtual Ptr<AdjusterAdapter> clone() const;

        ...
    };