00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
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
00118
00119
00120
00121 class CV_EXPORTS KeyPointsFilter
00122 {
00123 public:
00124 KeyPointsFilter(){}
00125
00126
00127
00128
00129 static void runByImageBorder( vector<KeyPoint>& keypoints, Size imageSize, int borderSize );
00130
00131
00132
00133 static void runByKeypointSize( vector<KeyPoint>& keypoints, float minSize,
00134 float maxSize=FLT_MAX );
00135
00136
00137
00138 static void runByPixelsMask( vector<KeyPoint>& keypoints, const Mat& mask );
00139
00140
00141
00142 static void removeDuplicated( vector<KeyPoint>& keypoints );
00143
00144
00145
00146
00147 static void retainBest( vector<KeyPoint>& keypoints, int npoints );
00148 };
00149
00150
00151
00152
00153
00154
00155
00156 class CV_EXPORTS_W FeatureDetector : public virtual Algorithm
00157 {
00158 public:
00159 virtual ~FeatureDetector();
00160
00161
00162
00163
00164
00165
00166
00167
00168 CV_WRAP void detect( const Mat& image, CV_OUT vector<KeyPoint>& keypoints, const Mat& mask=Mat() ) const;
00169
00170
00171
00172
00173
00174
00175
00176 void detect( const vector<Mat>& images, vector<vector<KeyPoint> >& keypoints, const vector<Mat>& masks=vector<Mat>() ) const;
00177
00178
00179 CV_WRAP virtual bool empty() const;
00180
00181
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
00189
00190
00191
00192 static void removeInvalidPoints( const Mat& mask, vector<KeyPoint>& keypoints );
00193 };
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205 class CV_EXPORTS_W DescriptorExtractor : public virtual Algorithm
00206 {
00207 public:
00208 virtual ~DescriptorExtractor();
00209
00210
00211
00212
00213
00214
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
00220
00221
00222
00223
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
00239
00240 static void removeBorderKeypoints( vector<KeyPoint>& keypoints,
00241 Size imageSize, int borderSize );
00242 };
00243
00244
00245
00246
00247
00248
00249 class CV_EXPORTS_W Feature2D : public FeatureDetector, public DescriptorExtractor
00250 {
00251 public:
00252
00253
00254
00255
00256
00257
00258
00259
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
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
00281 int descriptorSize() const;
00282
00283 int descriptorType() const;
00284
00285
00286 void operator()(InputArray image, InputArray mask, vector<KeyPoint>& keypoints) const;
00287
00288
00289 void operator()( InputArray image, InputArray mask, vector<KeyPoint>& keypoints,
00290 OutputArray descriptors, bool useProvidedKeypoints=false ) const;
00291
00292 AlgorithmInfo* info() const;
00293
00294
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
00299
00300
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
00316 CV_PROP_RW int threshold;
00317 CV_PROP_RW int octaves;
00318
00319
00320 struct BriskPatternPoint{
00321 float x;
00322 float y;
00323 float sigma;
00324 };
00325 struct BriskShortPair{
00326 unsigned int i;
00327 unsigned int j;
00328 };
00329 struct BriskLongPair{
00330 unsigned int i;
00331 unsigned int j;
00332 int weighted_dx;
00333 int weighted_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
00340 BriskPatternPoint* patternPoints_;
00341 unsigned int points_;
00342 float* scaleList_;
00343 unsigned int* sizeList_;
00344 static const unsigned int scales_;
00345 static const float scalerange_;
00346 static const unsigned int n_rot_;
00347
00348
00349 int strings_;
00350 float dMax_;
00351 float dMin_;
00352 BriskShortPair* shortPairs_;
00353 BriskLongPair* longPairs_;
00354 unsigned int noShortPairs_;
00355 unsigned int noLongPairs_;
00356
00357
00358 static const float basicSize_;
00359 };
00360
00361
00365 class CV_EXPORTS_W ORB : public Feature2D
00366 {
00367 public:
00368
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
00375 int descriptorSize() const;
00376
00377 int descriptorType() const;
00378
00379
00380 void operator()(InputArray image, InputArray mask, vector<KeyPoint>& keypoints) const;
00381
00382
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;
00459 bool scaleNormalized;
00460 double patternScale;
00461 int nOctaves;
00462 bool extAll;
00463
00464 double patternScale0;
00465 int nOctaves0;
00466 vector<int> selectedPairs0;
00467
00468 struct PatternPoint
00469 {
00470 float x;
00471 float y;
00472 float sigma;
00473 };
00474
00475 struct DescriptionPair
00476 {
00477 uchar i;
00478 uchar j;
00479 };
00480
00481 struct OrientationPair
00482 {
00483 uchar i;
00484 uchar j;
00485 int weight_dx;
00486 int weight_dy;
00487 };
00488
00489 vector<PatternPoint> patternLookup;
00490 int patternSizes[NB_SCALES];
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 {
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 ¶meters = 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> ¢ers) 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
00690
00691
00692 class CV_EXPORTS_W GridAdaptedFeatureDetector : public FeatureDetector
00693 {
00694 public:
00695
00696
00697
00698
00699
00700
00701
00702 CV_WRAP GridAdaptedFeatureDetector( const Ptr<FeatureDetector>& detector=0,
00703 int maxTotalKeypoints=1000,
00704 int gridRows=4, int gridCols=4 );
00705
00706
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
00722
00723
00724 class CV_EXPORTS_W PyramidAdaptedFeatureDetector : public FeatureDetector
00725 {
00726 public:
00727
00728 CV_WRAP PyramidAdaptedFeatureDetector( const Ptr<FeatureDetector>& detector, int maxLevel=2 );
00729
00730
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
00876
00877
00878
00879
00880
00881
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
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
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
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
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
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
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
00999
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
01031
01032
01033
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;
01044 CV_PROP_RW int trainIdx;
01045 CV_PROP_RW int imgIdx;
01046
01047 CV_PROP_RW float distance;
01048
01049
01050 bool operator<( const DMatch &m ) const
01051 {
01052 return distance < m.distance;
01053 }
01054 };
01055
01056
01057
01058
01059
01060
01061
01062 class CV_EXPORTS_W DescriptorMatcher : public Algorithm
01063 {
01064 public:
01065 virtual ~DescriptorMatcher();
01066
01067
01068
01069
01070
01071 CV_WRAP virtual void add( const vector<Mat>& descriptors );
01072
01073
01074
01075 CV_WRAP const vector<Mat>& getTrainDescriptors() const;
01076
01077
01078
01079 CV_WRAP virtual void clear();
01080
01081
01082
01083
01084 CV_WRAP virtual bool empty() const;
01085
01086
01087
01088 CV_WRAP virtual bool isMaskSupported() const = 0;
01089
01090
01091
01092
01093
01094
01095
01096
01097
01098
01099
01100
01101 CV_WRAP virtual void train();
01102
01103
01104
01105
01106
01107 CV_WRAP void match( const Mat& queryDescriptors, const Mat& trainDescriptors,
01108 CV_OUT vector<DMatch>& matches, const Mat& mask=Mat() ) const;
01109
01110
01111
01112
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
01117
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
01123
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
01133 virtual void read( const FileNode& );
01134
01135 virtual void write( FileStorage& ) const;
01136
01137
01138
01139
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
01146
01147
01148 class CV_EXPORTS DescriptorCollection
01149 {
01150 public:
01151 DescriptorCollection();
01152 DescriptorCollection( const DescriptorCollection& collection );
01153 virtual ~DescriptorCollection();
01154
01155
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
01172
01173
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
01186 vector<Mat> trainDescCollection;
01187 };
01188
01189
01190
01191
01192
01193
01194
01195
01196
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
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
01232 virtual void read( const FileNode& );
01233
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
01261
01262
01263
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
01276
01277
01278
01279
01280
01281
01282
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
01292
01293 virtual void clear();
01294
01295
01296
01297 virtual bool isMaskSupported() = 0;
01298
01299
01300
01301
01302
01303 virtual void train();
01304
01305
01306
01307
01308
01309
01310
01311
01312
01313 void classify( const Mat& queryImage, vector<KeyPoint>& queryKeypoints,
01314 const Mat& trainImage, vector<KeyPoint>& trainKeypoints ) const;
01315
01316 void classify( const Mat& queryImage, vector<KeyPoint>& queryKeypoints );
01317
01318
01319
01320
01321
01322
01323
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
01328
01329
01330
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
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
01342
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
01354 virtual void read( const FileNode& fn );
01355
01356 virtual void write( FileStorage& fs ) const;
01357
01358
01359 virtual bool empty() const;
01360
01361
01362
01363
01364 virtual Ptr<GenericDescriptorMatcher> clone( bool emptyTrainData=false ) const = 0;
01365
01366 static Ptr<GenericDescriptorMatcher> create( const string& genericDescritptorMatcherType,
01367 const string ¶msFilename=string() );
01368
01369 protected:
01370
01371
01372
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
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
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
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
01421
01422
01423
01424
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
01464
01465 struct CV_EXPORTS DrawMatchesFlags
01466 {
01467 enum{ DEFAULT = 0,
01468
01469
01470
01471
01472 DRAW_OVER_OUTIMG = 1,
01473
01474 NOT_DRAW_SINGLE_POINTS = 2,
01475 DRAW_RICH_KEYPOINTS = 4
01476
01477 };
01478 };
01479
01480
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
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
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
01522
01523
01524
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
01540
01541
01542
01543
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
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
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
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
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 }
01601
01602 #endif
01603
01604 #endif
01605
01606