1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
45 * training of cascade of boosted classifiers based on haar features
48 #ifndef __CVHAARTRAINING_H_
49 #define __CVHAARTRAINING_H_
51 #include <_cvcommon.h>
52 #include <cvclassifier.h>
56 /* parameters for tree cascade classifier training */
58 /* max number of clusters */
59 #define CV_MAX_CLUSTERS 3
61 /* term criteria for K-Means */
62 #define CV_TERM_CRITERIA() cvTermCriteria( CV_TERMCRIT_EPS, 1000, 1E-5 )
64 /* If CV_COL_ARRANGEMENT is defined then sample feature values are stored in column.
65 Otherwise they are stored in row */
66 #define CV_COL_ARRANGEMENT
68 /* print statistic info */
71 #define CV_STAGE_CART_FILE_NAME "AdaBoostCARTHaarClassifier.txt"
73 #define CV_HAAR_FEATURE_MAX 3
74 #define CV_HAAR_FEATURE_DESC_MAX 20
77 typedef double sqsum_type;
78 typedef short idx_type;
80 #define CV_SUM_MAT_TYPE CV_32SC1
81 #define CV_SQSUM_MAT_TYPE CV_64FC1
82 #define CV_IDX_MAT_TYPE CV_16SC1
84 #define CV_STUMP_TRAIN_PORTION 100
86 #define CV_THRESHOLD_EPS (0.00001F)
88 typedef struct CvTHaarFeature
90 char desc[CV_HAAR_FEATURE_DESC_MAX];
96 } rect[CV_HAAR_FEATURE_MAX];
99 typedef struct CvFastHaarFeature
106 } rect[CV_HAAR_FEATURE_MAX];
109 typedef struct CvIntHaarFeatures
113 CvTHaarFeature* feature;
114 CvFastHaarFeature* fastfeature;
117 CV_INLINE CvTHaarFeature cvHaarFeature( const char* desc,
118 int x0, int y0, int w0, int h0, float wt0,
119 int x1, int y1, int w1, int h1, float wt1,
120 int x2 CV_DEFAULT( 0 ), int y2 CV_DEFAULT( 0 ),
121 int w2 CV_DEFAULT( 0 ), int h2 CV_DEFAULT( 0 ),
122 float wt2 CV_DEFAULT( 0.0F ) );
124 CV_INLINE CvTHaarFeature cvHaarFeature( const char* desc,
125 int x0, int y0, int w0, int h0, float wt0,
126 int x1, int y1, int w1, int h1, float wt1,
127 int x2, int y2, int w2, int h2, float wt2 )
131 assert( CV_HAAR_FEATURE_MAX >= 3 );
132 assert( strlen( desc ) < CV_HAAR_FEATURE_DESC_MAX );
134 strcpy( &(hf.desc[0]), desc );
135 hf.tilted = ( hf.desc[0] == 't' );
139 hf.rect[0].r.width = w0;
140 hf.rect[0].r.height = h0;
141 hf.rect[0].weight = wt0;
145 hf.rect[1].r.width = w1;
146 hf.rect[1].r.height = h1;
147 hf.rect[1].weight = wt1;
151 hf.rect[2].r.width = w2;
152 hf.rect[2].r.height = h2;
153 hf.rect[2].weight = wt2;
158 /* Prepared for training samples */
159 typedef struct CvHaarTrainingData
161 CvSize winsize; /* training image size */
162 int maxnum; /* maximum number of samples */
163 CvMat sum; /* sum images (each row represents image) */
164 CvMat tilted; /* tilted sum images (each row represents image) */
165 CvMat normfactor; /* normalization factor */
166 CvMat cls; /* classes. 1.0 - object, 0.0 - background */
167 CvMat weights; /* weights */
169 CvMat* valcache; /* precalculated feature values (CV_32FC1) */
170 CvMat* idxcache; /* presorted indices (CV_IDX_MAT_TYPE) */
174 /* Passed to callback functions */
175 typedef struct CvUserdata
177 CvHaarTrainingData* trainingData;
178 CvIntHaarFeatures* haarFeatures;
182 CvUserdata cvUserdata( CvHaarTrainingData* trainingData,
183 CvIntHaarFeatures* haarFeatures );
186 CvUserdata cvUserdata( CvHaarTrainingData* trainingData,
187 CvIntHaarFeatures* haarFeatures )
191 userdata.trainingData = trainingData;
192 userdata.haarFeatures = haarFeatures;
198 #define CV_INT_HAAR_CLASSIFIER_FIELDS() \
199 float (*eval)( CvIntHaarClassifier*, sum_type*, sum_type*, float ); \
200 void (*save)( CvIntHaarClassifier*, FILE* file ); \
201 void (*release)( CvIntHaarClassifier** );
203 /* internal weak classifier*/
204 typedef struct CvIntHaarClassifier
206 CV_INT_HAAR_CLASSIFIER_FIELDS()
207 } CvIntHaarClassifier;
212 typedef struct CvCARTHaarClassifier
214 CV_INT_HAAR_CLASSIFIER_FIELDS()
218 CvTHaarFeature* feature;
219 CvFastHaarFeature* fastfeature;
224 } CvCARTHaarClassifier;
226 /* internal stage classifier */
227 typedef struct CvStageHaarClassifier
229 CV_INT_HAAR_CLASSIFIER_FIELDS()
233 CvIntHaarClassifier** classifier;
234 } CvStageHaarClassifier;
236 /* internal cascade classifier */
237 typedef struct CvCascadeHaarClassifier
239 CV_INT_HAAR_CLASSIFIER_FIELDS()
242 CvIntHaarClassifier** classifier;
243 } CvCascadeHaarClassifier;
246 /* internal tree cascade classifier node */
247 typedef struct CvTreeCascadeNode
249 CvStageHaarClassifier* stage;
251 struct CvTreeCascadeNode* next;
252 struct CvTreeCascadeNode* child;
253 struct CvTreeCascadeNode* parent;
255 struct CvTreeCascadeNode* next_same_level;
256 struct CvTreeCascadeNode* child_eval;
261 /* internal tree cascade classifier */
262 typedef struct CvTreeCascadeClassifier
264 CV_INT_HAAR_CLASSIFIER_FIELDS()
266 CvTreeCascadeNode* root; /* root of the tree */
267 CvTreeCascadeNode* root_eval; /* root node for the filtering */
270 } CvTreeCascadeClassifier;
273 CV_INLINE float cvEvalFastHaarFeature( CvFastHaarFeature* feature,
274 sum_type* sum, sum_type* tilted )
276 sum_type* img = NULL;
282 img = ( feature->tilted ) ? tilted : sum;
286 for( i = 0; feature->rect[i].weight != 0.0F && i < CV_HAAR_FEATURE_MAX; i++ )
288 ret += feature->rect[i].weight *
289 ( img[feature->rect[i].p0] - img[feature->rect[i].p1] -
290 img[feature->rect[i].p2] + img[feature->rect[i].p3] );
296 typedef struct CvSampleDistortionData
307 } CvSampleDistortionData;
310 * icvConvertToFastHaarFeature
312 * Convert to fast representation of haar features
314 * haarFeature - input array
315 * fastHaarFeature - output array
316 * size - size of arrays
317 * step - row step for the integral image
319 void icvConvertToFastHaarFeature( CvTHaarFeature* haarFeature,
320 CvFastHaarFeature* fastHaarFeature,
321 int size, int step );
324 void icvWriteVecHeader( FILE* file, int count, int width, int height );
325 void icvWriteVecSample( FILE* file, CvArr* sample );
326 void icvPlaceDistortedSample( CvArr* background,
327 int inverse, int maxintensitydev,
328 double maxxangle, double maxyangle, double maxzangle,
329 int inscribe, double maxshiftf, double maxscalef,
330 CvSampleDistortionData* data );
331 void icvEndSampleDistortion( CvSampleDistortionData* data );
333 int icvStartSampleDistortion( const char* imgfilename, int bgcolor, int bgthreshold,
334 CvSampleDistortionData* data );
336 typedef int (*CvGetHaarTrainingDataCallback)( CvMat* img, void* userdata );
338 typedef struct CvVecFile
347 int icvGetHaarTraininDataFromVecCallback( CvMat* img, void* userdata );
350 * icvGetHaarTrainingDataFromVec
352 * Fill <data> with samples from .vec file, passed <cascade>
353 int icvGetHaarTrainingDataFromVec( CvHaarTrainingData* data, int first, int count,
354 CvIntHaarClassifier* cascade,
355 const char* filename,
359 CvIntHaarClassifier* icvCreateCARTHaarClassifier( int count );
361 void icvReleaseHaarClassifier( CvIntHaarClassifier** classifier );
363 void icvInitCARTHaarClassifier( CvCARTHaarClassifier* carthaar, CvCARTClassifier* cart,
364 CvIntHaarFeatures* intHaarFeatures );
366 float icvEvalCARTHaarClassifier( CvIntHaarClassifier* classifier,
367 sum_type* sum, sum_type* tilted, float normfactor );
369 CvIntHaarClassifier* icvCreateStageHaarClassifier( int count, float threshold );
371 void icvReleaseStageHaarClassifier( CvIntHaarClassifier** classifier );
373 float icvEvalStageHaarClassifier( CvIntHaarClassifier* classifier,
374 sum_type* sum, sum_type* tilted, float normfactor );
376 CvIntHaarClassifier* icvCreateCascadeHaarClassifier( int count );
378 void icvReleaseCascadeHaarClassifier( CvIntHaarClassifier** classifier );
380 float icvEvalCascadeHaarClassifier( CvIntHaarClassifier* classifier,
381 sum_type* sum, sum_type* tilted, float normfactor );
383 void icvSaveHaarFeature( CvTHaarFeature* feature, FILE* file );
385 void icvLoadHaarFeature( CvTHaarFeature* feature, FILE* file );
387 void icvSaveCARTHaarClassifier( CvIntHaarClassifier* classifier, FILE* file );
389 CvIntHaarClassifier* icvLoadCARTHaarClassifier( FILE* file, int step );
391 void icvSaveStageHaarClassifier( CvIntHaarClassifier* classifier, FILE* file );
393 CvIntHaarClassifier* icvLoadCARTStageHaarClassifier( const char* filename, int step );
396 /* tree cascade classifier */
398 float icvEvalTreeCascadeClassifier( CvIntHaarClassifier* classifier,
399 sum_type* sum, sum_type* tilted, float normfactor );
401 void icvSetLeafNode( CvTreeCascadeClassifier* tree, CvTreeCascadeNode* leaf );
403 float icvEvalTreeCascadeClassifierFilter( CvIntHaarClassifier* classifier, sum_type* sum,
404 sum_type* tilted, float normfactor );
406 CvTreeCascadeNode* icvCreateTreeCascadeNode();
408 void icvReleaseTreeCascadeNodes( CvTreeCascadeNode** node );
410 void icvReleaseTreeCascadeClassifier( CvIntHaarClassifier** classifier );
412 /* Prints out current tree structure to <stdout> */
413 void icvPrintTreeCascade( CvTreeCascadeNode* root );
415 /* Loads tree cascade classifier */
416 CvIntHaarClassifier* icvLoadTreeCascadeClassifier( const char* filename, int step,
419 /* Finds leaves belonging to maximal level and connects them via leaf->next_same_level */
420 CvTreeCascadeNode* icvFindDeepestLeaves( CvTreeCascadeClassifier* tree );
422 #endif /* __CVHAARTRAINING_H_ */