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
00048 #ifdef __cplusplus
00049 extern "C" {
00050 #endif
00051
00052
00053
00054
00055
00056 #define CV_HAAR_MAGIC_VAL 0x42500000
00057 #define CV_TYPE_NAME_HAAR "opencv-haar-classifier"
00058
00059 #define CV_IS_HAAR_CLASSIFIER( haar ) \
00060 ((haar) != NULL && \
00061 (((const CvHaarClassifierCascade*)(haar))->flags & CV_MAGIC_MASK)==CV_HAAR_MAGIC_VAL)
00062
00063 #define CV_HAAR_FEATURE_MAX 3
00064
00065 typedef struct CvHaarFeature
00066 {
00067 int tilted;
00068 struct
00069 {
00070 CvRect r;
00071 float weight;
00072 } rect[CV_HAAR_FEATURE_MAX];
00073 } CvHaarFeature;
00074
00075 typedef struct CvHaarClassifier
00076 {
00077 int count;
00078 CvHaarFeature* haar_feature;
00079 float* threshold;
00080 int* left;
00081 int* right;
00082 float* alpha;
00083 } CvHaarClassifier;
00084
00085 typedef struct CvHaarStageClassifier
00086 {
00087 int count;
00088 float threshold;
00089 CvHaarClassifier* classifier;
00090
00091 int next;
00092 int child;
00093 int parent;
00094 } CvHaarStageClassifier;
00095
00096 typedef struct CvHidHaarClassifierCascade CvHidHaarClassifierCascade;
00097
00098 typedef struct CvHaarClassifierCascade
00099 {
00100 int flags;
00101 int count;
00102 CvSize orig_window_size;
00103 CvSize real_window_size;
00104 double scale;
00105 CvHaarStageClassifier* stage_classifier;
00106 CvHidHaarClassifierCascade* hid_cascade;
00107 } CvHaarClassifierCascade;
00108
00109 typedef struct CvAvgComp
00110 {
00111 CvRect rect;
00112 int neighbors;
00113 } CvAvgComp;
00114
00115
00116
00117 CVAPI(CvHaarClassifierCascade*) cvLoadHaarClassifierCascade(
00118 const char* directory, CvSize orig_window_size);
00119
00120 CVAPI(void) cvReleaseHaarClassifierCascade( CvHaarClassifierCascade** cascade );
00121
00122 #define CV_HAAR_DO_CANNY_PRUNING 1
00123 #define CV_HAAR_SCALE_IMAGE 2
00124 #define CV_HAAR_FIND_BIGGEST_OBJECT 4
00125 #define CV_HAAR_DO_ROUGH_SEARCH 8
00126
00127 CVAPI(CvSeq*) cvHaarDetectObjects( const CvArr* image,
00128 CvHaarClassifierCascade* cascade,
00129 CvMemStorage* storage, double scale_factor CV_DEFAULT(1.1),
00130 int min_neighbors CV_DEFAULT(3), int flags CV_DEFAULT(0),
00131 CvSize min_size CV_DEFAULT(cvSize(0,0)), CvSize max_size CV_DEFAULT(cvSize(0,0)));
00132
00133
00134 CVAPI(void) cvSetImagesForHaarClassifierCascade( CvHaarClassifierCascade* cascade,
00135 const CvArr* sum, const CvArr* sqsum,
00136 const CvArr* tilted_sum, double scale );
00137
00138
00139 CVAPI(int) cvRunHaarClassifierCascade( const CvHaarClassifierCascade* cascade,
00140 CvPoint pt, int start_stage CV_DEFAULT(0));
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 typedef struct
00152 {
00153 unsigned int x;
00154 unsigned int y;
00155 unsigned int l;
00156 } CvLSVMFilterPosition;
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175 typedef struct{
00176 CvLSVMFilterPosition V;
00177 float fineFunction[4];
00178 unsigned int sizeX;
00179 unsigned int sizeY;
00180 unsigned int p;
00181 unsigned int xp;
00182 float *H;
00183 } CvLSVMFilterObject;
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193 typedef struct CvLatentSvmDetector
00194 {
00195 int num_filters;
00196 int num_components;
00197 int* num_part_filters;
00198 CvLSVMFilterObject** filters;
00199 float* b;
00200 float score_threshold;
00201 }
00202 CvLatentSvmDetector;
00203
00204
00205
00206
00207
00208 typedef struct CvObjectDetection
00209 {
00210 CvRect rect;
00211 float score;
00212 } CvObjectDetection;
00213
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228 CVAPI(CvLatentSvmDetector*) cvLoadLatentSvmDetector(const char* filename);
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239 CVAPI(void) cvReleaseLatentSvmDetector(CvLatentSvmDetector** detector);
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260 CVAPI(CvSeq*) cvLatentSvmDetectObjects(IplImage* image,
00261 CvLatentSvmDetector* detector,
00262 CvMemStorage* storage,
00263 float overlap_threshold CV_DEFAULT(0.5f));
00264
00265 #ifdef __cplusplus
00266 }
00267
00268 namespace cv
00269 {
00270
00272
00273 CV_EXPORTS_W void groupRectangles(vector<Rect>& rectList, int groupThreshold, double eps=0.2);
00274 CV_EXPORTS_W void groupRectangles(vector<Rect>& rectList, CV_OUT vector<int>& weights, int groupThreshold, double eps=0.2);
00275
00276 class CV_EXPORTS FeatureEvaluator
00277 {
00278 public:
00279 enum { HAAR = 0, LBP = 1 };
00280 virtual ~FeatureEvaluator();
00281
00282 virtual bool read(const FileNode& node);
00283 virtual Ptr<FeatureEvaluator> clone() const;
00284 virtual int getFeatureType() const;
00285
00286 virtual bool setImage(const Mat&, Size origWinSize);
00287 virtual bool setWindow(Point p);
00288
00289 virtual double calcOrd(int featureIdx) const;
00290 virtual int calcCat(int featureIdx) const;
00291
00292 static Ptr<FeatureEvaluator> create(int type);
00293 };
00294
00295 template<> CV_EXPORTS void Ptr<CvHaarClassifierCascade>::delete_obj();
00296
00297 class CV_EXPORTS_W CascadeClassifier
00298 {
00299 public:
00300 CV_WRAP CascadeClassifier();
00301 CV_WRAP CascadeClassifier( const string& filename );
00302 virtual ~CascadeClassifier();
00303
00304 CV_WRAP virtual bool empty() const;
00305 CV_WRAP bool load( const string& filename );
00306 bool read( const FileNode& node );
00307 CV_WRAP void detectMultiScale( const Mat& image,
00308 CV_OUT vector<Rect>& objects,
00309 double scaleFactor=1.1,
00310 int minNeighbors=3, int flags=0,
00311 Size minSize=Size(),
00312 Size maxSize=Size() );
00313
00314 bool isOldFormatCascade() const;
00315 virtual Size getOriginalWindowSize() const;
00316 int getFeatureType() const;
00317 bool setImage( const Mat& );
00318
00319 protected:
00320 virtual bool detectSingleScale( const Mat& image, int stripCount, Size processingRectSize,
00321 int stripSize, int yStep, double factor, vector<Rect>& candidates );
00322
00323 private:
00324 enum { BOOST = 0 };
00325 enum { DO_CANNY_PRUNING = 1, SCALE_IMAGE = 2,
00326 FIND_BIGGEST_OBJECT = 4, DO_ROUGH_SEARCH = 8 };
00327
00328 friend struct CascadeClassifierInvoker;
00329
00330 template<class FEval>
00331 friend int predictOrdered( CascadeClassifier& cascade, Ptr<FeatureEvaluator> &featureEvaluator);
00332
00333 template<class FEval>
00334 friend int predictCategorical( CascadeClassifier& cascade, Ptr<FeatureEvaluator> &featureEvaluator);
00335
00336 template<class FEval>
00337 friend int predictOrderedStump( CascadeClassifier& cascade, Ptr<FeatureEvaluator> &featureEvaluator);
00338
00339 template<class FEval>
00340 friend int predictCategoricalStump( CascadeClassifier& cascade, Ptr<FeatureEvaluator> &featureEvaluator);
00341
00342 bool setImage( Ptr<FeatureEvaluator>&, const Mat& );
00343 int runAt( Ptr<FeatureEvaluator>&, Point );
00344
00345 class Data
00346 {
00347 public:
00348 struct CV_EXPORTS DTreeNode
00349 {
00350 int featureIdx;
00351 float threshold;
00352 int left;
00353 int right;
00354 };
00355
00356 struct CV_EXPORTS DTree
00357 {
00358 int nodeCount;
00359 };
00360
00361 struct CV_EXPORTS Stage
00362 {
00363 int first;
00364 int ntrees;
00365 float threshold;
00366 };
00367
00368 bool read(const FileNode &node);
00369
00370 bool isStumpBased;
00371
00372 int stageType;
00373 int featureType;
00374 int ncategories;
00375 Size origWinSize;
00376
00377 vector<Stage> stages;
00378 vector<DTree> classifiers;
00379 vector<DTreeNode> nodes;
00380 vector<float> leaves;
00381 vector<int> subsets;
00382 };
00383
00384 Data data;
00385 Ptr<FeatureEvaluator> featureEvaluator;
00386 Ptr<CvHaarClassifierCascade> oldCascade;
00387 };
00388
00390
00391 struct CV_EXPORTS_W HOGDescriptor
00392 {
00393 public:
00394 enum { L2Hys=0 };
00395 enum { DEFAULT_NLEVELS=64 };
00396
00397 CV_WRAP HOGDescriptor() : winSize(64,128), blockSize(16,16), blockStride(8,8),
00398 cellSize(8,8), nbins(9), derivAperture(1), winSigma(-1),
00399 histogramNormType(HOGDescriptor::L2Hys), L2HysThreshold(0.2), gammaCorrection(true),
00400 nlevels(HOGDescriptor::DEFAULT_NLEVELS)
00401 {}
00402
00403 CV_WRAP HOGDescriptor(Size _winSize, Size _blockSize, Size _blockStride,
00404 Size _cellSize, int _nbins, int _derivAperture=1, double _winSigma=-1,
00405 int _histogramNormType=HOGDescriptor::L2Hys,
00406 double _L2HysThreshold=0.2, bool _gammaCorrection=false,
00407 int _nlevels=HOGDescriptor::DEFAULT_NLEVELS)
00408 : winSize(_winSize), blockSize(_blockSize), blockStride(_blockStride), cellSize(_cellSize),
00409 nbins(_nbins), derivAperture(_derivAperture), winSigma(_winSigma),
00410 histogramNormType(_histogramNormType), L2HysThreshold(_L2HysThreshold),
00411 gammaCorrection(_gammaCorrection), nlevels(_nlevels)
00412 {}
00413
00414 CV_WRAP HOGDescriptor(const String& filename)
00415 {
00416 load(filename);
00417 }
00418
00419 HOGDescriptor(const HOGDescriptor& d)
00420 {
00421 d.copyTo(*this);
00422 }
00423
00424 virtual ~HOGDescriptor() {}
00425
00426 CV_WRAP size_t getDescriptorSize() const;
00427 CV_WRAP bool checkDetectorSize() const;
00428 CV_WRAP double getWinSigma() const;
00429
00430 CV_WRAP virtual void setSVMDetector(const vector<float>& _svmdetector);
00431
00432 virtual bool read(FileNode& fn);
00433 virtual void write(FileStorage& fs, const String& objname) const;
00434
00435 CV_WRAP virtual bool load(const String& filename, const String& objname=String());
00436 CV_WRAP virtual void save(const String& filename, const String& objname=String()) const;
00437 virtual void copyTo(HOGDescriptor& c) const;
00438
00439 CV_WRAP virtual void compute(const Mat& img,
00440 CV_OUT vector<float>& descriptors,
00441 Size winStride=Size(), Size padding=Size(),
00442 const vector<Point>& locations=vector<Point>()) const;
00443 CV_WRAP virtual void detect(const Mat& img, CV_OUT vector<Point>& foundLocations,
00444 double hitThreshold=0, Size winStride=Size(),
00445 Size padding=Size(),
00446 const vector<Point>& searchLocations=vector<Point>()) const;
00447 CV_WRAP virtual void detectMultiScale(const Mat& img, CV_OUT vector<Rect>& foundLocations,
00448 double hitThreshold=0, Size winStride=Size(),
00449 Size padding=Size(), double scale=1.05,
00450 int groupThreshold=2) const;
00451 CV_WRAP virtual void computeGradient(const Mat& img, CV_OUT Mat& grad, CV_OUT Mat& angleOfs,
00452 Size paddingTL=Size(), Size paddingBR=Size()) const;
00453
00454 static vector<float> getDefaultPeopleDetector();
00455
00456 CV_PROP Size winSize;
00457 CV_PROP Size blockSize;
00458 CV_PROP Size blockStride;
00459 CV_PROP Size cellSize;
00460 CV_PROP int nbins;
00461 CV_PROP int derivAperture;
00462 CV_PROP double winSigma;
00463 CV_PROP int histogramNormType;
00464 CV_PROP double L2HysThreshold;
00465 CV_PROP bool gammaCorrection;
00466 CV_PROP vector<float> svmDetector;
00467 CV_PROP int nlevels;
00468 };
00469
00470
00471 }
00472
00473 #endif
00474
00475 #endif