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_OBJDETECT_HPP__
00044 #define __OPENCV_OBJDETECT_HPP__
00045
00046 #include "opencv2/core/core.hpp"
00047 #include "opencv2/features2d/features2d.hpp"
00048
00049 #ifdef __cplusplus
00050 extern "C" {
00051 #endif
00052
00053
00054
00055
00056
00057 #define CV_HAAR_MAGIC_VAL 0x42500000
00058 #define CV_TYPE_NAME_HAAR "opencv-haar-classifier"
00059
00060 #define CV_IS_HAAR_CLASSIFIER( haar ) \
00061 ((haar) != NULL && \
00062 (((const CvHaarClassifierCascade*)(haar))->flags & CV_MAGIC_MASK)==CV_HAAR_MAGIC_VAL)
00063
00064 #define CV_HAAR_FEATURE_MAX 3
00065
00066 typedef struct CvHaarFeature
00067 {
00068 int tilted;
00069 struct
00070 {
00071 CvRect r;
00072 float weight;
00073 } rect[CV_HAAR_FEATURE_MAX];
00074 } CvHaarFeature;
00075
00076 typedef struct CvHaarClassifier
00077 {
00078 int count;
00079 CvHaarFeature* haar_feature;
00080 float* threshold;
00081 int* left;
00082 int* right;
00083 float* alpha;
00084 } CvHaarClassifier;
00085
00086 typedef struct CvHaarStageClassifier
00087 {
00088 int count;
00089 float threshold;
00090 CvHaarClassifier* classifier;
00091
00092 int next;
00093 int child;
00094 int parent;
00095 } CvHaarStageClassifier;
00096
00097 typedef struct CvHidHaarClassifierCascade CvHidHaarClassifierCascade;
00098
00099 typedef struct CvHaarClassifierCascade
00100 {
00101 int flags;
00102 int count;
00103 CvSize orig_window_size;
00104 CvSize real_window_size;
00105 double scale;
00106 CvHaarStageClassifier* stage_classifier;
00107 CvHidHaarClassifierCascade* hid_cascade;
00108 } CvHaarClassifierCascade;
00109
00110 typedef struct CvAvgComp
00111 {
00112 CvRect rect;
00113 int neighbors;
00114 } CvAvgComp;
00115
00116
00117
00118 CVAPI(CvHaarClassifierCascade*) cvLoadHaarClassifierCascade(
00119 const char* directory, CvSize orig_window_size);
00120
00121 CVAPI(void) cvReleaseHaarClassifierCascade( CvHaarClassifierCascade** cascade );
00122
00123 #define CV_HAAR_DO_CANNY_PRUNING 1
00124 #define CV_HAAR_SCALE_IMAGE 2
00125 #define CV_HAAR_FIND_BIGGEST_OBJECT 4
00126 #define CV_HAAR_DO_ROUGH_SEARCH 8
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137 CVAPI(CvSeq*) cvHaarDetectObjects( const CvArr* image,
00138 CvHaarClassifierCascade* cascade, CvMemStorage* storage,
00139 double scale_factor CV_DEFAULT(1.1),
00140 int min_neighbors CV_DEFAULT(3), int flags CV_DEFAULT(0),
00141 CvSize min_size CV_DEFAULT(cvSize(0,0)), CvSize max_size CV_DEFAULT(cvSize(0,0)));
00142
00143
00144 CVAPI(void) cvSetImagesForHaarClassifierCascade( CvHaarClassifierCascade* cascade,
00145 const CvArr* sum, const CvArr* sqsum,
00146 const CvArr* tilted_sum, double scale );
00147
00148
00149 CVAPI(int) cvRunHaarClassifierCascade( const CvHaarClassifierCascade* cascade,
00150 CvPoint pt, int start_stage CV_DEFAULT(0));
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161 typedef struct
00162 {
00163 unsigned int x;
00164 unsigned int y;
00165 unsigned int l;
00166 } CvLSVMFilterPosition;
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185 typedef struct{
00186 CvLSVMFilterPosition V;
00187 float fineFunction[4];
00188 unsigned int sizeX;
00189 unsigned int sizeY;
00190 unsigned int p;
00191 unsigned int xp;
00192 float *H;
00193 } CvLSVMFilterObject;
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203 typedef struct CvLatentSvmDetector
00204 {
00205 int num_filters;
00206 int num_components;
00207 int* num_part_filters;
00208 CvLSVMFilterObject** filters;
00209 float* b;
00210 float score_threshold;
00211 }
00212 CvLatentSvmDetector;
00213
00214
00215
00216
00217
00218 typedef struct CvObjectDetection
00219 {
00220 CvRect rect;
00221 float score;
00222 } CvObjectDetection;
00223
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 CVAPI(CvLatentSvmDetector*) cvLoadLatentSvmDetector(const char* filename);
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249 CVAPI(void) cvReleaseLatentSvmDetector(CvLatentSvmDetector** detector);
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271 CVAPI(CvSeq*) cvLatentSvmDetectObjects(IplImage* image,
00272 CvLatentSvmDetector* detector,
00273 CvMemStorage* storage,
00274 float overlap_threshold CV_DEFAULT(0.5f),
00275 int numThreads CV_DEFAULT(-1));
00276
00277 #ifdef __cplusplus
00278 }
00279
00280 CV_EXPORTS CvSeq* cvHaarDetectObjectsForROC( const CvArr* image,
00281 CvHaarClassifierCascade* cascade, CvMemStorage* storage,
00282 std::vector<int>& rejectLevels, std::vector<double>& levelWeightds,
00283 double scale_factor CV_DEFAULT(1.1),
00284 int min_neighbors CV_DEFAULT(3), int flags CV_DEFAULT(0),
00285 CvSize min_size CV_DEFAULT(cvSize(0,0)), CvSize max_size CV_DEFAULT(cvSize(0,0)),
00286 bool outputRejectLevels = false );
00287
00288 namespace cv
00289 {
00290
00292
00293 CV_EXPORTS_W void groupRectangles(vector<Rect>& rectList, int groupThreshold, double eps=0.2);
00294 CV_EXPORTS_W void groupRectangles(vector<Rect>& rectList, CV_OUT vector<int>& weights, int groupThreshold, double eps=0.2);
00295 CV_EXPORTS void groupRectangles(vector<Rect>& rectList, vector<int>& rejectLevels,
00296 vector<double>& levelWeights, int groupThreshold, double eps=0.2);
00297 CV_EXPORTS void groupRectangles_meanshift(vector<Rect>& rectList, vector<double>& foundWeights, vector<double>& foundScales,
00298 double detectThreshold = 0.0, Size winDetSize = Size(64, 128));
00299
00300
00301 class CV_EXPORTS FeatureEvaluator
00302 {
00303 public:
00304 enum { HAAR = 0, LBP = 1 };
00305 virtual ~FeatureEvaluator();
00306
00307 virtual bool read(const FileNode& node);
00308 virtual Ptr<FeatureEvaluator> clone() const;
00309 virtual int getFeatureType() const;
00310
00311 virtual bool setImage(const Mat&, Size origWinSize);
00312 virtual bool setWindow(Point p);
00313
00314 virtual double calcOrd(int featureIdx) const;
00315 virtual int calcCat(int featureIdx) const;
00316
00317 static Ptr<FeatureEvaluator> create(int type);
00318 };
00319
00320 template<> CV_EXPORTS void Ptr<CvHaarClassifierCascade>::delete_obj();
00321
00322 class CV_EXPORTS_W CascadeClassifier
00323 {
00324 public:
00325 CV_WRAP CascadeClassifier();
00326 CV_WRAP CascadeClassifier( const string& filename );
00327 virtual ~CascadeClassifier();
00328
00329 CV_WRAP virtual bool empty() const;
00330 CV_WRAP bool load( const string& filename );
00331 virtual bool read( const FileNode& node );
00332 CV_WRAP virtual void detectMultiScale( const Mat& image,
00333 CV_OUT vector<Rect>& objects,
00334 double scaleFactor=1.1,
00335 int minNeighbors=3, int flags=0,
00336 Size minSize=Size(),
00337 Size maxSize=Size() );
00338
00339 CV_WRAP virtual void detectMultiScale( const Mat& image,
00340 CV_OUT vector<Rect>& objects,
00341 vector<int>& rejectLevels,
00342 vector<double>& levelWeights,
00343 double scaleFactor=1.1,
00344 int minNeighbors=3, int flags=0,
00345 Size minSize=Size(),
00346 Size maxSize=Size(),
00347 bool outputRejectLevels=false );
00348
00349
00350 bool isOldFormatCascade() const;
00351 virtual Size getOriginalWindowSize() const;
00352 int getFeatureType() const;
00353 bool setImage( const Mat& );
00354
00355 protected:
00356
00357
00358
00359 virtual bool detectSingleScale( const Mat& image, int stripCount, Size processingRectSize,
00360 int stripSize, int yStep, double factor, vector<Rect>& candidates,
00361 vector<int>& rejectLevels, vector<double>& levelWeights, bool outputRejectLevels=false);
00362
00363 protected:
00364 enum { BOOST = 0 };
00365 enum { DO_CANNY_PRUNING = 1, SCALE_IMAGE = 2,
00366 FIND_BIGGEST_OBJECT = 4, DO_ROUGH_SEARCH = 8 };
00367
00368 friend struct CascadeClassifierInvoker;
00369
00370 template<class FEval>
00371 friend int predictOrdered( CascadeClassifier& cascade, Ptr<FeatureEvaluator> &featureEvaluator, double& weight);
00372
00373 template<class FEval>
00374 friend int predictCategorical( CascadeClassifier& cascade, Ptr<FeatureEvaluator> &featureEvaluator, double& weight);
00375
00376 template<class FEval>
00377 friend int predictOrderedStump( CascadeClassifier& cascade, Ptr<FeatureEvaluator> &featureEvaluator, double& weight);
00378
00379 template<class FEval>
00380 friend int predictCategoricalStump( CascadeClassifier& cascade, Ptr<FeatureEvaluator> &featureEvaluator, double& weight);
00381
00382 bool setImage( Ptr<FeatureEvaluator>&, const Mat& );
00383 virtual int runAt( Ptr<FeatureEvaluator>&, Point, double& weight );
00384
00385 class Data
00386 {
00387 public:
00388 struct CV_EXPORTS DTreeNode
00389 {
00390 int featureIdx;
00391 float threshold;
00392 int left;
00393 int right;
00394 };
00395
00396 struct CV_EXPORTS DTree
00397 {
00398 int nodeCount;
00399 };
00400
00401 struct CV_EXPORTS Stage
00402 {
00403 int first;
00404 int ntrees;
00405 float threshold;
00406 };
00407
00408 bool read(const FileNode &node);
00409
00410 bool isStumpBased;
00411
00412 int stageType;
00413 int featureType;
00414 int ncategories;
00415 Size origWinSize;
00416
00417 vector<Stage> stages;
00418 vector<DTree> classifiers;
00419 vector<DTreeNode> nodes;
00420 vector<float> leaves;
00421 vector<int> subsets;
00422 };
00423
00424 Data data;
00425 Ptr<FeatureEvaluator> featureEvaluator;
00426 Ptr<CvHaarClassifierCascade> oldCascade;
00427 };
00428
00429 void CV_EXPORTS_W groupRectangles( vector<Rect>& rectList, int groupThreshold, double eps, vector<int>* weights, vector<double>* levelWeights );
00430
00432
00433 struct CV_EXPORTS_W HOGDescriptor
00434 {
00435 public:
00436 enum { L2Hys=0 };
00437 enum { DEFAULT_NLEVELS=64 };
00438
00439 CV_WRAP HOGDescriptor() : winSize(64,128), blockSize(16,16), blockStride(8,8),
00440 cellSize(8,8), nbins(9), derivAperture(1), winSigma(-1),
00441 histogramNormType(HOGDescriptor::L2Hys), L2HysThreshold(0.2), gammaCorrection(true),
00442 nlevels(HOGDescriptor::DEFAULT_NLEVELS)
00443 {}
00444
00445 CV_WRAP HOGDescriptor(Size _winSize, Size _blockSize, Size _blockStride,
00446 Size _cellSize, int _nbins, int _derivAperture=1, double _winSigma=-1,
00447 int _histogramNormType=HOGDescriptor::L2Hys,
00448 double _L2HysThreshold=0.2, bool _gammaCorrection=false,
00449 int _nlevels=HOGDescriptor::DEFAULT_NLEVELS)
00450 : winSize(_winSize), blockSize(_blockSize), blockStride(_blockStride), cellSize(_cellSize),
00451 nbins(_nbins), derivAperture(_derivAperture), winSigma(_winSigma),
00452 histogramNormType(_histogramNormType), L2HysThreshold(_L2HysThreshold),
00453 gammaCorrection(_gammaCorrection), nlevels(_nlevels)
00454 {}
00455
00456 CV_WRAP HOGDescriptor(const String& filename)
00457 {
00458 load(filename);
00459 }
00460
00461 HOGDescriptor(const HOGDescriptor& d)
00462 {
00463 d.copyTo(*this);
00464 }
00465
00466 virtual ~HOGDescriptor() {}
00467
00468 CV_WRAP size_t getDescriptorSize() const;
00469 CV_WRAP bool checkDetectorSize() const;
00470 CV_WRAP double getWinSigma() const;
00471
00472 CV_WRAP virtual void setSVMDetector(const vector<float>& _svmdetector);
00473
00474 virtual bool read(FileNode& fn);
00475 virtual void write(FileStorage& fs, const String& objname) const;
00476
00477 CV_WRAP virtual bool load(const String& filename, const String& objname=String());
00478 CV_WRAP virtual void save(const String& filename, const String& objname=String()) const;
00479 virtual void copyTo(HOGDescriptor& c) const;
00480
00481 CV_WRAP virtual void compute(const Mat& img,
00482 CV_OUT vector<float>& descriptors,
00483 Size winStride=Size(), Size padding=Size(),
00484 const vector<Point>& locations=vector<Point>()) const;
00485
00486 CV_WRAP virtual void detect(const Mat& img, CV_OUT vector<Point>& foundLocations,
00487 vector<double>& weights,
00488 double hitThreshold=0, Size winStride=Size(),
00489 Size padding=Size(),
00490 const vector<Point>& searchLocations=vector<Point>()) const;
00491
00492 CV_WRAP virtual void detect(const Mat& img, CV_OUT vector<Point>& foundLocations,
00493 double hitThreshold=0, Size winStride=Size(),
00494 Size padding=Size(),
00495 const vector<Point>& searchLocations=vector<Point>()) const;
00496
00497 CV_WRAP virtual void detectMultiScale(const Mat& img, CV_OUT vector<Rect>& foundLocations,
00498 vector<double>& foundWeights, double hitThreshold=0,
00499 Size winStride=Size(), Size padding=Size(), double scale=1.05,
00500 double finalThreshold=2.0,bool useMeanshiftGrouping = false) const;
00501
00502 CV_WRAP virtual void detectMultiScale(const Mat& img, CV_OUT vector<Rect>& foundLocations,
00503 double hitThreshold=0, Size winStride=Size(),
00504 Size padding=Size(), double scale=1.05,
00505 double finalThreshold=2.0, bool useMeanshiftGrouping = false) const;
00506
00507 CV_WRAP virtual void computeGradient(const Mat& img, CV_OUT Mat& grad, CV_OUT Mat& angleOfs,
00508 Size paddingTL=Size(), Size paddingBR=Size()) const;
00509
00510 static vector<float> getDefaultPeopleDetector();
00511 static vector<float> getDaimlerPeopleDetector();
00512
00513 CV_PROP Size winSize;
00514 CV_PROP Size blockSize;
00515 CV_PROP Size blockStride;
00516 CV_PROP Size cellSize;
00517 CV_PROP int nbins;
00518 CV_PROP int derivAperture;
00519 CV_PROP double winSigma;
00520 CV_PROP int histogramNormType;
00521 CV_PROP double L2HysThreshold;
00522 CV_PROP bool gammaCorrection;
00523 CV_PROP vector<float> svmDetector;
00524 CV_PROP int nlevels;
00525 };
00526
00527
00528
00529
00530
00531 class CV_EXPORTS PlanarObjectDetector
00532 {
00533 public:
00534 PlanarObjectDetector();
00535 PlanarObjectDetector(const FileNode& node);
00536 PlanarObjectDetector(const vector<Mat>& pyr, int _npoints=300,
00537 int _patchSize=FernClassifier::PATCH_SIZE,
00538 int _nstructs=FernClassifier::DEFAULT_STRUCTS,
00539 int _structSize=FernClassifier::DEFAULT_STRUCT_SIZE,
00540 int _nviews=FernClassifier::DEFAULT_VIEWS,
00541 const LDetector& detector=LDetector(),
00542 const PatchGenerator& patchGenerator=PatchGenerator());
00543 virtual ~PlanarObjectDetector();
00544 virtual void train(const vector<Mat>& pyr, int _npoints=300,
00545 int _patchSize=FernClassifier::PATCH_SIZE,
00546 int _nstructs=FernClassifier::DEFAULT_STRUCTS,
00547 int _structSize=FernClassifier::DEFAULT_STRUCT_SIZE,
00548 int _nviews=FernClassifier::DEFAULT_VIEWS,
00549 const LDetector& detector=LDetector(),
00550 const PatchGenerator& patchGenerator=PatchGenerator());
00551 virtual void train(const vector<Mat>& pyr, const vector<KeyPoint>& keypoints,
00552 int _patchSize=FernClassifier::PATCH_SIZE,
00553 int _nstructs=FernClassifier::DEFAULT_STRUCTS,
00554 int _structSize=FernClassifier::DEFAULT_STRUCT_SIZE,
00555 int _nviews=FernClassifier::DEFAULT_VIEWS,
00556 const LDetector& detector=LDetector(),
00557 const PatchGenerator& patchGenerator=PatchGenerator());
00558 Rect getModelROI() const;
00559 vector<KeyPoint> getModelPoints() const;
00560 const LDetector& getDetector() const;
00561 const FernClassifier& getClassifier() const;
00562 void setVerbose(bool verbose);
00563
00564 void read(const FileNode& node);
00565 void write(FileStorage& fs, const String& name=String()) const;
00566 bool operator()(const Mat& image, CV_OUT Mat& H, CV_OUT vector<Point2f>& corners) const;
00567 bool operator()(const vector<Mat>& pyr, const vector<KeyPoint>& keypoints,
00568 CV_OUT Mat& H, CV_OUT vector<Point2f>& corners,
00569 CV_OUT vector<int>* pairs=0) const;
00570
00571 protected:
00572 bool verbose;
00573 Rect modelROI;
00574 vector<KeyPoint> modelPoints;
00575 LDetector ldetector;
00576 FernClassifier fernClassifier;
00577 };
00578
00579 struct CV_EXPORTS DataMatrixCode {
00580 char msg[4];
00581 Mat original;
00582 Point corners[4];
00583 };
00584
00585 CV_EXPORTS void findDataMatrix(const Mat& image, std::vector<DataMatrixCode>& codes);
00586 CV_EXPORTS void drawDataMatrixCodes(const std::vector<DataMatrixCode>& codes, Mat& drawImage);
00587 }
00588
00589
00590
00591
00592
00593 struct CV_EXPORTS CvDataMatrixCode {
00594 char msg[4];
00595 CvMat *original;
00596 CvMat *corners;
00597 };
00598
00599 #include <deque>
00600 CV_EXPORTS std::deque<CvDataMatrixCode> cvFindDataMatrix(CvMat *im);
00601 #endif
00602
00603 #endif