opencv on mbed

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers features2d.hpp Source File

features2d.hpp

00001 /*M///////////////////////////////////////////////////////////////////////////////////////
00002 //
00003 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
00004 //
00005 //  By downloading, copying, installing or using the software you agree to this license.
00006 //  If you do not agree to this license, do not download, install,
00007 //  copy or use the software.
00008 //
00009 //
00010 //                           License Agreement
00011 //                For Open Source Computer Vision Library
00012 //
00013 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
00014 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
00015 // Third party copyrights are property of their respective owners.
00016 //
00017 // Redistribution and use in source and binary forms, with or without modification,
00018 // are permitted provided that the following conditions are met:
00019 //
00020 //   * Redistribution's of source code must retain the above copyright notice,
00021 //     this list of conditions and the following disclaimer.
00022 //
00023 //   * Redistribution's in binary form must reproduce the above copyright notice,
00024 //     this list of conditions and the following disclaimer in the documentation
00025 //     and/or other materials provided with the distribution.
00026 //
00027 //   * The name of the copyright holders may not be used to endorse or promote products
00028 //     derived from this software without specific prior written permission.
00029 //
00030 // This software is provided by the copyright holders and contributors "as is" and
00031 // any express or implied warranties, including, but not limited to, the implied
00032 // warranties of merchantability and fitness for a particular purpose are disclaimed.
00033 // In no event shall the Intel Corporation or contributors be liable for any direct,
00034 // indirect, incidental, special, exemplary, or consequential damages
00035 // (including, but not limited to, procurement of substitute goods or services;
00036 // loss of use, data, or profits; or business interruption) however caused
00037 // and on any theory of liability, whether in contract, strict liability,
00038 // or tort (including negligence or otherwise) arising in any way out of
00039 // the use of this software, even if advised of the possibility of such damage.
00040 //
00041 //M*/
00042 
00043 #ifndef __OPENCV_FEATURES_2D_HPP__
00044 #define __OPENCV_FEATURES_2D_HPP__
00045 
00046 #include "opencv2/core.hpp"
00047 #include "opencv2/flann/miniflann.hpp"
00048 
00049 /**
00050   @defgroup features2d 2D Features Framework
00051   @{
00052     @defgroup features2d_main Feature Detection and Description
00053     @defgroup features2d_match Descriptor Matchers
00054 
00055 Matchers of keypoint descriptors in OpenCV have wrappers with a common interface that enables you to
00056 easily switch between different algorithms solving the same problem. This section is devoted to
00057 matching descriptors that are represented as vectors in a multidimensional space. All objects that
00058 implement vector descriptor matchers inherit the DescriptorMatcher interface.
00059 
00060 @note
00061    -   An example explaining keypoint matching can be found at
00062         opencv_source_code/samples/cpp/descriptor_extractor_matcher.cpp
00063     -   An example on descriptor matching evaluation can be found at
00064         opencv_source_code/samples/cpp/detector_descriptor_matcher_evaluation.cpp
00065     -   An example on one to many image matching can be found at
00066         opencv_source_code/samples/cpp/matching_to_many_images.cpp
00067 
00068     @defgroup features2d_draw Drawing Function of Keypoints and Matches
00069     @defgroup features2d_category Object Categorization
00070 
00071 This section describes approaches based on local 2D features and used to categorize objects.
00072 
00073 @note
00074    -   A complete Bag-Of-Words sample can be found at
00075         opencv_source_code/samples/cpp/bagofwords_classification.cpp
00076     -   (Python) An example using the features2D framework to perform object categorization can be
00077         found at opencv_source_code/samples/python/find_obj.py
00078 
00079   @}
00080  */
00081 
00082 namespace cv
00083 {
00084 
00085 //! @addtogroup features2d
00086 //! @{
00087 
00088 // //! writes vector of keypoints to the file storage
00089 // CV_EXPORTS void write(FileStorage& fs, const String& name, const std::vector<KeyPoint>& keypoints);
00090 // //! reads vector of keypoints from the specified file storage node
00091 // CV_EXPORTS void read(const FileNode& node, CV_OUT std::vector<KeyPoint>& keypoints);
00092 
00093 /** @brief A class filters a vector of keypoints.
00094 
00095  Because now it is difficult to provide a convenient interface for all usage scenarios of the
00096  keypoints filter class, it has only several needed by now static methods.
00097  */
00098 class CV_EXPORTS KeyPointsFilter
00099 {
00100 public:
00101     KeyPointsFilter(){}
00102 
00103     /*
00104      * Remove keypoints within borderPixels of an image edge.
00105      */
00106     static void runByImageBorder( std::vector<KeyPoint>& keypoints, Size imageSize, int borderSize );
00107     /*
00108      * Remove keypoints of sizes out of range.
00109      */
00110     static void runByKeypointSize( std::vector<KeyPoint>& keypoints, float minSize,
00111                                    float maxSize=FLT_MAX );
00112     /*
00113      * Remove keypoints from some image by mask for pixels of this image.
00114      */
00115     static void runByPixelsMask( std::vector<KeyPoint>& keypoints, const Mat& mask );
00116     /*
00117      * Remove duplicated keypoints.
00118      */
00119     static void removeDuplicated( std::vector<KeyPoint>& keypoints );
00120 
00121     /*
00122      * Retain the specified number of the best keypoints (according to the response)
00123      */
00124     static void retainBest( std::vector<KeyPoint>& keypoints, int npoints );
00125 };
00126 
00127 
00128 /************************************ Base Classes ************************************/
00129 
00130 /** @brief Abstract base class for 2D image feature detectors and descriptor extractors
00131 */
00132 class CV_EXPORTS_W Feature2D : public virtual Algorithm
00133 {
00134 public:
00135     virtual ~Feature2D();
00136 
00137     /** @brief Detects keypoints in an image (first variant) or image set (second variant).
00138 
00139     @param image Image.
00140     @param keypoints The detected keypoints. In the second variant of the method keypoints[i] is a set
00141     of keypoints detected in images[i] .
00142     @param mask Mask specifying where to look for keypoints (optional). It must be a 8-bit integer
00143     matrix with non-zero values in the region of interest.
00144      */
00145     CV_WRAP virtual void detect( InputArray image,
00146                                  CV_OUT std::vector<KeyPoint>& keypoints,
00147                                  InputArray mask=noArray() );
00148 
00149     /** @overload
00150     @param images Image set.
00151     @param keypoints The detected keypoints. In the second variant of the method keypoints[i] is a set
00152     of keypoints detected in images[i] .
00153     @param masks Masks for each input image specifying where to look for keypoints (optional).
00154     masks[i] is a mask for images[i].
00155     */
00156     virtual void detect( InputArrayOfArrays images,
00157                          std::vector<std::vector<KeyPoint> >& keypoints,
00158                          InputArrayOfArrays masks=noArray() );
00159 
00160     /** @brief Computes the descriptors for a set of keypoints detected in an image (first variant) or image set
00161     (second variant).
00162 
00163     @param image Image.
00164     @param keypoints Input collection of keypoints. Keypoints for which a descriptor cannot be
00165     computed are removed. Sometimes new keypoints can be added, for example: SIFT duplicates keypoint
00166     with several dominant orientations (for each orientation).
00167     @param descriptors Computed descriptors. In the second variant of the method descriptors[i] are
00168     descriptors computed for a keypoints[i]. Row j is the keypoints (or keypoints[i]) is the
00169     descriptor for keypoint j-th keypoint.
00170      */
00171     CV_WRAP virtual void compute( InputArray image,
00172                                   CV_OUT CV_IN_OUT std::vector<KeyPoint>& keypoints,
00173                                   OutputArray descriptors );
00174 
00175     /** @overload
00176 
00177     @param images Image set.
00178     @param keypoints Input collection of keypoints. Keypoints for which a descriptor cannot be
00179     computed are removed. Sometimes new keypoints can be added, for example: SIFT duplicates keypoint
00180     with several dominant orientations (for each orientation).
00181     @param descriptors Computed descriptors. In the second variant of the method descriptors[i] are
00182     descriptors computed for a keypoints[i]. Row j is the keypoints (or keypoints[i]) is the
00183     descriptor for keypoint j-th keypoint.
00184     */
00185     virtual void compute( InputArrayOfArrays images,
00186                           std::vector<std::vector<KeyPoint> >& keypoints,
00187                           OutputArrayOfArrays descriptors );
00188 
00189     /** Detects keypoints and computes the descriptors */
00190     CV_WRAP virtual void detectAndCompute( InputArray image, InputArray mask,
00191                                            CV_OUT std::vector<KeyPoint>& keypoints,
00192                                            OutputArray descriptors,
00193                                            bool useProvidedKeypoints=false );
00194 
00195     CV_WRAP virtual int descriptorSize() const;
00196     CV_WRAP virtual int descriptorType() const;
00197     CV_WRAP virtual int defaultNorm() const;
00198 
00199     //! Return true if detector object is empty
00200     CV_WRAP virtual bool empty() const;
00201 };
00202 
00203 /** Feature detectors in OpenCV have wrappers with a common interface that enables you to easily switch
00204 between different algorithms solving the same problem. All objects that implement keypoint detectors
00205 inherit the FeatureDetector interface. */
00206 typedef Feature2D FeatureDetector;
00207 
00208 /** Extractors of keypoint descriptors in OpenCV have wrappers with a common interface that enables you
00209 to easily switch between different algorithms solving the same problem. This section is devoted to
00210 computing descriptors represented as vectors in a multidimensional space. All objects that implement
00211 the vector descriptor extractors inherit the DescriptorExtractor interface.
00212  */
00213 typedef Feature2D DescriptorExtractor;
00214 
00215 //! @addtogroup features2d_main
00216 //! @{
00217 
00218 /** @brief Class implementing the BRISK keypoint detector and descriptor extractor, described in @cite LCS11 .
00219  */
00220 class CV_EXPORTS_W BRISK : public Feature2D
00221 {
00222 public:
00223     /** @brief The BRISK constructor
00224 
00225     @param thresh AGAST detection threshold score.
00226     @param octaves detection octaves. Use 0 to do single scale.
00227     @param patternScale apply this scale to the pattern used for sampling the neighbourhood of a
00228     keypoint.
00229      */
00230     CV_WRAP static Ptr<BRISK> create(int thresh=30, int octaves=3, float patternScale=1.0f);
00231 
00232     /** @brief The BRISK constructor for a custom pattern
00233 
00234     @param radiusList defines the radii (in pixels) where the samples around a keypoint are taken (for
00235     keypoint scale 1).
00236     @param numberList defines the number of sampling points on the sampling circle. Must be the same
00237     size as radiusList..
00238     @param dMax threshold for the short pairings used for descriptor formation (in pixels for keypoint
00239     scale 1).
00240     @param dMin threshold for the long pairings used for orientation determination (in pixels for
00241     keypoint scale 1).
00242     @param indexChange index remapping of the bits. */
00243     CV_WRAP static Ptr<BRISK> create(const std::vector<float> &radiusList, const std::vector<int> &numberList,
00244         float dMax=5.85f, float dMin=8.2f, const std::vector<int>& indexChange=std::vector<int>());
00245 };
00246 
00247 /** @brief Class implementing the ORB (*oriented BRIEF*) keypoint detector and descriptor extractor
00248 
00249 described in @cite RRKB11 . The algorithm uses FAST in pyramids to detect stable keypoints, selects
00250 the strongest features using FAST or Harris response, finds their orientation using first-order
00251 moments and computes the descriptors using BRIEF (where the coordinates of random point pairs (or
00252 k-tuples) are rotated according to the measured orientation).
00253  */
00254 class CV_EXPORTS_W ORB : public Feature2D
00255 {
00256 public:
00257     enum { kBytes = 32, HARRIS_SCORE=0, FAST_SCORE=1 };
00258 
00259     /** @brief The ORB constructor
00260 
00261     @param nfeatures The maximum number of features to retain.
00262     @param scaleFactor Pyramid decimation ratio, greater than 1. scaleFactor==2 means the classical
00263     pyramid, where each next level has 4x less pixels than the previous, but such a big scale factor
00264     will degrade feature matching scores dramatically. On the other hand, too close to 1 scale factor
00265     will mean that to cover certain scale range you will need more pyramid levels and so the speed
00266     will suffer.
00267     @param nlevels The number of pyramid levels. The smallest level will have linear size equal to
00268     input_image_linear_size/pow(scaleFactor, nlevels).
00269     @param edgeThreshold This is size of the border where the features are not detected. It should
00270     roughly match the patchSize parameter.
00271     @param firstLevel It should be 0 in the current implementation.
00272     @param WTA_K The number of points that produce each element of the oriented BRIEF descriptor. The
00273     default value 2 means the BRIEF where we take a random point pair and compare their brightnesses,
00274     so we get 0/1 response. Other possible values are 3 and 4. For example, 3 means that we take 3
00275     random points (of course, those point coordinates are random, but they are generated from the
00276     pre-defined seed, so each element of BRIEF descriptor is computed deterministically from the pixel
00277     rectangle), find point of maximum brightness and output index of the winner (0, 1 or 2). Such
00278     output will occupy 2 bits, and therefore it will need a special variant of Hamming distance,
00279     denoted as NORM_HAMMING2 (2 bits per bin). When WTA_K=4, we take 4 random points to compute each
00280     bin (that will also occupy 2 bits with possible values 0, 1, 2 or 3).
00281     @param scoreType The default HARRIS_SCORE means that Harris algorithm is used to rank features
00282     (the score is written to KeyPoint::score and is used to retain best nfeatures features);
00283     FAST_SCORE is alternative value of the parameter that produces slightly less stable keypoints,
00284     but it is a little faster to compute.
00285     @param patchSize size of the patch used by the oriented BRIEF descriptor. Of course, on smaller
00286     pyramid layers the perceived image area covered by a feature will be larger.
00287     @param fastThreshold
00288      */
00289     CV_WRAP static Ptr<ORB>  create(int nfeatures=500, float scaleFactor=1.2f, int nlevels=8, int edgeThreshold=31,
00290         int firstLevel=0, int WTA_K=2, int scoreType=ORB::HARRIS_SCORE, int patchSize=31, int fastThreshold=20);
00291 
00292     CV_WRAP virtual void setMaxFeatures(int maxFeatures) = 0;
00293     CV_WRAP virtual int getMaxFeatures() const = 0;
00294 
00295     CV_WRAP virtual void setScaleFactor(double scaleFactor) = 0;
00296     CV_WRAP virtual double getScaleFactor() const = 0;
00297 
00298     CV_WRAP virtual void setNLevels(int nlevels) = 0;
00299     CV_WRAP virtual int getNLevels() const = 0;
00300 
00301     CV_WRAP virtual void setEdgeThreshold(int edgeThreshold) = 0;
00302     CV_WRAP virtual int getEdgeThreshold() const = 0;
00303 
00304     CV_WRAP virtual void setFirstLevel(int firstLevel) = 0;
00305     CV_WRAP virtual int getFirstLevel() const = 0;
00306 
00307     CV_WRAP virtual void setWTA_K(int wta_k) = 0;
00308     CV_WRAP virtual int getWTA_K() const = 0;
00309 
00310     CV_WRAP virtual void setScoreType(int scoreType) = 0;
00311     CV_WRAP virtual int getScoreType() const = 0;
00312 
00313     CV_WRAP virtual void setPatchSize(int patchSize) = 0;
00314     CV_WRAP virtual int getPatchSize() const = 0;
00315 
00316     CV_WRAP virtual void setFastThreshold(int fastThreshold) = 0;
00317     CV_WRAP virtual int getFastThreshold() const = 0;
00318 };
00319 
00320 /** @brief Maximally stable extremal region extractor
00321 
00322 The class encapsulates all the parameters of the %MSER extraction algorithm (see [wiki
00323 article](http://en.wikipedia.org/wiki/Maximally_stable_extremal_regions)).
00324 
00325 - there are two different implementation of %MSER: one for grey image, one for color image
00326 
00327 - the grey image algorithm is taken from: @cite nister2008linear ;  the paper claims to be faster
00328 than union-find method; it actually get 1.5~2m/s on my centrino L7200 1.2GHz laptop.
00329 
00330 - the color image algorithm is taken from: @cite forssen2007maximally ; it should be much slower
00331 than grey image method ( 3~4 times ); the chi_table.h file is taken directly from paper's source
00332 code which is distributed under GPL.
00333 
00334 - (Python) A complete example showing the use of the %MSER detector can be found at samples/python/mser.py
00335 */
00336 class CV_EXPORTS_W MSER : public Feature2D
00337 {
00338 public:
00339     /** @brief Full consturctor for %MSER detector
00340 
00341     @param _delta it compares \f$(size_{i}-size_{i-delta})/size_{i-delta}\f$
00342     @param _min_area prune the area which smaller than minArea
00343     @param _max_area prune the area which bigger than maxArea
00344     @param _max_variation prune the area have simliar size to its children
00345     @param _min_diversity for color image, trace back to cut off mser with diversity less than min_diversity
00346     @param _max_evolution  for color image, the evolution steps
00347     @param _area_threshold for color image, the area threshold to cause re-initialize
00348     @param _min_margin for color image, ignore too small margin
00349     @param _edge_blur_size for color image, the aperture size for edge blur
00350      */
00351     CV_WRAP static Ptr<MSER> create( int _delta=5, int _min_area=60, int _max_area=14400,
00352           double _max_variation=0.25, double _min_diversity=.2,
00353           int _max_evolution=200, double _area_threshold=1.01,
00354           double _min_margin=0.003, int _edge_blur_size=5 );
00355 
00356     /** @brief Detect %MSER regions
00357 
00358     @param image input image (8UC1, 8UC3 or 8UC4)
00359     @param msers resulting list of point sets
00360     @param bboxes resulting bounding boxes
00361     */
00362     CV_WRAP virtual void detectRegions( InputArray image,
00363                                         CV_OUT std::vector<std::vector<Point> >& msers,
00364                                         std::vector<Rect>& bboxes ) = 0;
00365 
00366     CV_WRAP virtual void setDelta(int delta) = 0;
00367     CV_WRAP virtual int getDelta() const = 0;
00368 
00369     CV_WRAP virtual void setMinArea(int minArea) = 0;
00370     CV_WRAP virtual int getMinArea() const = 0;
00371 
00372     CV_WRAP virtual void setMaxArea(int maxArea) = 0;
00373     CV_WRAP virtual int getMaxArea() const = 0;
00374 
00375     CV_WRAP virtual void setPass2Only(bool f) = 0;
00376     CV_WRAP virtual bool getPass2Only() const = 0;
00377 };
00378 
00379 /** @overload */
00380 CV_EXPORTS void FAST ( InputArray image, CV_OUT std::vector<KeyPoint>& keypoints,
00381                       int threshold, bool nonmaxSuppression=true );
00382 
00383 /** @brief Detects corners using the FAST algorithm
00384 
00385 @param image grayscale image where keypoints (corners) are detected.
00386 @param keypoints keypoints detected on the image.
00387 @param threshold threshold on difference between intensity of the central pixel and pixels of a
00388 circle around this pixel.
00389 @param nonmaxSuppression if true, non-maximum suppression is applied to detected corners
00390 (keypoints).
00391 @param type one of the three neighborhoods as defined in the paper:
00392 FastFeatureDetector::TYPE_9_16, FastFeatureDetector::TYPE_7_12,
00393 FastFeatureDetector::TYPE_5_8
00394 
00395 Detects corners using the FAST algorithm by @cite Rosten06 .
00396 
00397 @note In Python API, types are given as cv2.FAST_FEATURE_DETECTOR_TYPE_5_8,
00398 cv2.FAST_FEATURE_DETECTOR_TYPE_7_12 and cv2.FAST_FEATURE_DETECTOR_TYPE_9_16. For corner
00399 detection, use cv2.FAST.detect() method.
00400  */
00401 CV_EXPORTS void FAST ( InputArray image, CV_OUT std::vector<KeyPoint>& keypoints,
00402                       int threshold, bool nonmaxSuppression, int type );
00403 
00404 //! @} features2d_main
00405 
00406 //! @addtogroup features2d_main
00407 //! @{
00408 
00409 /** @brief Wrapping class for feature detection using the FAST method. :
00410  */
00411 class CV_EXPORTS_W FastFeatureDetector : public Feature2D
00412 {
00413 public:
00414     enum
00415     {
00416         TYPE_5_8 = 0, TYPE_7_12 = 1, TYPE_9_16 = 2,
00417         THRESHOLD = 10000, NONMAX_SUPPRESSION=10001, FAST_N=10002,
00418     };
00419 
00420     CV_WRAP static Ptr<FastFeatureDetector> create( int threshold=10,
00421                                                     bool nonmaxSuppression=true,
00422                                                     int type=FastFeatureDetector::TYPE_9_16 );
00423 
00424     CV_WRAP virtual void setThreshold(int threshold) = 0;
00425     CV_WRAP virtual int getThreshold() const = 0;
00426 
00427     CV_WRAP virtual void setNonmaxSuppression(bool f) = 0;
00428     CV_WRAP virtual bool getNonmaxSuppression() const = 0;
00429 
00430     CV_WRAP virtual void setType(int type) = 0;
00431     CV_WRAP virtual int getType() const = 0;
00432 };
00433 
00434 /** @overload */
00435 CV_EXPORTS void AGAST ( InputArray image, CV_OUT std::vector<KeyPoint>& keypoints,
00436                       int threshold, bool nonmaxSuppression=true );
00437 
00438 /** @brief Detects corners using the AGAST algorithm
00439 
00440 @param image grayscale image where keypoints (corners) are detected.
00441 @param keypoints keypoints detected on the image.
00442 @param threshold threshold on difference between intensity of the central pixel and pixels of a
00443 circle around this pixel.
00444 @param nonmaxSuppression if true, non-maximum suppression is applied to detected corners
00445 (keypoints).
00446 @param type one of the four neighborhoods as defined in the paper:
00447 AgastFeatureDetector::AGAST_5_8, AgastFeatureDetector::AGAST_7_12d,
00448 AgastFeatureDetector::AGAST_7_12s, AgastFeatureDetector::OAST_9_16
00449 
00450 For non-Intel platforms, there is a tree optimised variant of AGAST with same numerical results.
00451 The 32-bit binary tree tables were generated automatically from original code using perl script.
00452 The perl script and examples of tree generation are placed in features2d/doc folder.
00453 Detects corners using the AGAST algorithm by @cite mair2010_agast .
00454 
00455  */
00456 CV_EXPORTS void AGAST ( InputArray image, CV_OUT std::vector<KeyPoint>& keypoints,
00457                       int threshold, bool nonmaxSuppression, int type );
00458 //! @} features2d_main
00459 
00460 //! @addtogroup features2d_main
00461 //! @{
00462 
00463 /** @brief Wrapping class for feature detection using the AGAST method. :
00464  */
00465 class CV_EXPORTS_W AgastFeatureDetector : public Feature2D
00466 {
00467 public:
00468     enum
00469     {
00470         AGAST_5_8 = 0, AGAST_7_12d = 1, AGAST_7_12s = 2, OAST_9_16 = 3,
00471         THRESHOLD = 10000, NONMAX_SUPPRESSION = 10001,
00472     };
00473 
00474     CV_WRAP static Ptr<AgastFeatureDetector> create( int threshold=10,
00475                                                      bool nonmaxSuppression=true,
00476                                                      int type=AgastFeatureDetector::OAST_9_16 );
00477 
00478     CV_WRAP virtual void setThreshold(int threshold) = 0;
00479     CV_WRAP virtual int getThreshold() const = 0;
00480 
00481     CV_WRAP virtual void setNonmaxSuppression(bool f) = 0;
00482     CV_WRAP virtual bool getNonmaxSuppression() const = 0;
00483 
00484     CV_WRAP virtual void setType(int type) = 0;
00485     CV_WRAP virtual int getType() const = 0;
00486 };
00487 
00488 /** @brief Wrapping class for feature detection using the goodFeaturesToTrack function. :
00489  */
00490 class CV_EXPORTS_W GFTTDetector : public Feature2D
00491 {
00492 public:
00493     CV_WRAP static Ptr<GFTTDetector> create( int maxCorners=1000, double qualityLevel=0.01, double minDistance=1,
00494                                              int blockSize=3, bool useHarrisDetector=false, double k=0.04 );
00495     CV_WRAP virtual void setMaxFeatures(int maxFeatures) = 0;
00496     CV_WRAP virtual int getMaxFeatures() const = 0;
00497 
00498     CV_WRAP virtual void setQualityLevel(double qlevel) = 0;
00499     CV_WRAP virtual double getQualityLevel() const = 0;
00500 
00501     CV_WRAP virtual void setMinDistance(double minDistance) = 0;
00502     CV_WRAP virtual double getMinDistance() const = 0;
00503 
00504     CV_WRAP virtual void setBlockSize(int blockSize) = 0;
00505     CV_WRAP virtual int getBlockSize() const = 0;
00506 
00507     CV_WRAP virtual void setHarrisDetector(bool val) = 0;
00508     CV_WRAP virtual bool getHarrisDetector() const = 0;
00509 
00510     CV_WRAP virtual void setK(double k) = 0;
00511     CV_WRAP virtual double getK() const = 0;
00512 };
00513 
00514 /** @brief Class for extracting blobs from an image. :
00515 
00516 The class implements a simple algorithm for extracting blobs from an image:
00517 
00518 1.  Convert the source image to binary images by applying thresholding with several thresholds from
00519     minThreshold (inclusive) to maxThreshold (exclusive) with distance thresholdStep between
00520     neighboring thresholds.
00521 2.  Extract connected components from every binary image by findContours and calculate their
00522     centers.
00523 3.  Group centers from several binary images by their coordinates. Close centers form one group that
00524     corresponds to one blob, which is controlled by the minDistBetweenBlobs parameter.
00525 4.  From the groups, estimate final centers of blobs and their radiuses and return as locations and
00526     sizes of keypoints.
00527 
00528 This class performs several filtrations of returned blobs. You should set filterBy\* to true/false
00529 to turn on/off corresponding filtration. Available filtrations:
00530 
00531 -   **By color**. This filter compares the intensity of a binary image at the center of a blob to
00532 blobColor. If they differ, the blob is filtered out. Use blobColor = 0 to extract dark blobs
00533 and blobColor = 255 to extract light blobs.
00534 -   **By area**. Extracted blobs have an area between minArea (inclusive) and maxArea (exclusive).
00535 -   **By circularity**. Extracted blobs have circularity
00536 (\f$\frac{4*\pi*Area}{perimeter * perimeter}\f$) between minCircularity (inclusive) and
00537 maxCircularity (exclusive).
00538 -   **By ratio of the minimum inertia to maximum inertia**. Extracted blobs have this ratio
00539 between minInertiaRatio (inclusive) and maxInertiaRatio (exclusive).
00540 -   **By convexity**. Extracted blobs have convexity (area / area of blob convex hull) between
00541 minConvexity (inclusive) and maxConvexity (exclusive).
00542 
00543 Default values of parameters are tuned to extract dark circular blobs.
00544  */
00545 class CV_EXPORTS_W SimpleBlobDetector : public Feature2D
00546 {
00547 public:
00548   struct CV_EXPORTS_W_SIMPLE Params
00549   {
00550       CV_WRAP Params();
00551       CV_PROP_RW float thresholdStep;
00552       CV_PROP_RW float minThreshold;
00553       CV_PROP_RW float maxThreshold;
00554       CV_PROP_RW size_t minRepeatability;
00555       CV_PROP_RW float minDistBetweenBlobs;
00556 
00557       CV_PROP_RW bool filterByColor;
00558       CV_PROP_RW uchar blobColor;
00559 
00560       CV_PROP_RW bool filterByArea;
00561       CV_PROP_RW float minArea, maxArea;
00562 
00563       CV_PROP_RW bool filterByCircularity;
00564       CV_PROP_RW float minCircularity, maxCircularity;
00565 
00566       CV_PROP_RW bool filterByInertia;
00567       CV_PROP_RW float minInertiaRatio, maxInertiaRatio;
00568 
00569       CV_PROP_RW bool filterByConvexity;
00570       CV_PROP_RW float minConvexity, maxConvexity;
00571 
00572       void read( const FileNode& fn );
00573       void write( FileStorage& fs ) const;
00574   };
00575 
00576   CV_WRAP static Ptr<SimpleBlobDetector>
00577     create(const SimpleBlobDetector::Params &parameters = SimpleBlobDetector::Params());
00578 };
00579 
00580 //! @} features2d_main
00581 
00582 //! @addtogroup features2d_main
00583 //! @{
00584 
00585 /** @brief Class implementing the KAZE keypoint detector and descriptor extractor, described in @cite ABD12 .
00586 
00587 @note AKAZE descriptor can only be used with KAZE or AKAZE keypoints .. [ABD12] KAZE Features. Pablo
00588 F. Alcantarilla, Adrien Bartoli and Andrew J. Davison. In European Conference on Computer Vision
00589 (ECCV), Fiorenze, Italy, October 2012.
00590 */
00591 class CV_EXPORTS_W KAZE : public Feature2D
00592 {
00593 public:
00594     enum
00595     {
00596         DIFF_PM_G1 = 0,
00597         DIFF_PM_G2 = 1,
00598         DIFF_WEICKERT = 2,
00599         DIFF_CHARBONNIER = 3
00600     };
00601 
00602     /** @brief The KAZE constructor
00603 
00604     @param extended Set to enable extraction of extended (128-byte) descriptor.
00605     @param upright Set to enable use of upright descriptors (non rotation-invariant).
00606     @param threshold Detector response threshold to accept point
00607     @param nOctaves Maximum octave evolution of the image
00608     @param nOctaveLayers Default number of sublevels per scale level
00609     @param diffusivity Diffusivity type. DIFF_PM_G1, DIFF_PM_G2, DIFF_WEICKERT or
00610     DIFF_CHARBONNIER
00611      */
00612     CV_WRAP static Ptr<KAZE> create(bool extended=false, bool upright=false,
00613                                     float threshold = 0.001f,
00614                                     int nOctaves = 4, int nOctaveLayers = 4,
00615                                     int diffusivity = KAZE::DIFF_PM_G2);
00616 
00617     CV_WRAP virtual void setExtended(bool extended) = 0;
00618     CV_WRAP virtual bool getExtended() const = 0;
00619 
00620     CV_WRAP virtual void setUpright(bool upright) = 0;
00621     CV_WRAP virtual bool getUpright() const = 0;
00622 
00623     CV_WRAP virtual void setThreshold(double threshold) = 0;
00624     CV_WRAP virtual double getThreshold() const = 0;
00625 
00626     CV_WRAP virtual void setNOctaves(int octaves) = 0;
00627     CV_WRAP virtual int getNOctaves() const = 0;
00628 
00629     CV_WRAP virtual void setNOctaveLayers(int octaveLayers) = 0;
00630     CV_WRAP virtual int getNOctaveLayers() const = 0;
00631 
00632     CV_WRAP virtual void setDiffusivity(int diff) = 0;
00633     CV_WRAP virtual int getDiffusivity() const = 0;
00634 };
00635 
00636 /** @brief Class implementing the AKAZE keypoint detector and descriptor extractor, described in @cite ANB13 . :
00637 
00638 @note AKAZE descriptors can only be used with KAZE or AKAZE keypoints. Try to avoid using *extract*
00639 and *detect* instead of *operator()* due to performance reasons. .. [ANB13] Fast Explicit Diffusion
00640 for Accelerated Features in Nonlinear Scale Spaces. Pablo F. Alcantarilla, Jesús Nuevo and Adrien
00641 Bartoli. In British Machine Vision Conference (BMVC), Bristol, UK, September 2013.
00642  */
00643 class CV_EXPORTS_W AKAZE : public Feature2D
00644 {
00645 public:
00646     // AKAZE descriptor type
00647     enum
00648     {
00649         DESCRIPTOR_KAZE_UPRIGHT = 2, ///< Upright descriptors, not invariant to rotation
00650         DESCRIPTOR_KAZE = 3,
00651         DESCRIPTOR_MLDB_UPRIGHT = 4, ///< Upright descriptors, not invariant to rotation
00652         DESCRIPTOR_MLDB = 5
00653     };
00654 
00655     /** @brief The AKAZE constructor
00656 
00657     @param descriptor_type Type of the extracted descriptor: DESCRIPTOR_KAZE,
00658     DESCRIPTOR_KAZE_UPRIGHT, DESCRIPTOR_MLDB or DESCRIPTOR_MLDB_UPRIGHT.
00659     @param descriptor_size Size of the descriptor in bits. 0 -> Full size
00660     @param descriptor_channels Number of channels in the descriptor (1, 2, 3)
00661     @param threshold Detector response threshold to accept point
00662     @param nOctaves Maximum octave evolution of the image
00663     @param nOctaveLayers Default number of sublevels per scale level
00664     @param diffusivity Diffusivity type. DIFF_PM_G1, DIFF_PM_G2, DIFF_WEICKERT or
00665     DIFF_CHARBONNIER
00666      */
00667     CV_WRAP static Ptr<AKAZE> create(int descriptor_type=AKAZE::DESCRIPTOR_MLDB,
00668                                      int descriptor_size = 0, int descriptor_channels = 3,
00669                                      float threshold = 0.001f, int nOctaves = 4,
00670                                      int nOctaveLayers = 4, int diffusivity = KAZE::DIFF_PM_G2);
00671 
00672     CV_WRAP virtual void setDescriptorType(int dtype) = 0;
00673     CV_WRAP virtual int getDescriptorType() const = 0;
00674 
00675     CV_WRAP virtual void setDescriptorSize(int dsize) = 0;
00676     CV_WRAP virtual int getDescriptorSize() const = 0;
00677 
00678     CV_WRAP virtual void setDescriptorChannels(int dch) = 0;
00679     CV_WRAP virtual int getDescriptorChannels() const = 0;
00680 
00681     CV_WRAP virtual void setThreshold(double threshold) = 0;
00682     CV_WRAP virtual double getThreshold() const = 0;
00683 
00684     CV_WRAP virtual void setNOctaves(int octaves) = 0;
00685     CV_WRAP virtual int getNOctaves() const = 0;
00686 
00687     CV_WRAP virtual void setNOctaveLayers(int octaveLayers) = 0;
00688     CV_WRAP virtual int getNOctaveLayers() const = 0;
00689 
00690     CV_WRAP virtual void setDiffusivity(int diff) = 0;
00691     CV_WRAP virtual int getDiffusivity() const = 0;
00692 };
00693 
00694 //! @} features2d_main
00695 
00696 /****************************************************************************************\
00697 *                                      Distance                                          *
00698 \****************************************************************************************/
00699 
00700 template<typename T>
00701 struct CV_EXPORTS Accumulator
00702 {
00703     typedef T Type;
00704 };
00705 
00706 template<> struct Accumulator<unsigned char>  { typedef float Type; };
00707 template<> struct Accumulator<unsigned short> { typedef float Type; };
00708 template<> struct Accumulator<char>   { typedef float Type; };
00709 template<> struct Accumulator<short>  { typedef float Type; };
00710 
00711 /*
00712  * Squared Euclidean distance functor
00713  */
00714 template<class T>
00715 struct CV_EXPORTS SL2
00716 {
00717     enum { normType = NORM_L2SQR };
00718     typedef T ValueType;
00719     typedef typename Accumulator<T>::Type ResultType;
00720 
00721     ResultType operator()( const T* a, const T* b, int size ) const
00722     {
00723         return normL2Sqr<ValueType, ResultType>(a, b, size);
00724     }
00725 };
00726 
00727 /*
00728  * Euclidean distance functor
00729  */
00730 template<class T>
00731 struct CV_EXPORTS L2
00732 {
00733     enum { normType = NORM_L2 };
00734     typedef T ValueType;
00735     typedef typename Accumulator<T>::Type ResultType;
00736 
00737     ResultType operator()( const T* a, const T* b, int size ) const
00738     {
00739         return (ResultType)std::sqrt((double)normL2Sqr<ValueType, ResultType>(a, b, size));
00740     }
00741 };
00742 
00743 /*
00744  * Manhattan distance (city block distance) functor
00745  */
00746 template<class T>
00747 struct CV_EXPORTS L1
00748 {
00749     enum { normType = NORM_L1 };
00750     typedef T ValueType;
00751     typedef typename Accumulator<T>::Type ResultType;
00752 
00753     ResultType operator()( const T* a, const T* b, int size ) const
00754     {
00755         return normL1<ValueType, ResultType>(a, b, size);
00756     }
00757 };
00758 
00759 /****************************************************************************************\
00760 *                                  DescriptorMatcher                                     *
00761 \****************************************************************************************/
00762 
00763 //! @addtogroup features2d_match
00764 //! @{
00765 
00766 /** @brief Abstract base class for matching keypoint descriptors.
00767 
00768 It has two groups of match methods: for matching descriptors of an image with another image or with
00769 an image set.
00770  */
00771 class CV_EXPORTS_W DescriptorMatcher : public Algorithm
00772 {
00773 public:
00774     virtual ~DescriptorMatcher();
00775 
00776     /** @brief Adds descriptors to train a CPU(trainDescCollectionis) or GPU(utrainDescCollectionis) descriptor
00777     collection.
00778 
00779     If the collection is not empty, the new descriptors are added to existing train descriptors.
00780 
00781     @param descriptors Descriptors to add. Each descriptors[i] is a set of descriptors from the same
00782     train image.
00783      */
00784     CV_WRAP virtual void add( InputArrayOfArrays descriptors );
00785 
00786     /** @brief Returns a constant link to the train descriptor collection trainDescCollection .
00787      */
00788     CV_WRAP const std::vector<Mat>& getTrainDescriptors() const;
00789 
00790     /** @brief Clears the train descriptor collections.
00791      */
00792     CV_WRAP virtual void clear();
00793 
00794     /** @brief Returns true if there are no train descriptors in the both collections.
00795      */
00796     CV_WRAP virtual bool empty() const;
00797 
00798     /** @brief Returns true if the descriptor matcher supports masking permissible matches.
00799      */
00800     CV_WRAP virtual bool isMaskSupported() const = 0;
00801 
00802     /** @brief Trains a descriptor matcher
00803 
00804     Trains a descriptor matcher (for example, the flann index). In all methods to match, the method
00805     train() is run every time before matching. Some descriptor matchers (for example, BruteForceMatcher)
00806     have an empty implementation of this method. Other matchers really train their inner structures (for
00807     example, FlannBasedMatcher trains flann::Index ).
00808      */
00809     CV_WRAP virtual void train();
00810 
00811     /** @brief Finds the best match for each descriptor from a query set.
00812 
00813     @param queryDescriptors Query set of descriptors.
00814     @param trainDescriptors Train set of descriptors. This set is not added to the train descriptors
00815     collection stored in the class object.
00816     @param matches Matches. If a query descriptor is masked out in mask , no match is added for this
00817     descriptor. So, matches size may be smaller than the query descriptors count.
00818     @param mask Mask specifying permissible matches between an input query and train matrices of
00819     descriptors.
00820 
00821     In the first variant of this method, the train descriptors are passed as an input argument. In the
00822     second variant of the method, train descriptors collection that was set by DescriptorMatcher::add is
00823     used. Optional mask (or masks) can be passed to specify which query and training descriptors can be
00824     matched. Namely, queryDescriptors[i] can be matched with trainDescriptors[j] only if
00825     mask.at<uchar>(i,j) is non-zero.
00826      */
00827     CV_WRAP void match( InputArray queryDescriptors, InputArray trainDescriptors,
00828                 CV_OUT std::vector<DMatch>& matches, InputArray mask=noArray() ) const;
00829 
00830     /** @brief Finds the k best matches for each descriptor from a query set.
00831 
00832     @param queryDescriptors Query set of descriptors.
00833     @param trainDescriptors Train set of descriptors. This set is not added to the train descriptors
00834     collection stored in the class object.
00835     @param mask Mask specifying permissible matches between an input query and train matrices of
00836     descriptors.
00837     @param matches Matches. Each matches[i] is k or less matches for the same query descriptor.
00838     @param k Count of best matches found per each query descriptor or less if a query descriptor has
00839     less than k possible matches in total.
00840     @param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
00841     false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
00842     the matches vector does not contain matches for fully masked-out query descriptors.
00843 
00844     These extended variants of DescriptorMatcher::match methods find several best matches for each query
00845     descriptor. The matches are returned in the distance increasing order. See DescriptorMatcher::match
00846     for the details about query and train descriptors.
00847      */
00848     CV_WRAP void knnMatch( InputArray queryDescriptors, InputArray trainDescriptors,
00849                    CV_OUT std::vector<std::vector<DMatch> >& matches, int k,
00850                    InputArray mask=noArray(), bool compactResult=false ) const;
00851 
00852     /** @brief For each query descriptor, finds the training descriptors not farther than the specified distance.
00853 
00854     @param queryDescriptors Query set of descriptors.
00855     @param trainDescriptors Train set of descriptors. This set is not added to the train descriptors
00856     collection stored in the class object.
00857     @param matches Found matches.
00858     @param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
00859     false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
00860     the matches vector does not contain matches for fully masked-out query descriptors.
00861     @param maxDistance Threshold for the distance between matched descriptors. Distance means here
00862     metric distance (e.g. Hamming distance), not the distance between coordinates (which is measured
00863     in Pixels)!
00864     @param mask Mask specifying permissible matches between an input query and train matrices of
00865     descriptors.
00866 
00867     For each query descriptor, the methods find such training descriptors that the distance between the
00868     query descriptor and the training descriptor is equal or smaller than maxDistance. Found matches are
00869     returned in the distance increasing order.
00870      */
00871     void radiusMatch( InputArray queryDescriptors, InputArray trainDescriptors,
00872                       std::vector<std::vector<DMatch> >& matches, float maxDistance,
00873                       InputArray mask=noArray(), bool compactResult=false ) const;
00874 
00875     /** @overload
00876     @param queryDescriptors Query set of descriptors.
00877     @param matches Matches. If a query descriptor is masked out in mask , no match is added for this
00878     descriptor. So, matches size may be smaller than the query descriptors count.
00879     @param masks Set of masks. Each masks[i] specifies permissible matches between the input query
00880     descriptors and stored train descriptors from the i-th image trainDescCollection[i].
00881     */
00882     CV_WRAP void match( InputArray queryDescriptors, CV_OUT std::vector<DMatch>& matches,
00883                         InputArrayOfArrays masks=noArray() );
00884     /** @overload
00885     @param queryDescriptors Query set of descriptors.
00886     @param matches Matches. Each matches[i] is k or less matches for the same query descriptor.
00887     @param k Count of best matches found per each query descriptor or less if a query descriptor has
00888     less than k possible matches in total.
00889     @param masks Set of masks. Each masks[i] specifies permissible matches between the input query
00890     descriptors and stored train descriptors from the i-th image trainDescCollection[i].
00891     @param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
00892     false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
00893     the matches vector does not contain matches for fully masked-out query descriptors.
00894     */
00895     CV_WRAP void knnMatch( InputArray queryDescriptors, CV_OUT std::vector<std::vector<DMatch> >& matches, int k,
00896                            InputArrayOfArrays masks=noArray(), bool compactResult=false );
00897     /** @overload
00898     @param queryDescriptors Query set of descriptors.
00899     @param matches Found matches.
00900     @param maxDistance Threshold for the distance between matched descriptors. Distance means here
00901     metric distance (e.g. Hamming distance), not the distance between coordinates (which is measured
00902     in Pixels)!
00903     @param masks Set of masks. Each masks[i] specifies permissible matches between the input query
00904     descriptors and stored train descriptors from the i-th image trainDescCollection[i].
00905     @param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
00906     false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
00907     the matches vector does not contain matches for fully masked-out query descriptors.
00908     */
00909     void radiusMatch( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance,
00910                       InputArrayOfArrays masks=noArray(), bool compactResult=false );
00911 
00912     // Reads matcher object from a file node
00913     virtual void read( const FileNode& );
00914     // Writes matcher object to a file storage
00915     virtual void write( FileStorage& ) const;
00916 
00917     /** @brief Clones the matcher.
00918 
00919     @param emptyTrainData If emptyTrainData is false, the method creates a deep copy of the object,
00920     that is, copies both parameters and train data. If emptyTrainData is true, the method creates an
00921     object copy with the current parameters but with empty train data.
00922      */
00923     virtual Ptr<DescriptorMatcher>  clone( bool emptyTrainData=false ) const = 0;
00924 
00925     /** @brief Creates a descriptor matcher of a given type with the default parameters (using default
00926     constructor).
00927 
00928     @param descriptorMatcherType Descriptor matcher type. Now the following matcher types are
00929     supported:
00930     -   `BruteForce` (it uses L2 )
00931     -   `BruteForce-L1`
00932     -   `BruteForce-Hamming`
00933     -   `BruteForce-Hamming(2)`
00934     -   `FlannBased`
00935      */
00936     CV_WRAP static Ptr<DescriptorMatcher>  create( const String& descriptorMatcherType );
00937 protected:
00938     /**
00939      * Class to work with descriptors from several images as with one merged matrix.
00940      * It is used e.g. in FlannBasedMatcher.
00941      */
00942     class CV_EXPORTS DescriptorCollection
00943     {
00944     public:
00945         DescriptorCollection();
00946         DescriptorCollection( const DescriptorCollection& collection );
00947         virtual ~DescriptorCollection();
00948 
00949         // Vector of matrices "descriptors" will be merged to one matrix "mergedDescriptors" here.
00950         void set( const std::vector<Mat>& descriptors );
00951         virtual void clear();
00952 
00953         const Mat& getDescriptors() const;
00954         const Mat getDescriptor( int imgIdx, int localDescIdx ) const;
00955         const Mat getDescriptor( int globalDescIdx ) const;
00956         void getLocalIdx( int globalDescIdx, int& imgIdx, int& localDescIdx ) const;
00957 
00958         int size() const;
00959 
00960     protected:
00961         Mat mergedDescriptors;
00962         std::vector<int> startIdxs;
00963     };
00964 
00965     //! In fact the matching is implemented only by the following two methods. These methods suppose
00966     //! that the class object has been trained already. Public match methods call these methods
00967     //! after calling train().
00968     virtual void knnMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, int k,
00969         InputArrayOfArrays masks=noArray(), bool compactResult=false ) = 0;
00970     virtual void radiusMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance,
00971         InputArrayOfArrays masks=noArray(), bool compactResult=false ) = 0;
00972 
00973     static bool isPossibleMatch( InputArray mask, int queryIdx, int trainIdx );
00974     static bool isMaskedOut( InputArrayOfArrays masks, int queryIdx );
00975 
00976     static Mat clone_op( Mat m ) { return m.clone(); }
00977     void checkMasks( InputArrayOfArrays masks, int queryDescriptorsCount ) const;
00978 
00979     //! Collection of descriptors from train images.
00980     std::vector<Mat> trainDescCollection;
00981     std::vector<UMat> utrainDescCollection;
00982 };
00983 
00984 /** @brief Brute-force descriptor matcher.
00985 
00986 For each descriptor in the first set, this matcher finds the closest descriptor in the second set
00987 by trying each one. This descriptor matcher supports masking permissible matches of descriptor
00988 sets.
00989  */
00990 class CV_EXPORTS_W BFMatcher : public DescriptorMatcher
00991 {
00992 public:
00993     /** @brief Brute-force matcher constructor.
00994 
00995     @param normType One of NORM_L1, NORM_L2, NORM_HAMMING, NORM_HAMMING2. L1 and L2 norms are
00996     preferable choices for SIFT and SURF descriptors, NORM_HAMMING should be used with ORB, BRISK and
00997     BRIEF, NORM_HAMMING2 should be used with ORB when WTA_K==3 or 4 (see ORB::ORB constructor
00998     description).
00999     @param crossCheck If it is false, this is will be default BFMatcher behaviour when it finds the k
01000     nearest neighbors for each query descriptor. If crossCheck==true, then the knnMatch() method with
01001     k=1 will only return pairs (i,j) such that for i-th query descriptor the j-th descriptor in the
01002     matcher's collection is the nearest and vice versa, i.e. the BFMatcher will only return consistent
01003     pairs. Such technique usually produces best results with minimal number of outliers when there are
01004     enough matches. This is alternative to the ratio test, used by D. Lowe in SIFT paper.
01005      */
01006     CV_WRAP BFMatcher( int normType=NORM_L2, bool crossCheck=false );
01007     virtual ~BFMatcher() {}
01008 
01009     virtual bool isMaskSupported() const { return true; }
01010 
01011     virtual Ptr<DescriptorMatcher>  clone( bool emptyTrainData=false ) const;
01012 protected:
01013     virtual void knnMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, int k,
01014         InputArrayOfArrays masks=noArray(), bool compactResult=false );
01015     virtual void radiusMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance,
01016         InputArrayOfArrays masks=noArray(), bool compactResult=false );
01017 
01018     int normType;
01019     bool crossCheck;
01020 };
01021 
01022 
01023 /** @brief Flann-based descriptor matcher.
01024 
01025 This matcher trains flann::Index_ on a train descriptor collection and calls its nearest search
01026 methods to find the best matches. So, this matcher may be faster when matching a large train
01027 collection than the brute force matcher. FlannBasedMatcher does not support masking permissible
01028 matches of descriptor sets because flann::Index does not support this. :
01029  */
01030 class CV_EXPORTS_W FlannBasedMatcher : public DescriptorMatcher
01031 {
01032 public:
01033     CV_WRAP FlannBasedMatcher( const Ptr<flann::IndexParams> & indexParams=makePtr<flann::KDTreeIndexParams>(),
01034                        const Ptr<flann::SearchParams> & searchParams=makePtr<flann::SearchParams>() );
01035 
01036     virtual void add( InputArrayOfArrays descriptors );
01037     virtual void clear();
01038 
01039     // Reads matcher object from a file node
01040     virtual void read( const FileNode& );
01041     // Writes matcher object to a file storage
01042     virtual void write( FileStorage& ) const;
01043 
01044     virtual void train();
01045     virtual bool isMaskSupported() const;
01046 
01047     virtual Ptr<DescriptorMatcher>  clone( bool emptyTrainData=false ) const;
01048 protected:
01049     static void convertToDMatches( const DescriptorCollection& descriptors,
01050                                    const Mat& indices, const Mat& distances,
01051                                    std::vector<std::vector<DMatch> >& matches );
01052 
01053     virtual void knnMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, int k,
01054         InputArrayOfArrays masks=noArray(), bool compactResult=false );
01055     virtual void radiusMatchImpl( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance,
01056         InputArrayOfArrays masks=noArray(), bool compactResult=false );
01057 
01058     Ptr<flann::IndexParams>  indexParams;
01059     Ptr<flann::SearchParams>  searchParams;
01060     Ptr<flann::Index>  flannIndex;
01061 
01062     DescriptorCollection mergedDescriptors;
01063     int addedDescCount;
01064 };
01065 
01066 //! @} features2d_match
01067 
01068 /****************************************************************************************\
01069 *                                   Drawing functions                                    *
01070 \****************************************************************************************/
01071 
01072 //! @addtogroup features2d_draw
01073 //! @{
01074 
01075 struct CV_EXPORTS DrawMatchesFlags
01076 {
01077     enum{ DEFAULT = 0, //!< Output image matrix will be created (Mat::create),
01078                        //!< i.e. existing memory of output image may be reused.
01079                        //!< Two source image, matches and single keypoints will be drawn.
01080                        //!< For each keypoint only the center point will be drawn (without
01081                        //!< the circle around keypoint with keypoint size and orientation).
01082           DRAW_OVER_OUTIMG = 1, //!< Output image matrix will not be created (Mat::create).
01083                                 //!< Matches will be drawn on existing content of output image.
01084           NOT_DRAW_SINGLE_POINTS = 2, //!< Single keypoints will not be drawn.
01085           DRAW_RICH_KEYPOINTS = 4 //!< For each keypoint the circle around keypoint with keypoint size and
01086                                   //!< orientation will be drawn.
01087         };
01088 };
01089 
01090 /** @brief Draws keypoints.
01091 
01092 @param image Source image.
01093 @param keypoints Keypoints from the source image.
01094 @param outImage Output image. Its content depends on the flags value defining what is drawn in the
01095 output image. See possible flags bit values below.
01096 @param color Color of keypoints.
01097 @param flags Flags setting drawing features. Possible flags bit values are defined by
01098 DrawMatchesFlags. See details above in drawMatches .
01099 
01100 @note
01101 For Python API, flags are modified as cv2.DRAW_MATCHES_FLAGS_DEFAULT,
01102 cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS, cv2.DRAW_MATCHES_FLAGS_DRAW_OVER_OUTIMG,
01103 cv2.DRAW_MATCHES_FLAGS_NOT_DRAW_SINGLE_POINTS
01104  */
01105 CV_EXPORTS_W void drawKeypoints( InputArray image, const std::vector<KeyPoint>& keypoints, InputOutputArray outImage,
01106                                const Scalar& color=Scalar::all(-1), int flags=DrawMatchesFlags::DEFAULT );
01107 
01108 /** @brief Draws the found matches of keypoints from two images.
01109 
01110 @param img1 First source image.
01111 @param keypoints1 Keypoints from the first source image.
01112 @param img2 Second source image.
01113 @param keypoints2 Keypoints from the second source image.
01114 @param matches1to2 Matches from the first image to the second one, which means that keypoints1[i]
01115 has a corresponding point in keypoints2[matches[i]] .
01116 @param outImg Output image. Its content depends on the flags value defining what is drawn in the
01117 output image. See possible flags bit values below.
01118 @param matchColor Color of matches (lines and connected keypoints). If matchColor==Scalar::all(-1)
01119 , the color is generated randomly.
01120 @param singlePointColor Color of single keypoints (circles), which means that keypoints do not
01121 have the matches. If singlePointColor==Scalar::all(-1) , the color is generated randomly.
01122 @param matchesMask Mask determining which matches are drawn. If the mask is empty, all matches are
01123 drawn.
01124 @param flags Flags setting drawing features. Possible flags bit values are defined by
01125 DrawMatchesFlags.
01126 
01127 This function draws matches of keypoints from two images in the output image. Match is a line
01128 connecting two keypoints (circles). See cv::DrawMatchesFlags.
01129  */
01130 CV_EXPORTS_W void drawMatches( InputArray img1, const std::vector<KeyPoint>& keypoints1,
01131                              InputArray img2, const std::vector<KeyPoint>& keypoints2,
01132                              const std::vector<DMatch>& matches1to2, InputOutputArray outImg,
01133                              const Scalar& matchColor=Scalar::all(-1), const Scalar& singlePointColor=Scalar::all(-1),
01134                              const std::vector<char>& matchesMask=std::vector<char>(), int flags=DrawMatchesFlags::DEFAULT );
01135 
01136 /** @overload */
01137 CV_EXPORTS_AS(drawMatchesKnn) void drawMatches( InputArray img1, const std::vector<KeyPoint>& keypoints1,
01138                              InputArray img2, const std::vector<KeyPoint>& keypoints2,
01139                              const std::vector<std::vector<DMatch> >& matches1to2, InputOutputArray outImg,
01140                              const Scalar& matchColor=Scalar::all(-1), const Scalar& singlePointColor=Scalar::all(-1),
01141                              const std::vector<std::vector<char> >& matchesMask=std::vector<std::vector<char> >(), int flags=DrawMatchesFlags::DEFAULT );
01142 
01143 //! @} features2d_draw
01144 
01145 /****************************************************************************************\
01146 *   Functions to evaluate the feature detectors and [generic] descriptor extractors      *
01147 \****************************************************************************************/
01148 
01149 CV_EXPORTS void evaluateFeatureDetector( const Mat& img1, const Mat& img2, const Mat& H1to2,
01150                                          std::vector<KeyPoint>* keypoints1, std::vector<KeyPoint>* keypoints2,
01151                                          float& repeatability, int& correspCount,
01152                                          const Ptr<FeatureDetector>& fdetector=Ptr<FeatureDetector>() );
01153 
01154 CV_EXPORTS void computeRecallPrecisionCurve( const std::vector<std::vector<DMatch> >& matches1to2,
01155                                              const std::vector<std::vector<uchar> >& correctMatches1to2Mask,
01156                                              std::vector<Point2f>& recallPrecisionCurve );
01157 
01158 CV_EXPORTS float getRecall( const std::vector<Point2f>& recallPrecisionCurve, float l_precision );
01159 CV_EXPORTS int getNearestPoint( const std::vector<Point2f>& recallPrecisionCurve, float l_precision );
01160 
01161 /****************************************************************************************\
01162 *                                     Bag of visual words                                *
01163 \****************************************************************************************/
01164 
01165 //! @addtogroup features2d_category
01166 //! @{
01167 
01168 /** @brief Abstract base class for training the *bag of visual words* vocabulary from a set of descriptors.
01169 
01170 For details, see, for example, *Visual Categorization with Bags of Keypoints* by Gabriella Csurka,
01171 Christopher R. Dance, Lixin Fan, Jutta Willamowski, Cedric Bray, 2004. :
01172  */
01173 class CV_EXPORTS_W BOWTrainer
01174 {
01175 public:
01176     BOWTrainer();
01177     virtual ~BOWTrainer();
01178 
01179     /** @brief Adds descriptors to a training set.
01180 
01181     @param descriptors Descriptors to add to a training set. Each row of the descriptors matrix is a
01182     descriptor.
01183 
01184     The training set is clustered using clustermethod to construct the vocabulary.
01185      */
01186     CV_WRAP void add( const Mat& descriptors );
01187 
01188     /** @brief Returns a training set of descriptors.
01189     */
01190     CV_WRAP const std::vector<Mat>& getDescriptors() const;
01191 
01192     /** @brief Returns the count of all descriptors stored in the training set.
01193     */
01194     CV_WRAP int descriptorsCount() const;
01195 
01196     CV_WRAP virtual void clear();
01197 
01198     /** @overload */
01199     CV_WRAP virtual Mat cluster() const = 0;
01200 
01201     /** @brief Clusters train descriptors.
01202 
01203     @param descriptors Descriptors to cluster. Each row of the descriptors matrix is a descriptor.
01204     Descriptors are not added to the inner train descriptor set.
01205 
01206     The vocabulary consists of cluster centers. So, this method returns the vocabulary. In the first
01207     variant of the method, train descriptors stored in the object are clustered. In the second variant,
01208     input descriptors are clustered.
01209      */
01210     CV_WRAP virtual Mat cluster( const Mat& descriptors ) const = 0;
01211 
01212 protected:
01213     std::vector<Mat> descriptors;
01214     int size;
01215 };
01216 
01217 /** @brief kmeans -based class to train visual vocabulary using the *bag of visual words* approach. :
01218  */
01219 class CV_EXPORTS_W BOWKMeansTrainer : public BOWTrainer
01220 {
01221 public:
01222     /** @brief The constructor.
01223 
01224     @see cv::kmeans
01225     */
01226     CV_WRAP BOWKMeansTrainer( int clusterCount, const TermCriteria& termcrit=TermCriteria(),
01227                       int attempts=3, int flags=KMEANS_PP_CENTERS );
01228     virtual ~BOWKMeansTrainer();
01229 
01230     // Returns trained vocabulary (i.e. cluster centers).
01231     CV_WRAP virtual Mat cluster() const;
01232     CV_WRAP virtual Mat cluster( const Mat& descriptors ) const;
01233 
01234 protected:
01235 
01236     int clusterCount;
01237     TermCriteria termcrit;
01238     int attempts;
01239     int flags;
01240 };
01241 
01242 /** @brief Class to compute an image descriptor using the *bag of visual words*.
01243 
01244 Such a computation consists of the following steps:
01245 
01246 1.  Compute descriptors for a given image and its keypoints set.
01247 2.  Find the nearest visual words from the vocabulary for each keypoint descriptor.
01248 3.  Compute the bag-of-words image descriptor as is a normalized histogram of vocabulary words
01249 encountered in the image. The i-th bin of the histogram is a frequency of i-th word of the
01250 vocabulary in the given image.
01251  */
01252 class CV_EXPORTS_W BOWImgDescriptorExtractor
01253 {
01254 public:
01255     /** @brief The constructor.
01256 
01257     @param dextractor Descriptor extractor that is used to compute descriptors for an input image and
01258     its keypoints.
01259     @param dmatcher Descriptor matcher that is used to find the nearest word of the trained vocabulary
01260     for each keypoint descriptor of the image.
01261      */
01262     CV_WRAP BOWImgDescriptorExtractor( const Ptr<DescriptorExtractor> & dextractor,
01263                                const Ptr<DescriptorMatcher> & dmatcher );
01264     /** @overload */
01265     BOWImgDescriptorExtractor( const Ptr<DescriptorMatcher> & dmatcher );
01266     virtual ~BOWImgDescriptorExtractor();
01267 
01268     /** @brief Sets a visual vocabulary.
01269 
01270     @param vocabulary Vocabulary (can be trained using the inheritor of BOWTrainer ). Each row of the
01271     vocabulary is a visual word (cluster center).
01272      */
01273     CV_WRAP void setVocabulary( const Mat& vocabulary );
01274 
01275     /** @brief Returns the set vocabulary.
01276     */
01277     CV_WRAP const Mat& getVocabulary() const;
01278 
01279     /** @brief Computes an image descriptor using the set visual vocabulary.
01280 
01281     @param image Image, for which the descriptor is computed.
01282     @param keypoints Keypoints detected in the input image.
01283     @param imgDescriptor Computed output image descriptor.
01284     @param pointIdxsOfClusters Indices of keypoints that belong to the cluster. This means that
01285     pointIdxsOfClusters[i] are keypoint indices that belong to the i -th cluster (word of vocabulary)
01286     returned if it is non-zero.
01287     @param descriptors Descriptors of the image keypoints that are returned if they are non-zero.
01288      */
01289     void compute( InputArray image, std::vector<KeyPoint>& keypoints, OutputArray imgDescriptor,
01290                   std::vector<std::vector<int> >* pointIdxsOfClusters=0, Mat* descriptors=0 );
01291     /** @overload
01292     @param keypointDescriptors Computed descriptors to match with vocabulary.
01293     @param imgDescriptor Computed output image descriptor.
01294     @param pointIdxsOfClusters Indices of keypoints that belong to the cluster. This means that
01295     pointIdxsOfClusters[i] are keypoint indices that belong to the i -th cluster (word of vocabulary)
01296     returned if it is non-zero.
01297     */
01298     void compute( InputArray keypointDescriptors, OutputArray imgDescriptor,
01299                   std::vector<std::vector<int> >* pointIdxsOfClusters=0 );
01300     // compute() is not constant because DescriptorMatcher::match is not constant
01301 
01302     CV_WRAP_AS(compute) void compute2( const Mat& image, std::vector<KeyPoint>& keypoints, CV_OUT Mat& imgDescriptor )
01303     { compute(image,keypoints,imgDescriptor); }
01304 
01305     /** @brief Returns an image descriptor size if the vocabulary is set. Otherwise, it returns 0.
01306     */
01307     CV_WRAP int descriptorSize() const;
01308 
01309     /** @brief Returns an image descriptor type.
01310      */
01311     CV_WRAP int descriptorType() const;
01312 
01313 protected:
01314     Mat vocabulary;
01315     Ptr<DescriptorExtractor>  dextractor;
01316     Ptr<DescriptorMatcher>  dmatcher;
01317 };
01318 
01319 //! @} features2d_category
01320 
01321 //! @} features2d
01322 
01323 } /* namespace cv */
01324 
01325 #endif
01326