opencv  2.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
objdetect.hpp
Go to the documentation of this file.
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
8 //
9 //
10 // License Agreement
11 // For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
22 //
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
26 //
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42 
43 #ifndef __OPENCV_OBJDETECT_HPP__
44 #define __OPENCV_OBJDETECT_HPP__
45 
46 #include "opencv2/core/core.hpp"
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 /****************************************************************************************\
53 * Haar-like Object Detection functions *
54 \****************************************************************************************/
55 
56 #define CV_HAAR_MAGIC_VAL 0x42500000
57 #define CV_TYPE_NAME_HAAR "opencv-haar-classifier"
58 
59 #define CV_IS_HAAR_CLASSIFIER( haar ) \
60  ((haar) != NULL && \
61  (((const CvHaarClassifierCascade*)(haar))->flags & CV_MAGIC_MASK)==CV_HAAR_MAGIC_VAL)
62 
63 #define CV_HAAR_FEATURE_MAX 3
64 
65 typedef struct CvHaarFeature
66 {
67  int tilted;
68  struct
69  {
71  float weight;
74 
75 typedef struct CvHaarClassifier
76 {
77  int count;
79  float* threshold;
80  int* left;
81  int* right;
82  float* alpha;
84 
85 typedef struct CvHaarStageClassifier
86 {
87  int count;
88  float threshold;
90 
91  int next;
92  int child;
93  int parent;
95 
97 
99 {
100  int flags;
101  int count;
104  double scale;
108 
109 typedef struct CvAvgComp
110 {
113 } CvAvgComp;
114 
115 /* Loads haar classifier cascade from a directory.
116  It is obsolete: convert your cascade to xml and use cvLoad instead */
118  const char* directory, CvSize orig_window_size);
119 
121 
122 #define CV_HAAR_DO_CANNY_PRUNING 1
123 #define CV_HAAR_SCALE_IMAGE 2
124 #define CV_HAAR_FIND_BIGGEST_OBJECT 4
125 #define CV_HAAR_DO_ROUGH_SEARCH 8
126 
127 CVAPI(CvSeq*) cvHaarDetectObjects( const CvArr* image,
128  CvHaarClassifierCascade* cascade,
129  CvMemStorage* storage, double scale_factor CV_DEFAULT(1.1),
130  int min_neighbors CV_DEFAULT(3), int flags CV_DEFAULT(0),
131  CvSize min_size CV_DEFAULT(cvSize(0,0)), CvSize max_size CV_DEFAULT(cvSize(0,0)));
132 
133 /* sets images for haar classifier cascade */
135  const CvArr* sum, const CvArr* sqsum,
136  const CvArr* tilted_sum, double scale );
137 
138 /* runs the cascade on the specified window */
139 CVAPI(int) cvRunHaarClassifierCascade( const CvHaarClassifierCascade* cascade,
140  CvPoint pt, int start_stage CV_DEFAULT(0));
141 
142 
143 /****************************************************************************************\
144 * Latent SVM Object Detection functions *
145 \****************************************************************************************/
146 
147 // DataType: STRUCT position
148 // Structure describes the position of the filter in the feature pyramid
149 // l - level in the feature pyramid
150 // (x, y) - coordinate in level l
151 typedef struct
152 {
153  unsigned int x;
154  unsigned int y;
155  unsigned int l;
157 
158 // DataType: STRUCT filterObject
159 // Description of the filter, which corresponds to the part of the object
160 // V - ideal (penalty = 0) position of the partial filter
161 // from the root filter position (V_i in the paper)
162 // penaltyFunction - vector describes penalty function (d_i in the paper)
163 // pf[0] * x + pf[1] * y + pf[2] * x^2 + pf[3] * y^2
164 // FILTER DESCRIPTION
165 // Rectangular map (sizeX x sizeY),
166 // every cell stores feature vector (dimension = p)
167 // H - matrix of feature vectors
168 // to set and get feature vectors (i,j)
169 // used formula H[(j * sizeX + i) * p + k], where
170 // k - component of feature vector in cell (i, j)
171 // END OF FILTER DESCRIPTION
172 // xp - auxillary parameter for internal use
173 // size of row in feature vectors
174 // (yp = (int) (p / xp); p = xp * yp)
175 typedef struct{
177  float fineFunction[4];
178  unsigned int sizeX;
179  unsigned int sizeY;
180  unsigned int p;
181  unsigned int xp;
182  float *H;
184 
185 // data type: STRUCT CvLatentSvmDetector
186 // structure contains internal representation of trained Latent SVM detector
187 // num_filters - total number of filters (root plus part) in model
188 // num_components - number of components in model
189 // num_part_filters - array containing number of part filters for each component
190 // filters - root and part filters for all model components
191 // b - biases for all model components
192 // score_threshold - confidence level threshold
193 typedef struct CvLatentSvmDetector
194 {
199  float* b;
201 }
203 
204 // data type: STRUCT CvObjectDetection
205 // structure contains the bounding box and confidence level for detected object
206 // rect - bounding box for a detected object
207 // score - confidence level
208 typedef struct CvObjectDetection
209 {
211  float score;
213 
215 
216 
217 /*
218 // load trained detector from a file
219 //
220 // API
221 // CvLatentSvmDetector* cvLoadLatentSvmDetector(const char* filename);
222 // INPUT
223 // filename - path to the file containing the parameters of
224  - trained Latent SVM detector
225 // OUTPUT
226 // trained Latent SVM detector in internal representation
227 */
228 CVAPI(CvLatentSvmDetector*) cvLoadLatentSvmDetector(const char* filename);
229 
230 /*
231 // release memory allocated for CvLatentSvmDetector structure
232 //
233 // API
234 // void cvReleaseLatentSvmDetector(CvLatentSvmDetector** detector);
235 // INPUT
236 // detector - CvLatentSvmDetector structure to be released
237 // OUTPUT
238 */
239 CVAPI(void) cvReleaseLatentSvmDetector(CvLatentSvmDetector** detector);
240 
241 /*
242 // find rectangular regions in the given image that are likely
243 // to contain objects and corresponding confidence levels
244 //
245 // API
246 // CvSeq* cvLatentSvmDetectObjects(const IplImage* image,
247 // CvLatentSvmDetector* detector,
248 // CvMemStorage* storage,
249 // float overlap_threshold = 0.5f);
250 // INPUT
251 // image - image to detect objects in
252 // detector - Latent SVM detector in internal representation
253 // storage - memory storage to store the resultant sequence
254 // of the object candidate rectangles
255 // overlap_threshold - threshold for the non-maximum suppression algorithm
256  = 0.5f [here will be the reference to original paper]
257 // OUTPUT
258 // sequence of detected objects (bounding boxes and confidence levels stored in CvObjectDetection structures)
259 */
260 CVAPI(CvSeq*) cvLatentSvmDetectObjects(IplImage* image,
261  CvLatentSvmDetector* detector,
262  CvMemStorage* storage,
263  float overlap_threshold CV_DEFAULT(0.5f));
264 
265 #ifdef __cplusplus
266 }
267 
268 namespace cv
269 {
270 
272 
273 CV_EXPORTS_W void groupRectangles(vector<Rect>& rectList, int groupThreshold, double eps=0.2);
274 CV_EXPORTS_W void groupRectangles(vector<Rect>& rectList, CV_OUT vector<int>& weights, int groupThreshold, double eps=0.2);
275 
277 {
278 public:
279  enum { HAAR = 0, LBP = 1 };
280  virtual ~FeatureEvaluator();
281  virtual bool read(const FileNode& node);
282  virtual Ptr<FeatureEvaluator> clone() const;
283  virtual int getFeatureType() const;
284 
285  virtual bool setImage(const Mat&, Size origWinSize);
286  virtual bool setWindow(Point p);
287 
288  virtual double calcOrd(int featureIdx) const;
289  virtual int calcCat(int featureIdx) const;
290 
291  static Ptr<FeatureEvaluator> create(int type);
292 };
293 
295 
297 {
298 public:
300  {
302  float threshold; // for ordered features only
303  int left;
304  int right;
305  };
306 
308  {
310  };
311 
313  {
314  int first;
315  int ntrees;
316  float threshold;
317  };
318 
319  enum { BOOST = 0 };
320  enum { DO_CANNY_PRUNING = 1, SCALE_IMAGE = 2,
321  FIND_BIGGEST_OBJECT = 4, DO_ROUGH_SEARCH = 8 };
322 
324  CV_WRAP CascadeClassifier(const string& filename);
326 
327  CV_WRAP bool empty() const;
328  CV_WRAP bool load(const string& filename);
329  bool read(const FileNode& node);
330  CV_WRAP void detectMultiScale( const Mat& image,
331  CV_OUT vector<Rect>& objects,
332  double scaleFactor=1.1,
333  int minNeighbors=3, int flags=0,
334  Size minSize=Size(),
335  Size maxSize=Size());
336 
337  bool setImage( Ptr<FeatureEvaluator>&, const Mat& );
338  int runAt( Ptr<FeatureEvaluator>&, Point );
339 
341 
346 
347  vector<Stage> stages;
348  vector<DTree> classifiers;
349  vector<DTreeNode> nodes;
350  vector<float> leaves;
351  vector<int> subsets;
352 
355 };
356 
357 
359 
361 {
362 public:
363  enum { L2Hys=0 };
364  enum { DEFAULT_NLEVELS=64 };
365 
366  CV_WRAP HOGDescriptor() : winSize(64,128), blockSize(16,16), blockStride(8,8),
367  cellSize(8,8), nbins(9), derivAperture(1), winSigma(-1),
368  histogramNormType(HOGDescriptor::L2Hys), L2HysThreshold(0.2), gammaCorrection(true),
369  nlevels(HOGDescriptor::DEFAULT_NLEVELS)
370  {}
371 
372  CV_WRAP HOGDescriptor(Size _winSize, Size _blockSize, Size _blockStride,
373  Size _cellSize, int _nbins, int _derivAperture=1, double _winSigma=-1,
374  int _histogramNormType=HOGDescriptor::L2Hys,
375  double _L2HysThreshold=0.2, bool _gammaCorrection=false,
376  int _nlevels=HOGDescriptor::DEFAULT_NLEVELS)
377  : winSize(_winSize), blockSize(_blockSize), blockStride(_blockStride), cellSize(_cellSize),
378  nbins(_nbins), derivAperture(_derivAperture), winSigma(_winSigma),
379  histogramNormType(_histogramNormType), L2HysThreshold(_L2HysThreshold),
380  gammaCorrection(_gammaCorrection), nlevels(_nlevels)
381  {}
382 
383  CV_WRAP HOGDescriptor(const String& filename)
384  {
385  load(filename);
386  }
387 
389  {
390  d.copyTo(*this);
391  }
392 
393  virtual ~HOGDescriptor() {}
394 
395  CV_WRAP size_t getDescriptorSize() const;
396  CV_WRAP bool checkDetectorSize() const;
397  CV_WRAP double getWinSigma() const;
398 
399  CV_WRAP virtual void setSVMDetector(const vector<float>& _svmdetector);
400 
401  virtual bool read(FileNode& fn);
402  virtual void write(FileStorage& fs, const String& objname) const;
403 
404  CV_WRAP virtual bool load(const String& filename, const String& objname=String());
405  CV_WRAP virtual void save(const String& filename, const String& objname=String()) const;
406  virtual void copyTo(HOGDescriptor& c) const;
407 
408  CV_WRAP virtual void compute(const Mat& img,
409  CV_OUT vector<float>& descriptors,
410  Size winStride=Size(), Size padding=Size(),
411  const vector<Point>& locations=vector<Point>()) const;
412  CV_WRAP virtual void detect(const Mat& img, CV_OUT vector<Point>& foundLocations,
413  double hitThreshold=0, Size winStride=Size(),
414  Size padding=Size(),
415  const vector<Point>& searchLocations=vector<Point>()) const;
416  CV_WRAP virtual void detectMultiScale(const Mat& img, CV_OUT vector<Rect>& foundLocations,
417  double hitThreshold=0, Size winStride=Size(),
418  Size padding=Size(), double scale=1.05,
419  int groupThreshold=2) const;
420  CV_WRAP virtual void computeGradient(const Mat& img, CV_OUT Mat& grad, CV_OUT Mat& angleOfs,
421  Size paddingTL=Size(), Size paddingBR=Size()) const;
422 
423  static vector<float> getDefaultPeopleDetector();
424 
435  CV_PROP vector<float> svmDetector;
437 };
438 
439 
440 }
441 
442 #endif
443 
444 #endif