include/opencv2/features2d/features2d.hpp
Go to the documentation of this file.
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/core.hpp"
00047 #include "opencv2/flann/miniflann.hpp"
00048 
00049 #ifdef __cplusplus
00050 #include <limits>
00051 
00052 namespace cv
00053 {
00054 
00055 CV_EXPORTS bool initModule_features2d();
00056 
00069 class CV_EXPORTS_W_SIMPLE KeyPoint
00070 {
00071 public:
00073     CV_WRAP KeyPoint() : pt(0,0), size(0), angle(-1), response(0), octave(0), class_id(-1) {}
00075     KeyPoint(Point2f _pt, float _size, float _angle=-1,
00076             float _response=0, int _octave=0, int _class_id=-1)
00077             : pt(_pt), size(_size), angle(_angle),
00078             response(_response), octave(_octave), class_id(_class_id) {}
00080     CV_WRAP KeyPoint(float x, float y, float _size, float _angle=-1,
00081             float _response=0, int _octave=0, int _class_id=-1)
00082             : pt(x, y), size(_size), angle(_angle),
00083             response(_response), octave(_octave), class_id(_class_id) {}
00084 
00085     size_t hash() const;
00086 
00088     static void convert(const vector<KeyPoint>& keypoints,
00089                         CV_OUT vector<Point2f>& points2f,
00090                         const vector<int>& keypointIndexes=vector<int>());
00092     static void convert(const vector<Point2f>& points2f,
00093                         CV_OUT vector<KeyPoint>& keypoints,
00094                         float size=1, float response=1, int octave=0, int class_id=-1);
00095 
00099     static float overlap(const KeyPoint& kp1, const KeyPoint& kp2);
00100 
00101     CV_PROP_RW Point2f pt; 
00102     CV_PROP_RW float size; 
00103     CV_PROP_RW float angle; 
00104 
00105 
00106     CV_PROP_RW float response; 
00107     CV_PROP_RW int octave; 
00108     CV_PROP_RW int class_id; 
00109 };
00110 
00112 CV_EXPORTS void write(FileStorage& fs, const string& name, const vector<KeyPoint>& keypoints);
00114 CV_EXPORTS void read(const FileNode& node, CV_OUT vector<KeyPoint>& keypoints);
00115 
00116 /*
00117  * A class filters a vector of keypoints.
00118  * Because now it is difficult to provide a convenient interface for all usage scenarios of the keypoints filter class,
00119  * it has only several needed by now static methods.
00120  */
00121 class CV_EXPORTS KeyPointsFilter
00122 {
00123 public:
00124     KeyPointsFilter(){}
00125 
00126     /*
00127      * Remove keypoints within borderPixels of an image edge.
00128      */
00129     static void runByImageBorder( vector<KeyPoint>& keypoints, Size imageSize, int borderSize );
00130     /*
00131      * Remove keypoints of sizes out of range.
00132      */
00133     static void runByKeypointSize( vector<KeyPoint>& keypoints, float minSize,
00134                                    float maxSize=FLT_MAX );
00135     /*
00136      * Remove keypoints from some image by mask for pixels of this image.
00137      */
00138     static void runByPixelsMask( vector<KeyPoint>& keypoints, const Mat& mask );
00139     /*
00140      * Remove duplicated keypoints.
00141      */
00142     static void removeDuplicated( vector<KeyPoint>& keypoints );
00143 
00144     /*
00145      * Retain the specified number of the best keypoints (according to the response)
00146      */
00147     static void retainBest( vector<KeyPoint>& keypoints, int npoints );
00148 };
00149 
00150 
00151 /************************************ Base Classes ************************************/
00152 
00153 /*
00154  * Abstract base class for 2D image feature detectors.
00155  */
00156 class CV_EXPORTS_W FeatureDetector : public virtual Algorithm
00157 {
00158 public:
00159     virtual ~FeatureDetector();
00160 
00161     /*
00162      * Detect keypoints in an image.
00163      * image        The image.
00164      * keypoints    The detected keypoints.
00165      * mask         Mask specifying where to look for keypoints (optional). Must be a char
00166      *              matrix with non-zero values in the region of interest.
00167      */
00168     CV_WRAP void detect( const Mat& image, CV_OUT vector<KeyPoint>& keypoints, const Mat& mask=Mat() ) const;
00169 
00170     /*
00171      * Detect keypoints in an image set.
00172      * images       Image collection.
00173      * keypoints    Collection of keypoints detected in an input images. keypoints[i] is a set of keypoints detected in an images[i].
00174      * masks        Masks for image set. masks[i] is a mask for images[i].
00175      */
00176     void detect( const vector<Mat>& images, vector<vector<KeyPoint> >& keypoints, const vector<Mat>& masks=vector<Mat>() ) const;
00177 
00178     // Return true if detector object is empty
00179     CV_WRAP virtual bool empty() const;
00180 
00181     // Create feature detector by detector name.
00182     CV_WRAP static Ptr<FeatureDetector> create( const string& detectorType );
00183 
00184 protected:
00185     virtual void detectImpl( const Mat& image, vector<KeyPoint>& keypoints, const Mat& mask=Mat() ) const = 0;
00186 
00187     /*
00188      * Remove keypoints that are not in the mask.
00189      * Helper function, useful when wrapping a library call for keypoint detection that
00190      * does not support a mask argument.
00191      */
00192     static void removeInvalidPoints( const Mat& mask, vector<KeyPoint>& keypoints );
00193 };
00194 
00195 
00196 /*
00197  * Abstract base class for computing descriptors for image keypoints.
00198  *
00199  * In this interface we assume a keypoint descriptor can be represented as a
00200  * dense, fixed-dimensional vector of some basic type. Most descriptors used
00201  * in practice follow this pattern, as it makes it very easy to compute
00202  * distances between descriptors. Therefore we represent a collection of
00203  * descriptors as a Mat, where each row is one keypoint descriptor.
00204  */
00205 class CV_EXPORTS_W DescriptorExtractor : public virtual Algorithm
00206 {
00207 public:
00208     virtual ~DescriptorExtractor();
00209 
00210     /*
00211      * Compute the descriptors for a set of keypoints in an image.
00212      * image        The image.
00213      * keypoints    The input keypoints. Keypoints for which a descriptor cannot be computed are removed.
00214      * descriptors  Copmputed descriptors. Row i is the descriptor for keypoint i.
00215      */
00216     CV_WRAP void compute( const Mat& image, CV_OUT CV_IN_OUT vector<KeyPoint>& keypoints, CV_OUT Mat& descriptors ) const;
00217 
00218     /*
00219      * Compute the descriptors for a keypoints collection detected in image collection.
00220      * images       Image collection.
00221      * keypoints    Input keypoints collection. keypoints[i] is keypoints detected in images[i].
00222      *              Keypoints for which a descriptor cannot be computed are removed.
00223      * descriptors  Descriptor collection. descriptors[i] are descriptors computed for set keypoints[i].
00224      */
00225     void compute( const vector<Mat>& images, vector<vector<KeyPoint> >& keypoints, vector<Mat>& descriptors ) const;
00226 
00227     CV_WRAP virtual int descriptorSize() const = 0;
00228     CV_WRAP virtual int descriptorType() const = 0;
00229 
00230     CV_WRAP virtual bool empty() const;
00231 
00232     CV_WRAP static Ptr<DescriptorExtractor> create( const string& descriptorExtractorType );
00233 
00234 protected:
00235     virtual void computeImpl( const Mat& image, vector<KeyPoint>& keypoints, Mat& descriptors ) const = 0;
00236 
00237     /*
00238      * Remove keypoints within borderPixels of an image edge.
00239      */
00240     static void removeBorderKeypoints( vector<KeyPoint>& keypoints,
00241                                       Size imageSize, int borderSize );
00242 };
00243 
00244 
00245 
00246 /*
00247  * Abstract base class for simultaneous 2D feature detection descriptor extraction.
00248  */
00249 class CV_EXPORTS_W Feature2D : public FeatureDetector, public DescriptorExtractor
00250 {
00251 public:
00252     /*
00253      * Detect keypoints in an image.
00254      * image        The image.
00255      * keypoints    The detected keypoints.
00256      * mask         Mask specifying where to look for keypoints (optional). Must be a char
00257      *              matrix with non-zero values in the region of interest.
00258      * useProvidedKeypoints If true, the method will skip the detection phase and will compute
00259      *                      descriptors for the provided keypoints
00260      */
00261     CV_WRAP_AS(detectAndCompute) virtual void operator()( InputArray image, InputArray mask,
00262                                      CV_OUT vector<KeyPoint>& keypoints,
00263                                      OutputArray descriptors,
00264                                      bool useProvidedKeypoints=false ) const = 0;
00265 
00266     // Create feature detector and descriptor extractor by name.
00267     CV_WRAP static Ptr<Feature2D> create( const string& name );
00268 };
00269 
00273 class CV_EXPORTS_W BRISK : public Feature2D
00274 {
00275 public:
00276     CV_WRAP explicit BRISK(int thresh=30, int octaves=3, float patternScale=1.0f);
00277 
00278     virtual ~BRISK();
00279 
00280     // returns the descriptor size in bytes
00281     int descriptorSize() const;
00282     // returns the descriptor type
00283     int descriptorType() const;
00284 
00285     // Compute the BRISK features on an image
00286     void operator()(InputArray image, InputArray mask, vector<KeyPoint>& keypoints) const;
00287 
00288     // Compute the BRISK features and descriptors on an image
00289     void operator()( InputArray image, InputArray mask, vector<KeyPoint>& keypoints,
00290                       OutputArray descriptors, bool useProvidedKeypoints=false ) const;
00291 
00292     AlgorithmInfo* info() const;
00293 
00294     // custom setup
00295     CV_WRAP explicit BRISK(std::vector<float> &radiusList, std::vector<int> &numberList,
00296         float dMax=5.85f, float dMin=8.2f, std::vector<int> indexChange=std::vector<int>());
00297 
00298     // call this to generate the kernel:
00299     // circle of radius r (pixels), with n points;
00300     // short pairings with dMax, long pairings with dMin
00301     CV_WRAP void generateKernel(std::vector<float> &radiusList,
00302         std::vector<int> &numberList, float dMax=5.85f, float dMin=8.2f,
00303         std::vector<int> indexChange=std::vector<int>());
00304 
00305 protected:
00306 
00307     void computeImpl( const Mat& image, vector<KeyPoint>& keypoints, Mat& descriptors ) const;
00308     void detectImpl( const Mat& image, vector<KeyPoint>& keypoints, const Mat& mask=Mat() ) const;
00309 
00310     void computeKeypointsNoOrientation(InputArray image, InputArray mask, vector<KeyPoint>& keypoints) const;
00311     void computeDescriptorsAndOrOrientation(InputArray image, InputArray mask, vector<KeyPoint>& keypoints,
00312                                        OutputArray descriptors, bool doDescriptors, bool doOrientation,
00313                                        bool useProvidedKeypoints) const;
00314 
00315     // Feature parameters
00316     CV_PROP_RW int threshold;
00317     CV_PROP_RW int octaves;
00318 
00319     // some helper structures for the Brisk pattern representation
00320     struct BriskPatternPoint{
00321         float x;         // x coordinate relative to center
00322         float y;         // x coordinate relative to center
00323         float sigma;     // Gaussian smoothing sigma
00324     };
00325     struct BriskShortPair{
00326         unsigned int i;  // index of the first pattern point
00327         unsigned int j;  // index of other pattern point
00328     };
00329     struct BriskLongPair{
00330         unsigned int i;  // index of the first pattern point
00331         unsigned int j;  // index of other pattern point
00332         int weighted_dx; // 1024.0/dx
00333         int weighted_dy; // 1024.0/dy
00334     };
00335     inline int smoothedIntensity(const cv::Mat& image,
00336                 const cv::Mat& integral,const float key_x,
00337                 const float key_y, const unsigned int scale,
00338                 const unsigned int rot, const unsigned int point) const;
00339     // pattern properties
00340     BriskPatternPoint* patternPoints_;     //[i][rotation][scale]
00341     unsigned int points_;                 // total number of collocation points
00342     float* scaleList_;                     // lists the scaling per scale index [scale]
00343     unsigned int* sizeList_;             // lists the total pattern size per scale index [scale]
00344     static const unsigned int scales_;    // scales discretization
00345     static const float scalerange_;     // span of sizes 40->4 Octaves - else, this needs to be adjusted...
00346     static const unsigned int n_rot_;    // discretization of the rotation look-up
00347 
00348     // pairs
00349     int strings_;                        // number of uchars the descriptor consists of
00350     float dMax_;                         // short pair maximum distance
00351     float dMin_;                         // long pair maximum distance
00352     BriskShortPair* shortPairs_;         // d<_dMax
00353     BriskLongPair* longPairs_;             // d>_dMin
00354     unsigned int noShortPairs_;         // number of shortParis
00355     unsigned int noLongPairs_;             // number of longParis
00356 
00357     // general
00358     static const float basicSize_;
00359 };
00360 
00361 
00365 class CV_EXPORTS_W ORB : public Feature2D
00366 {
00367 public:
00368     // the size of the signature in bytes
00369     enum { kBytes = 32, HARRIS_SCORE=0, FAST_SCORE=1 };
00370 
00371     CV_WRAP explicit ORB(int nfeatures = 500, float scaleFactor = 1.2f, int nlevels = 8, int edgeThreshold = 31,
00372         int firstLevel = 0, int WTA_K=2, int scoreType=ORB::HARRIS_SCORE, int patchSize=31 );
00373 
00374     // returns the descriptor size in bytes
00375     int descriptorSize() const;
00376     // returns the descriptor type
00377     int descriptorType() const;
00378 
00379     // Compute the ORB features and descriptors on an image
00380     void operator()(InputArray image, InputArray mask, vector<KeyPoint>& keypoints) const;
00381 
00382     // Compute the ORB features and descriptors on an image
00383     void operator()( InputArray image, InputArray mask, vector<KeyPoint>& keypoints,
00384                      OutputArray descriptors, bool useProvidedKeypoints=false ) const;
00385 
00386     AlgorithmInfo* info() const;
00387 
00388 protected:
00389 
00390     void computeImpl( const Mat& image, vector<KeyPoint>& keypoints, Mat& descriptors ) const;
00391     void detectImpl( const Mat& image, vector<KeyPoint>& keypoints, const Mat& mask=Mat() ) const;
00392 
00393     CV_PROP_RW int nfeatures;
00394     CV_PROP_RW double scaleFactor;
00395     CV_PROP_RW int nlevels;
00396     CV_PROP_RW int edgeThreshold;
00397     CV_PROP_RW int firstLevel;
00398     CV_PROP_RW int WTA_K;
00399     CV_PROP_RW int scoreType;
00400     CV_PROP_RW int patchSize;
00401 };
00402 
00403 typedef ORB OrbFeatureDetector;
00404 typedef ORB OrbDescriptorExtractor;
00405 
00409 class CV_EXPORTS FREAK : public DescriptorExtractor
00410 {
00411 public:
00419     explicit FREAK( bool orientationNormalized = true,
00420            bool scaleNormalized = true,
00421            float patternScale = 22.0f,
00422            int nOctaves = 4,
00423            const vector<int>& selectedPairs = vector<int>());
00424     FREAK( const FREAK& rhs );
00425     FREAK& operator=( const FREAK& );
00426 
00427     virtual ~FREAK();
00428 
00430     virtual int descriptorSize() const;
00431 
00433     virtual int descriptorType() const;
00434 
00442     vector<int> selectPairs( const vector<Mat>& images, vector<vector<KeyPoint> >& keypoints,
00443                       const double corrThresh = 0.7, bool verbose = true );
00444 
00445     AlgorithmInfo* info() const;
00446 
00447     enum
00448     {
00449         NB_SCALES = 64, NB_PAIRS = 512, NB_ORIENPAIRS = 45
00450     };
00451 
00452 protected:
00453     virtual void computeImpl( const Mat& image, vector<KeyPoint>& keypoints, Mat& descriptors ) const;
00454     void buildPattern();
00455     uchar meanIntensity( const Mat& image, const Mat& integral, const float kp_x, const float kp_y,
00456                          const unsigned int scale, const unsigned int rot, const unsigned int point ) const;
00457 
00458     bool orientationNormalized; //true if the orientation is normalized, false otherwise
00459     bool scaleNormalized; //true if the scale is normalized, false otherwise
00460     double patternScale; //scaling of the pattern
00461     int nOctaves; //number of octaves
00462     bool extAll; // true if all pairs need to be extracted for pairs selection
00463 
00464     double patternScale0;
00465     int nOctaves0;
00466     vector<int> selectedPairs0;
00467 
00468     struct PatternPoint
00469     {
00470         float x; // x coordinate relative to center
00471         float y; // x coordinate relative to center
00472         float sigma; // Gaussian smoothing sigma
00473     };
00474 
00475     struct DescriptionPair
00476     {
00477         uchar i; // index of the first point
00478         uchar j; // index of the second point
00479     };
00480 
00481     struct OrientationPair
00482     {
00483         uchar i; // index of the first point
00484         uchar j; // index of the second point
00485         int weight_dx; // dx/(norm_sq))*4096
00486         int weight_dy; // dy/(norm_sq))*4096
00487     };
00488 
00489     vector<PatternPoint> patternLookup; // look-up table for the pattern points (position+sigma of all points at all scales and orientation)
00490     int patternSizes[NB_SCALES]; // size of the pattern at a specific scale (used to check if a point is within image boundaries)
00491     DescriptionPair descriptionPairs[NB_PAIRS];
00492     OrientationPair orientationPairs[NB_ORIENPAIRS];
00493 };
00494 
00495 
00505 class CV_EXPORTS_W MSER : public FeatureDetector
00506 {
00507 public:
00509     CV_WRAP explicit MSER( int _delta=5, int _min_area=60, int _max_area=14400,
00510           double _max_variation=0.25, double _min_diversity=.2,
00511           int _max_evolution=200, double _area_threshold=1.01,
00512           double _min_margin=0.003, int _edge_blur_size=5 );
00513 
00515     CV_WRAP_AS(detect) void operator()( const Mat& image, CV_OUT vector<vector<Point> >& msers,
00516                                         const Mat& mask=Mat() ) const;
00517     AlgorithmInfo* info() const;
00518 
00519 protected:
00520     void detectImpl( const Mat& image, vector<KeyPoint>& keypoints, const Mat& mask=Mat() ) const;
00521 
00522     int delta;
00523     int minArea;
00524     int maxArea;
00525     double maxVariation;
00526     double minDiversity;
00527     int maxEvolution;
00528     double areaThreshold;
00529     double minMargin;
00530     int edgeBlurSize;
00531 };
00532 
00533 typedef MSER MserFeatureDetector;
00534 
00540 class CV_EXPORTS_W StarDetector : public FeatureDetector
00541 {
00542 public:
00544     CV_WRAP StarDetector(int _maxSize=45, int _responseThreshold=30,
00545                  int _lineThresholdProjected=10,
00546                  int _lineThresholdBinarized=8,
00547                  int _suppressNonmaxSize=5);
00548 
00550     CV_WRAP_AS(detect) void operator()(const Mat& image,
00551                 CV_OUT vector<KeyPoint>& keypoints) const;
00552 
00553     AlgorithmInfo* info() const;
00554 
00555 protected:
00556     void detectImpl( const Mat& image, vector<KeyPoint>& keypoints, const Mat& mask=Mat() ) const;
00557 
00558     int maxSize;
00559     int responseThreshold;
00560     int lineThresholdProjected;
00561     int lineThresholdBinarized;
00562     int suppressNonmaxSize;
00563 };
00564 
00566 CV_EXPORTS void FAST( InputArray image, CV_OUT vector<KeyPoint>& keypoints,
00567                       int threshold, bool nonmaxSupression=true );
00568 
00569 CV_EXPORTS void FASTX( InputArray image, CV_OUT vector<KeyPoint>& keypoints,
00570                       int threshold, bool nonmaxSupression, int type );
00571 
00572 class CV_EXPORTS_W FastFeatureDetector : public FeatureDetector
00573 {
00574 public:
00575 
00576     enum
00577     { // Define it in old class to simplify migration to 2.5
00578       TYPE_5_8 = 0, TYPE_7_12 = 1, TYPE_9_16 = 2
00579     };
00580 
00581     CV_WRAP FastFeatureDetector( int threshold=10, bool nonmaxSuppression=true );
00582     AlgorithmInfo* info() const;
00583 
00584 protected:
00585     virtual void detectImpl( const Mat& image, vector<KeyPoint>& keypoints, const Mat& mask=Mat() ) const;
00586 
00587     int threshold;
00588     bool nonmaxSuppression;
00589 };
00590 
00591 
00592 class CV_EXPORTS GFTTDetector : public FeatureDetector
00593 {
00594 public:
00595     GFTTDetector( int maxCorners=1000, double qualityLevel=0.01, double minDistance=1,
00596                   int blockSize=3, bool useHarrisDetector=false, double k=0.04 );
00597     AlgorithmInfo* info() const;
00598 
00599 protected:
00600     virtual void detectImpl( const Mat& image, vector<KeyPoint>& keypoints, const Mat& mask=Mat() ) const;
00601 
00602     int nfeatures;
00603     double qualityLevel;
00604     double minDistance;
00605     int blockSize;
00606     bool useHarrisDetector;
00607     double k;
00608 };
00609 
00610 typedef GFTTDetector GoodFeaturesToTrackDetector;
00611 typedef StarDetector StarFeatureDetector;
00612 
00613 class CV_EXPORTS_W SimpleBlobDetector : public FeatureDetector
00614 {
00615 public:
00616   struct CV_EXPORTS_W_SIMPLE Params
00617   {
00618       CV_WRAP Params();
00619       CV_PROP_RW float thresholdStep;
00620       CV_PROP_RW float minThreshold;
00621       CV_PROP_RW float maxThreshold;
00622       CV_PROP_RW size_t minRepeatability;
00623       CV_PROP_RW float minDistBetweenBlobs;
00624 
00625       CV_PROP_RW bool filterByColor;
00626       CV_PROP_RW uchar blobColor;
00627 
00628       CV_PROP_RW bool filterByArea;
00629       CV_PROP_RW float minArea, maxArea;
00630 
00631       CV_PROP_RW bool filterByCircularity;
00632       CV_PROP_RW float minCircularity, maxCircularity;
00633 
00634       CV_PROP_RW bool filterByInertia;
00635       CV_PROP_RW float minInertiaRatio, maxInertiaRatio;
00636 
00637       CV_PROP_RW bool filterByConvexity;
00638       CV_PROP_RW float minConvexity, maxConvexity;
00639 
00640       void read( const FileNode& fn );
00641       void write( FileStorage& fs ) const;
00642   };
00643 
00644   CV_WRAP SimpleBlobDetector(const SimpleBlobDetector::Params &parameters = SimpleBlobDetector::Params());
00645 
00646   virtual void read( const FileNode& fn );
00647   virtual void write( FileStorage& fs ) const;
00648 
00649 protected:
00650   struct CV_EXPORTS Center
00651   {
00652       Point2d location;
00653       double radius;
00654       double confidence;
00655   };
00656 
00657   virtual void detectImpl( const Mat& image, vector<KeyPoint>& keypoints, const Mat& mask=Mat() ) const;
00658   virtual void findBlobs(const Mat &image, const Mat &binaryImage, vector<Center> &centers) const;
00659 
00660   Params params;
00661 };
00662 
00663 
00664 class CV_EXPORTS DenseFeatureDetector : public FeatureDetector
00665 {
00666 public:
00667     explicit DenseFeatureDetector( float initFeatureScale=1.f, int featureScaleLevels=1,
00668                                    float featureScaleMul=0.1f,
00669                                    int initXyStep=6, int initImgBound=0,
00670                                    bool varyXyStepWithScale=true,
00671                                    bool varyImgBoundWithScale=false );
00672     AlgorithmInfo* info() const;
00673 
00674 protected:
00675     virtual void detectImpl( const Mat& image, vector<KeyPoint>& keypoints, const Mat& mask=Mat() ) const;
00676 
00677     double initFeatureScale;
00678     int featureScaleLevels;
00679     double featureScaleMul;
00680 
00681     int initXyStep;
00682     int initImgBound;
00683 
00684     bool varyXyStepWithScale;
00685     bool varyImgBoundWithScale;
00686 };
00687 
00688 /*
00689  * Adapts a detector to partition the source image into a grid and detect
00690  * points in each cell.
00691  */
00692 class CV_EXPORTS_W GridAdaptedFeatureDetector : public FeatureDetector
00693 {
00694 public:
00695     /*
00696      * detector            Detector that will be adapted.
00697      * maxTotalKeypoints   Maximum count of keypoints detected on the image. Only the strongest keypoints
00698      *                      will be keeped.
00699      * gridRows            Grid rows count.
00700      * gridCols            Grid column count.
00701      */
00702     CV_WRAP GridAdaptedFeatureDetector( const Ptr<FeatureDetector>& detector=0,
00703                                         int maxTotalKeypoints=1000,
00704                                         int gridRows=4, int gridCols=4 );
00705 
00706     // TODO implement read/write
00707     virtual bool empty() const;
00708 
00709     AlgorithmInfo* info() const;
00710 
00711 protected:
00712     virtual void detectImpl( const Mat& image, vector<KeyPoint>& keypoints, const Mat& mask=Mat() ) const;
00713 
00714     Ptr<FeatureDetector> detector;
00715     int maxTotalKeypoints;
00716     int gridRows;
00717     int gridCols;
00718 };
00719 
00720 /*
00721  * Adapts a detector to detect points over multiple levels of a Gaussian
00722  * pyramid. Useful for detectors that are not inherently scaled.
00723  */
00724 class CV_EXPORTS_W PyramidAdaptedFeatureDetector : public FeatureDetector
00725 {
00726 public:
00727     // maxLevel - The 0-based index of the last pyramid layer
00728     CV_WRAP PyramidAdaptedFeatureDetector( const Ptr<FeatureDetector>& detector, int maxLevel=2 );
00729 
00730     // TODO implement read/write
00731     virtual bool empty() const;
00732 
00733 protected:
00734     virtual void detectImpl( const Mat& image, vector<KeyPoint>& keypoints, const Mat& mask=Mat() ) const;
00735 
00736     Ptr<FeatureDetector> detector;
00737     int maxLevel;
00738 };
00739 
00743 class CV_EXPORTS AdjusterAdapter: public FeatureDetector
00744 {
00745 public:
00748     virtual ~AdjusterAdapter() {}
00753     virtual void tooFew(int min, int n_detected) = 0;
00758     virtual void tooMany(int max, int n_detected) = 0;
00762     virtual bool good() const = 0;
00763 
00764     virtual Ptr<AdjusterAdapter> clone() const = 0;
00765 
00766     static Ptr<AdjusterAdapter> create( const string& detectorType );
00767 };
00780 class CV_EXPORTS DynamicAdaptedFeatureDetector: public FeatureDetector
00781 {
00782 public:
00783 
00790     DynamicAdaptedFeatureDetector( const Ptr<AdjusterAdapter>& adjuster, int min_features=400, int max_features=500, int max_iters=5 );
00791 
00792     virtual bool empty() const;
00793 
00794 protected:
00795     virtual void detectImpl( const Mat& image, vector<KeyPoint>& keypoints, const Mat& mask=Mat() ) const;
00796 
00797 private:
00798     DynamicAdaptedFeatureDetector& operator=(const DynamicAdaptedFeatureDetector&);
00799     DynamicAdaptedFeatureDetector(const DynamicAdaptedFeatureDetector&);
00800 
00801     int escape_iters_;
00802     int min_features_, max_features_;
00803     const Ptr<AdjusterAdapter> adjuster_;
00804 };
00805 
00809 class CV_EXPORTS FastAdjuster: public AdjusterAdapter
00810 {
00811 public:
00815     FastAdjuster(int init_thresh=20, bool nonmax=true, int min_thresh=1, int max_thresh=200);
00816 
00817     virtual void tooFew(int minv, int n_detected);
00818     virtual void tooMany(int maxv, int n_detected);
00819     virtual bool good() const;
00820 
00821     virtual Ptr<AdjusterAdapter> clone() const;
00822 
00823 protected:
00824     virtual void detectImpl( const Mat& image, vector<KeyPoint>& keypoints, const Mat& mask=Mat() ) const;
00825 
00826     int thresh_;
00827     bool nonmax_;
00828     int init_thresh_, min_thresh_, max_thresh_;
00829 };
00830 
00831 
00835 class CV_EXPORTS StarAdjuster: public AdjusterAdapter
00836 {
00837 public:
00838     StarAdjuster(double initial_thresh=30.0, double min_thresh=2., double max_thresh=200.);
00839 
00840     virtual void tooFew(int minv, int n_detected);
00841     virtual void tooMany(int maxv, int n_detected);
00842     virtual bool good() const;
00843 
00844     virtual Ptr<AdjusterAdapter> clone() const;
00845 
00846 protected:
00847     virtual void detectImpl( const Mat& image, vector<KeyPoint>& keypoints, const Mat& mask=Mat() ) const;
00848 
00849     double thresh_, init_thresh_, min_thresh_, max_thresh_;
00850 };
00851 
00852 class CV_EXPORTS SurfAdjuster: public AdjusterAdapter
00853 {
00854 public:
00855     SurfAdjuster( double initial_thresh=400.f, double min_thresh=2, double max_thresh=1000 );
00856 
00857     virtual void tooFew(int minv, int n_detected);
00858     virtual void tooMany(int maxv, int n_detected);
00859     virtual bool good() const;
00860 
00861     virtual Ptr<AdjusterAdapter> clone() const;
00862 
00863 protected:
00864     virtual void detectImpl( const Mat& image, vector<KeyPoint>& keypoints, const Mat& mask=Mat() ) const;
00865 
00866     double thresh_, init_thresh_, min_thresh_, max_thresh_;
00867 };
00868 
00869 CV_EXPORTS Mat windowedMatchingMask( const vector<KeyPoint>& keypoints1, const vector<KeyPoint>& keypoints2,
00870                                      float maxDeltaX, float maxDeltaY );
00871 
00872 
00873 
00874 /*
00875  * OpponentColorDescriptorExtractor
00876  *
00877  * Adapts a descriptor extractor to compute descripors in Opponent Color Space
00878  * (refer to van de Sande et al., CGIV 2008 "Color Descriptors for Object Category Recognition").
00879  * Input RGB image is transformed in Opponent Color Space. Then unadapted descriptor extractor
00880  * (set in constructor) computes descriptors on each of the three channel and concatenate
00881  * them into a single color descriptor.
00882  */
00883 class CV_EXPORTS OpponentColorDescriptorExtractor : public DescriptorExtractor
00884 {
00885 public:
00886     OpponentColorDescriptorExtractor( const Ptr<DescriptorExtractor>& descriptorExtractor );
00887 
00888     virtual void read( const FileNode& );
00889     virtual void write( FileStorage& ) const;
00890 
00891     virtual int descriptorSize() const;
00892     virtual int descriptorType() const;
00893 
00894     virtual bool empty() const;
00895 
00896 protected:
00897     virtual void computeImpl( const Mat& image, vector<KeyPoint>& keypoints, Mat& descriptors ) const;
00898 
00899     Ptr<DescriptorExtractor> descriptorExtractor;
00900 };
00901 
00902 /*
00903  * BRIEF Descriptor
00904  */
00905 class CV_EXPORTS BriefDescriptorExtractor : public DescriptorExtractor
00906 {
00907 public:
00908     static const int PATCH_SIZE = 48;
00909     static const int KERNEL_SIZE = 9;
00910 
00911     // bytes is a length of descriptor in bytes. It can be equal 16, 32 or 64 bytes.
00912     BriefDescriptorExtractor( int bytes = 32 );
00913 
00914     virtual void read( const FileNode& );
00915     virtual void write( FileStorage& ) const;
00916 
00917     virtual int descriptorSize() const;
00918     virtual int descriptorType() const;
00919 
00921 
00922     AlgorithmInfo* info() const;
00923 
00924 protected:
00925     virtual void computeImpl(const Mat& image, vector<KeyPoint>& keypoints, Mat& descriptors) const;
00926 
00927     typedef void(*PixelTestFn)(const Mat&, const vector<KeyPoint>&, Mat&);
00928 
00929     int bytes_;
00930     PixelTestFn test_fn_;
00931 };
00932 
00933 
00934 /****************************************************************************************\
00935 *                                      Distance                                          *
00936 \****************************************************************************************/
00937 
00938 template<typename T>
00939 struct CV_EXPORTS Accumulator
00940 {
00941     typedef T Type;
00942 };
00943 
00944 template<> struct Accumulator<unsigned char>  { typedef float Type; };
00945 template<> struct Accumulator<unsigned short> { typedef float Type; };
00946 template<> struct Accumulator<char>   { typedef float Type; };
00947 template<> struct Accumulator<short>  { typedef float Type; };
00948 
00949 /*
00950  * Squared Euclidean distance functor
00951  */
00952 template<class T>
00953 struct CV_EXPORTS SL2
00954 {
00955     enum { normType = NORM_L2SQR };
00956     typedef T ValueType;
00957     typedef typename Accumulator<T>::Type ResultType;
00958 
00959     ResultType operator()( const T* a, const T* b, int size ) const
00960     {
00961         return normL2Sqr<ValueType, ResultType>(a, b, size);
00962     }
00963 };
00964 
00965 /*
00966  * Euclidean distance functor
00967  */
00968 template<class T>
00969 struct CV_EXPORTS L2
00970 {
00971     enum { normType = NORM_L2 };
00972     typedef T ValueType;
00973     typedef typename Accumulator<T>::Type ResultType;
00974 
00975     ResultType operator()( const T* a, const T* b, int size ) const
00976     {
00977         return (ResultType)sqrt((double)normL2Sqr<ValueType, ResultType>(a, b, size));
00978     }
00979 };
00980 
00981 /*
00982  * Manhattan distance (city block distance) functor
00983  */
00984 template<class T>
00985 struct CV_EXPORTS L1
00986 {
00987     enum { normType = NORM_L1 };
00988     typedef T ValueType;
00989     typedef typename Accumulator<T>::Type ResultType;
00990 
00991     ResultType operator()( const T* a, const T* b, int size ) const
00992     {
00993         return normL1<ValueType, ResultType>(a, b, size);
00994     }
00995 };
00996 
00997 /*
00998  * Hamming distance functor - counts the bit differences between two strings - useful for the Brief descriptor
00999  * bit count of A exclusive XOR'ed with B
01000  */
01001 struct CV_EXPORTS Hamming
01002 {
01003     enum { normType = NORM_HAMMING };
01004     typedef unsigned char ValueType;
01005     typedef int ResultType;
01006 
01009     ResultType operator()( const unsigned char* a, const unsigned char* b, int size ) const
01010     {
01011         return normHamming(a, b, size);
01012     }
01013 };
01014 
01015 typedef Hamming HammingLUT;
01016 
01017 template<int cellsize> struct CV_EXPORTS HammingMultilevel
01018 {
01019     enum { normType = NORM_HAMMING + (cellsize>1) };
01020     typedef unsigned char ValueType;
01021     typedef int ResultType;
01022 
01023     ResultType operator()( const unsigned char* a, const unsigned char* b, int size ) const
01024     {
01025         return normHamming(a, b, size, cellsize);
01026     }
01027 };
01028 
01029 /****************************************************************************************\
01030 *                                      DMatch                                            *
01031 \****************************************************************************************/
01032 /*
01033  * Struct for matching: query descriptor index, train descriptor index, train image index and distance between descriptors.
01034  */
01035 struct CV_EXPORTS_W_SIMPLE DMatch
01036 {
01037     CV_WRAP DMatch() : queryIdx(-1), trainIdx(-1), imgIdx(-1), distance(FLT_MAX) {}
01038     CV_WRAP DMatch( int _queryIdx, int _trainIdx, float _distance ) :
01039             queryIdx(_queryIdx), trainIdx(_trainIdx), imgIdx(-1), distance(_distance) {}
01040     CV_WRAP DMatch( int _queryIdx, int _trainIdx, int _imgIdx, float _distance ) :
01041             queryIdx(_queryIdx), trainIdx(_trainIdx), imgIdx(_imgIdx), distance(_distance) {}
01042 
01043     CV_PROP_RW int queryIdx; // query descriptor index
01044     CV_PROP_RW int trainIdx; // train descriptor index
01045     CV_PROP_RW int imgIdx;   // train image index
01046 
01047     CV_PROP_RW float distance;
01048 
01049     // less is better
01050     bool operator<( const DMatch &m ) const
01051     {
01052         return distance < m.distance;
01053     }
01054 };
01055 
01056 /****************************************************************************************\
01057 *                                  DescriptorMatcher                                     *
01058 \****************************************************************************************/
01059 /*
01060  * Abstract base class for matching two sets of descriptors.
01061  */
01062 class CV_EXPORTS_W DescriptorMatcher : public Algorithm
01063 {
01064 public:
01065     virtual ~DescriptorMatcher();
01066 
01067     /*
01068      * Add descriptors to train descriptor collection.
01069      * descriptors      Descriptors to add. Each descriptors[i] is a descriptors set from one image.
01070      */
01071     CV_WRAP virtual void add( const vector<Mat>& descriptors );
01072     /*
01073      * Get train descriptors collection.
01074      */
01075     CV_WRAP const vector<Mat>& getTrainDescriptors() const;
01076     /*
01077      * Clear train descriptors collection.
01078      */
01079     CV_WRAP virtual void clear();
01080 
01081     /*
01082      * Return true if there are not train descriptors in collection.
01083      */
01084     CV_WRAP virtual bool empty() const;
01085     /*
01086      * Return true if the matcher supports mask in match methods.
01087      */
01088     CV_WRAP virtual bool isMaskSupported() const = 0;
01089 
01090     /*
01091      * Train matcher (e.g. train flann index).
01092      * In all methods to match the method train() is run every time before matching.
01093      * Some descriptor matchers (e.g. BruteForceMatcher) have empty implementation
01094      * of this method, other matchers really train their inner structures
01095      * (e.g. FlannBasedMatcher trains flann::Index). So nonempty implementation
01096      * of train() should check the class object state and do traing/retraining
01097      * only if the state requires that (e.g. FlannBasedMatcher trains flann::Index
01098      * if it has not trained yet or if new descriptors have been added to the train
01099      * collection).
01100      */
01101     CV_WRAP virtual void train();
01102     /*
01103      * Group of methods to match descriptors from image pair.
01104      * Method train() is run in this methods.
01105      */
01106     // Find one best match for each query descriptor (if mask is empty).
01107     CV_WRAP void match( const Mat& queryDescriptors, const Mat& trainDescriptors,
01108                 CV_OUT vector<DMatch>& matches, const Mat& mask=Mat() ) const;
01109     // Find k best matches for each query descriptor (in increasing order of distances).
01110     // compactResult is used when mask is not empty. If compactResult is false matches
01111     // vector will have the same size as queryDescriptors rows. If compactResult is true
01112     // matches vector will not contain matches for fully masked out query descriptors.
01113     CV_WRAP void knnMatch( const Mat& queryDescriptors, const Mat& trainDescriptors,
01114                    CV_OUT vector<vector<DMatch> >& matches, int k,
01115                    const Mat& mask=Mat(), bool compactResult=false ) const;
01116     // Find best matches for each query descriptor which have distance less than
01117     // maxDistance (in increasing order of distances).
01118     void radiusMatch( const Mat& queryDescriptors, const Mat& trainDescriptors,
01119                       vector<vector<DMatch> >& matches, float maxDistance,
01120                       const Mat& mask=Mat(), bool compactResult=false ) const;
01121     /*
01122      * Group of methods to match descriptors from one image to image set.
01123      * See description of similar methods for matching image pair above.
01124      */
01125     CV_WRAP void match( const Mat& queryDescriptors, CV_OUT vector<DMatch>& matches,
01126                 const vector<Mat>& masks=vector<Mat>() );
01127     CV_WRAP void knnMatch( const Mat& queryDescriptors, CV_OUT vector<vector<DMatch> >& matches, int k,
01128            const vector<Mat>& masks=vector<Mat>(), bool compactResult=false );
01129     void radiusMatch( const Mat& queryDescriptors, vector<vector<DMatch> >& matches, float maxDistance,
01130                    const vector<Mat>& masks=vector<Mat>(), bool compactResult=false );
01131 
01132     // Reads matcher object from a file node
01133     virtual void read( const FileNode& );
01134     // Writes matcher object to a file storage
01135     virtual void write( FileStorage& ) const;
01136 
01137     // Clone the matcher. If emptyTrainData is false the method create deep copy of the object, i.e. copies
01138     // both parameters and train data. If emptyTrainData is true the method create object copy with current parameters
01139     // but with empty train data.
01140     virtual Ptr<DescriptorMatcher> clone( bool emptyTrainData=false ) const = 0;
01141 
01142     CV_WRAP static Ptr<DescriptorMatcher> create( const string& descriptorMatcherType );
01143 protected:
01144     /*
01145      * Class to work with descriptors from several images as with one merged matrix.
01146      * It is used e.g. in FlannBasedMatcher.
01147      */
01148     class CV_EXPORTS DescriptorCollection
01149     {
01150     public:
01151         DescriptorCollection();
01152         DescriptorCollection( const DescriptorCollection& collection );
01153         virtual ~DescriptorCollection();
01154 
01155         // Vector of matrices "descriptors" will be merged to one matrix "mergedDescriptors" here.
01156         void set( const vector<Mat>& descriptors );
01157         virtual void clear();
01158 
01159         const Mat& getDescriptors() const;
01160         const Mat getDescriptor( int imgIdx, int localDescIdx ) const;
01161         const Mat getDescriptor( int globalDescIdx ) const;
01162         void getLocalIdx( int globalDescIdx, int& imgIdx, int& localDescIdx ) const;
01163 
01164         int size() const;
01165 
01166     protected:
01167         Mat mergedDescriptors;
01168         vector<int> startIdxs;
01169     };
01170 
01171     // In fact the matching is implemented only by the following two methods. These methods suppose
01172     // that the class object has been trained already. Public match methods call these methods
01173     // after calling train().
01174     virtual void knnMatchImpl( const Mat& queryDescriptors, vector<vector<DMatch> >& matches, int k,
01175            const vector<Mat>& masks=vector<Mat>(), bool compactResult=false ) = 0;
01176     virtual void radiusMatchImpl( const Mat& queryDescriptors, vector<vector<DMatch> >& matches, float maxDistance,
01177            const vector<Mat>& masks=vector<Mat>(), bool compactResult=false ) = 0;
01178 
01179     static bool isPossibleMatch( const Mat& mask, int queryIdx, int trainIdx );
01180     static bool isMaskedOut( const vector<Mat>& masks, int queryIdx );
01181 
01182     static Mat clone_op( Mat m ) { return m.clone(); }
01183     void checkMasks( const vector<Mat>& masks, int queryDescriptorsCount ) const;
01184 
01185     // Collection of descriptors from train images.
01186     vector<Mat> trainDescCollection;
01187 };
01188 
01189 /*
01190  * Brute-force descriptor matcher.
01191  *
01192  * For each descriptor in the first set, this matcher finds the closest
01193  * descriptor in the second set by trying each one.
01194  *
01195  * For efficiency, BruteForceMatcher is templated on the distance metric.
01196  * For float descriptors, a common choice would be cv::L2<float>.
01197  */
01198 class CV_EXPORTS_W BFMatcher : public DescriptorMatcher
01199 {
01200 public:
01201     CV_WRAP BFMatcher( int normType, bool crossCheck=false );
01202     virtual ~BFMatcher() {}
01203 
01204     virtual bool isMaskSupported() const { return true; }
01205 
01206     virtual Ptr<DescriptorMatcher> clone( bool emptyTrainData=false ) const;
01207 
01208 protected:
01209     virtual void knnMatchImpl( const Mat& queryDescriptors, vector<vector<DMatch> >& matches, int k,
01210            const vector<Mat>& masks=vector<Mat>(), bool compactResult=false );
01211     virtual void radiusMatchImpl( const Mat& queryDescriptors, vector<vector<DMatch> >& matches, float maxDistance,
01212            const vector<Mat>& masks=vector<Mat>(), bool compactResult=false );
01213 
01214     int normType;
01215     bool crossCheck;
01216 };
01217 
01218 
01219 /*
01220  * Flann based matcher
01221  */
01222 class CV_EXPORTS_W FlannBasedMatcher : public DescriptorMatcher
01223 {
01224 public:
01225     CV_WRAP FlannBasedMatcher( const Ptr<flann::IndexParams>& indexParams=new flann::KDTreeIndexParams(),
01226                        const Ptr<flann::SearchParams>& searchParams=new flann::SearchParams() );
01227 
01228     virtual void add( const vector<Mat>& descriptors );
01229     virtual void clear();
01230 
01231     // Reads matcher object from a file node
01232     virtual void read( const FileNode& );
01233     // Writes matcher object to a file storage
01234     virtual void write( FileStorage& ) const;
01235 
01236     virtual void train();
01237     virtual bool isMaskSupported() const;
01238 
01239     virtual Ptr<DescriptorMatcher> clone( bool emptyTrainData=false ) const;
01240 
01241 protected:
01242     static void convertToDMatches( const DescriptorCollection& descriptors,
01243                                    const Mat& indices, const Mat& distances,
01244                                    vector<vector<DMatch> >& matches );
01245 
01246     virtual void knnMatchImpl( const Mat& queryDescriptors, vector<vector<DMatch> >& matches, int k,
01247                    const vector<Mat>& masks=vector<Mat>(), bool compactResult=false );
01248     virtual void radiusMatchImpl( const Mat& queryDescriptors, vector<vector<DMatch> >& matches, float maxDistance,
01249                    const vector<Mat>& masks=vector<Mat>(), bool compactResult=false );
01250 
01251     Ptr<flann::IndexParams> indexParams;
01252     Ptr<flann::SearchParams> searchParams;
01253     Ptr<flann::Index> flannIndex;
01254 
01255     DescriptorCollection mergedDescriptors;
01256     int addedDescCount;
01257 };
01258 
01259 /****************************************************************************************\
01260 *                                GenericDescriptorMatcher                                *
01261 \****************************************************************************************/
01262 /*
01263  *   Abstract interface for a keypoint descriptor and matcher
01264  */
01265 class GenericDescriptorMatcher;
01266 typedef GenericDescriptorMatcher GenericDescriptorMatch;
01267 
01268 class CV_EXPORTS GenericDescriptorMatcher
01269 {
01270 public:
01271     GenericDescriptorMatcher();
01272     virtual ~GenericDescriptorMatcher();
01273 
01274     /*
01275      * Add train collection: images and keypoints from them.
01276      * images       A set of train images.
01277      * ketpoints    Keypoint collection that have been detected on train images.
01278      *
01279      * Keypoints for which a descriptor cannot be computed are removed. Such keypoints
01280      * must be filtered in this method befor adding keypoints to train collection "trainPointCollection".
01281      * If inheritor class need perform such prefiltering the method add() must be overloaded.
01282      * In the other class methods programmer has access to the train keypoints by a constant link.
01283      */
01284     virtual void add( const vector<Mat>& images,
01285                       vector<vector<KeyPoint> >& keypoints );
01286 
01287     const vector<Mat>& getTrainImages() const;
01288     const vector<vector<KeyPoint> >& getTrainKeypoints() const;
01289 
01290     /*
01291      * Clear images and keypoints storing in train collection.
01292      */
01293     virtual void clear();
01294     /*
01295      * Returns true if matcher supports mask to match descriptors.
01296      */
01297     virtual bool isMaskSupported() = 0;
01298     /*
01299      * Train some inner structures (e.g. flann index or decision trees).
01300      * train() methods is run every time in matching methods. So the method implementation
01301      * should has a check whether these inner structures need be trained/retrained or not.
01302      */
01303     virtual void train();
01304 
01305     /*
01306      * Classifies query keypoints.
01307      * queryImage    The query image
01308      * queryKeypoints   Keypoints from the query image
01309      * trainImage    The train image
01310      * trainKeypoints   Keypoints from the train image
01311      */
01312     // Classify keypoints from query image under one train image.
01313     void classify( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
01314                            const Mat& trainImage, vector<KeyPoint>& trainKeypoints ) const;
01315     // Classify keypoints from query image under train image collection.
01316     void classify( const Mat& queryImage, vector<KeyPoint>& queryKeypoints );
01317 
01318     /*
01319      * Group of methods to match keypoints from image pair.
01320      * Keypoints for which a descriptor cannot be computed are removed.
01321      * train() method is called here.
01322      */
01323     // Find one best match for each query descriptor (if mask is empty).
01324     void match( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
01325                 const Mat& trainImage, vector<KeyPoint>& trainKeypoints,
01326                 vector<DMatch>& matches, const Mat& mask=Mat() ) const;
01327     // Find k best matches for each query keypoint (in increasing order of distances).
01328     // compactResult is used when mask is not empty. If compactResult is false matches
01329     // vector will have the same size as queryDescriptors rows.
01330     // If compactResult is true matches vector will not contain matches for fully masked out query descriptors.
01331     void knnMatch( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
01332                    const Mat& trainImage, vector<KeyPoint>& trainKeypoints,
01333                    vector<vector<DMatch> >& matches, int k,
01334                    const Mat& mask=Mat(), bool compactResult=false ) const;
01335     // Find best matches for each query descriptor which have distance less than maxDistance (in increasing order of distances).
01336     void radiusMatch( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
01337                       const Mat& trainImage, vector<KeyPoint>& trainKeypoints,
01338                       vector<vector<DMatch> >& matches, float maxDistance,
01339                       const Mat& mask=Mat(), bool compactResult=false ) const;
01340     /*
01341      * Group of methods to match keypoints from one image to image set.
01342      * See description of similar methods for matching image pair above.
01343      */
01344     void match( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
01345                 vector<DMatch>& matches, const vector<Mat>& masks=vector<Mat>() );
01346     void knnMatch( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
01347                    vector<vector<DMatch> >& matches, int k,
01348                    const vector<Mat>& masks=vector<Mat>(), bool compactResult=false );
01349     void radiusMatch( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
01350                       vector<vector<DMatch> >& matches, float maxDistance,
01351                       const vector<Mat>& masks=vector<Mat>(), bool compactResult=false );
01352 
01353     // Reads matcher object from a file node
01354     virtual void read( const FileNode& fn );
01355     // Writes matcher object to a file storage
01356     virtual void write( FileStorage& fs ) const;
01357 
01358     // Return true if matching object is empty (e.g. feature detector or descriptor matcher are empty)
01359     virtual bool empty() const;
01360 
01361     // Clone the matcher. If emptyTrainData is false the method create deep copy of the object, i.e. copies
01362     // both parameters and train data. If emptyTrainData is true the method create object copy with current parameters
01363     // but with empty train data.
01364     virtual Ptr<GenericDescriptorMatcher> clone( bool emptyTrainData=false ) const = 0;
01365 
01366     static Ptr<GenericDescriptorMatcher> create( const string& genericDescritptorMatcherType,
01367                                                  const string &paramsFilename=string() );
01368 
01369 protected:
01370     // In fact the matching is implemented only by the following two methods. These methods suppose
01371     // that the class object has been trained already. Public match methods call these methods
01372     // after calling train().
01373     virtual void knnMatchImpl( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
01374                                vector<vector<DMatch> >& matches, int k,
01375                                const vector<Mat>& masks, bool compactResult ) = 0;
01376     virtual void radiusMatchImpl( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
01377                                   vector<vector<DMatch> >& matches, float maxDistance,
01378                                   const vector<Mat>& masks, bool compactResult ) = 0;
01379     /*
01380      * A storage for sets of keypoints together with corresponding images and class IDs
01381      */
01382     class CV_EXPORTS KeyPointCollection
01383     {
01384     public:
01385         KeyPointCollection();
01386         KeyPointCollection( const KeyPointCollection& collection );
01387         void add( const vector<Mat>& images, const vector<vector<KeyPoint> >& keypoints );
01388         void clear();
01389 
01390         // Returns the total number of keypoints in the collection
01391         size_t keypointCount() const;
01392         size_t imageCount() const;
01393 
01394         const vector<vector<KeyPoint> >& getKeypoints() const;
01395         const vector<KeyPoint>& getKeypoints( int imgIdx ) const;
01396         const KeyPoint& getKeyPoint( int imgIdx, int localPointIdx ) const;
01397         const KeyPoint& getKeyPoint( int globalPointIdx ) const;
01398         void getLocalIdx( int globalPointIdx, int& imgIdx, int& localPointIdx ) const;
01399 
01400         const vector<Mat>& getImages() const;
01401         const Mat& getImage( int imgIdx ) const;
01402 
01403     protected:
01404         int pointCount;
01405 
01406         vector<Mat> images;
01407         vector<vector<KeyPoint> > keypoints;
01408         // global indices of the first points in each image, startIndices.size() = keypoints.size()
01409         vector<int> startIndices;
01410 
01411     private:
01412         static Mat clone_op( Mat m ) { return m.clone(); }
01413     };
01414 
01415     KeyPointCollection trainPointCollection;
01416 };
01417 
01418 
01419 /****************************************************************************************\
01420 *                                VectorDescriptorMatcher                                 *
01421 \****************************************************************************************/
01422 
01423 /*
01424  *  A class used for matching descriptors that can be described as vectors in a finite-dimensional space
01425  */
01426 class VectorDescriptorMatcher;
01427 typedef VectorDescriptorMatcher VectorDescriptorMatch;
01428 
01429 class CV_EXPORTS VectorDescriptorMatcher : public GenericDescriptorMatcher
01430 {
01431 public:
01432     VectorDescriptorMatcher( const Ptr<DescriptorExtractor>& extractor, const Ptr<DescriptorMatcher>& matcher );
01433     virtual ~VectorDescriptorMatcher();
01434 
01435     virtual void add( const vector<Mat>& imgCollection,
01436                       vector<vector<KeyPoint> >& pointCollection );
01437 
01438     virtual void clear();
01439 
01440     virtual void train();
01441 
01442     virtual bool isMaskSupported();
01443 
01444     virtual void read( const FileNode& fn );
01445     virtual void write( FileStorage& fs ) const;
01446     virtual bool empty() const;
01447 
01448     virtual Ptr<GenericDescriptorMatcher> clone( bool emptyTrainData=false ) const;
01449 
01450 protected:
01451     virtual void knnMatchImpl( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
01452                                vector<vector<DMatch> >& matches, int k,
01453                                const vector<Mat>& masks, bool compactResult );
01454     virtual void radiusMatchImpl( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
01455                                   vector<vector<DMatch> >& matches, float maxDistance,
01456                                   const vector<Mat>& masks, bool compactResult );
01457 
01458     Ptr<DescriptorExtractor> extractor;
01459     Ptr<DescriptorMatcher> matcher;
01460 };
01461 
01462 /****************************************************************************************\
01463 *                                   Drawing functions                                    *
01464 \****************************************************************************************/
01465 struct CV_EXPORTS DrawMatchesFlags
01466 {
01467     enum{ DEFAULT = 0, // Output image matrix will be created (Mat::create),
01468                        // i.e. existing memory of output image may be reused.
01469                        // Two source image, matches and single keypoints will be drawn.
01470                        // For each keypoint only the center point will be drawn (without
01471                        // the circle around keypoint with keypoint size and orientation).
01472           DRAW_OVER_OUTIMG = 1, // Output image matrix will not be created (Mat::create).
01473                                 // Matches will be drawn on existing content of output image.
01474           NOT_DRAW_SINGLE_POINTS = 2, // Single keypoints will not be drawn.
01475           DRAW_RICH_KEYPOINTS = 4 // For each keypoint the circle around keypoint with keypoint size and
01476                                   // orientation will be drawn.
01477         };
01478 };
01479 
01480 // Draw keypoints.
01481 CV_EXPORTS_W void drawKeypoints( const Mat& image, const vector<KeyPoint>& keypoints, CV_OUT Mat& outImage,
01482                                const Scalar& color=Scalar::all(-1), int flags=DrawMatchesFlags::DEFAULT );
01483 
01484 // Draws matches of keypints from two images on output image.
01485 CV_EXPORTS void drawMatches( const Mat& img1, const vector<KeyPoint>& keypoints1,
01486                              const Mat& img2, const vector<KeyPoint>& keypoints2,
01487                              const vector<DMatch>& matches1to2, Mat& outImg,
01488                              const Scalar& matchColor=Scalar::all(-1), const Scalar& singlePointColor=Scalar::all(-1),
01489                              const vector<char>& matchesMask=vector<char>(), int flags=DrawMatchesFlags::DEFAULT );
01490 
01491 CV_EXPORTS void drawMatches( const Mat& img1, const vector<KeyPoint>& keypoints1,
01492                              const Mat& img2, const vector<KeyPoint>& keypoints2,
01493                              const vector<vector<DMatch> >& matches1to2, Mat& outImg,
01494                              const Scalar& matchColor=Scalar::all(-1), const Scalar& singlePointColor=Scalar::all(-1),
01495                              const vector<vector<char> >& matchesMask=vector<vector<char> >(), int flags=DrawMatchesFlags::DEFAULT );
01496 
01497 /****************************************************************************************\
01498 *   Functions to evaluate the feature detectors and [generic] descriptor extractors      *
01499 \****************************************************************************************/
01500 
01501 CV_EXPORTS void evaluateFeatureDetector( const Mat& img1, const Mat& img2, const Mat& H1to2,
01502                                          vector<KeyPoint>* keypoints1, vector<KeyPoint>* keypoints2,
01503                                          float& repeatability, int& correspCount,
01504                                          const Ptr<FeatureDetector>& fdetector=Ptr<FeatureDetector>() );
01505 
01506 CV_EXPORTS void computeRecallPrecisionCurve( const vector<vector<DMatch> >& matches1to2,
01507                                              const vector<vector<uchar> >& correctMatches1to2Mask,
01508                                              vector<Point2f>& recallPrecisionCurve );
01509 
01510 CV_EXPORTS float getRecall( const vector<Point2f>& recallPrecisionCurve, float l_precision );
01511 CV_EXPORTS int getNearestPoint( const vector<Point2f>& recallPrecisionCurve, float l_precision );
01512 
01513 CV_EXPORTS void evaluateGenericDescriptorMatcher( const Mat& img1, const Mat& img2, const Mat& H1to2,
01514                                                   vector<KeyPoint>& keypoints1, vector<KeyPoint>& keypoints2,
01515                                                   vector<vector<DMatch> >* matches1to2, vector<vector<uchar> >* correctMatches1to2Mask,
01516                                                   vector<Point2f>& recallPrecisionCurve,
01517                                                   const Ptr<GenericDescriptorMatcher>& dmatch=Ptr<GenericDescriptorMatcher>() );
01518 
01519 
01520 /****************************************************************************************\
01521 *                                     Bag of visual words                                *
01522 \****************************************************************************************/
01523 /*
01524  * Abstract base class for training of a 'bag of visual words' vocabulary from a set of descriptors
01525  */
01526 class CV_EXPORTS BOWTrainer
01527 {
01528 public:
01529     BOWTrainer();
01530     virtual ~BOWTrainer();
01531 
01532     void add( const Mat& descriptors );
01533     const vector<Mat>& getDescriptors() const;
01534     int descripotorsCount() const;
01535 
01536     virtual void clear();
01537 
01538     /*
01539      * Train visual words vocabulary, that is cluster training descriptors and
01540      * compute cluster centers.
01541      * Returns cluster centers.
01542      *
01543      * descriptors      Training descriptors computed on images keypoints.
01544      */
01545     virtual Mat cluster() const = 0;
01546     virtual Mat cluster( const Mat& descriptors ) const = 0;
01547 
01548 protected:
01549     vector<Mat> descriptors;
01550     int size;
01551 };
01552 
01553 /*
01554  * This is BOWTrainer using cv::kmeans to get vocabulary.
01555  */
01556 class CV_EXPORTS BOWKMeansTrainer : public BOWTrainer
01557 {
01558 public:
01559     BOWKMeansTrainer( int clusterCount, const TermCriteria& termcrit=TermCriteria(),
01560                       int attempts=3, int flags=KMEANS_PP_CENTERS );
01561     virtual ~BOWKMeansTrainer();
01562 
01563     // Returns trained vocabulary (i.e. cluster centers).
01564     virtual Mat cluster() const;
01565     virtual Mat cluster( const Mat& descriptors ) const;
01566 
01567 protected:
01568 
01569     int clusterCount;
01570     TermCriteria termcrit;
01571     int attempts;
01572     int flags;
01573 };
01574 
01575 /*
01576  * Class to compute image descriptor using bag of visual words.
01577  */
01578 class CV_EXPORTS BOWImgDescriptorExtractor
01579 {
01580 public:
01581     BOWImgDescriptorExtractor( const Ptr<DescriptorExtractor>& dextractor,
01582                                const Ptr<DescriptorMatcher>& dmatcher );
01583     virtual ~BOWImgDescriptorExtractor();
01584 
01585     void setVocabulary( const Mat& vocabulary );
01586     const Mat& getVocabulary() const;
01587     void compute( const Mat& image, vector<KeyPoint>& keypoints, Mat& imgDescriptor,
01588                   vector<vector<int> >* pointIdxsOfClusters=0, Mat* descriptors=0 );
01589     // compute() is not constant because DescriptorMatcher::match is not constant
01590 
01591     int descriptorSize() const;
01592     int descriptorType() const;
01593 
01594 protected:
01595     Mat vocabulary;
01596     Ptr<DescriptorExtractor> dextractor;
01597     Ptr<DescriptorMatcher> dmatcher;
01598 };
01599 
01600 } /* namespace cv */
01601 
01602 #endif /* __cplusplus */
01603 
01604 #endif
01605 
01606 /* End of file. */