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 /* print statistic info */
67 #define CV_STAGE_CART_FILE_NAME "AdaBoostCARTHaarClassifier.txt"
69 #define CV_HAAR_FEATURE_MAX 3
70 #define CV_HAAR_FEATURE_DESC_MAX 20
73 typedef double sqsum_type;
74 typedef short idx_type;
76 #define CV_SUM_MAT_TYPE CV_32SC1
77 #define CV_SQSUM_MAT_TYPE CV_64FC1
78 #define CV_IDX_MAT_TYPE CV_16SC1
80 #define CV_STUMP_TRAIN_PORTION 100
82 #define CV_THRESHOLD_EPS (0.00001F)
84 typedef struct CvTHaarFeature
86 char desc[CV_HAAR_FEATURE_DESC_MAX];
92 } rect[CV_HAAR_FEATURE_MAX];
95 typedef struct CvFastHaarFeature
102 } rect[CV_HAAR_FEATURE_MAX];
105 typedef struct CvIntHaarFeatures
109 CvTHaarFeature* feature;
110 CvFastHaarFeature* fastfeature;
113 CV_INLINE CvTHaarFeature cvHaarFeature( const char* desc,
114 int x0, int y0, int w0, int h0, float wt0,
115 int x1, int y1, int w1, int h1, float wt1,
116 int x2 CV_DEFAULT( 0 ), int y2 CV_DEFAULT( 0 ),
117 int w2 CV_DEFAULT( 0 ), int h2 CV_DEFAULT( 0 ),
118 float wt2 CV_DEFAULT( 0.0F ) );
120 CV_INLINE CvTHaarFeature cvHaarFeature( const char* desc,
121 int x0, int y0, int w0, int h0, float wt0,
122 int x1, int y1, int w1, int h1, float wt1,
123 int x2, int y2, int w2, int h2, float wt2 )
127 assert( CV_HAAR_FEATURE_MAX >= 3 );
128 assert( strlen( desc ) < CV_HAAR_FEATURE_DESC_MAX );
130 strcpy( &(hf.desc[0]), desc );
131 hf.tilted = ( hf.desc[0] == 't' );
135 hf.rect[0].r.width = w0;
136 hf.rect[0].r.height = h0;
137 hf.rect[0].weight = wt0;
141 hf.rect[1].r.width = w1;
142 hf.rect[1].r.height = h1;
143 hf.rect[1].weight = wt1;
147 hf.rect[2].r.width = w2;
148 hf.rect[2].r.height = h2;
149 hf.rect[2].weight = wt2;
154 /* Prepared for training samples */
155 typedef struct CvHaarTrainingData
157 CvSize winsize; /* training image size */
158 int maxnum; /* maximum number of samples */
159 CvMat sum; /* sum images (each row represents image) */
160 CvMat tilted; /* tilted sum images (each row represents image) */
161 CvMat normfactor; /* normalization factor */
162 CvMat cls; /* classes. 1.0 - object, 0.0 - background */
163 CvMat weights; /* weights */
165 CvMat* valcache; /* precalculated feature values (CV_32FC1) */
166 CvMat* idxcache; /* presorted indices (CV_IDX_MAT_TYPE) */
170 /* Passed to callback functions */
171 typedef struct CvUserdata
173 CvHaarTrainingData* trainingData;
174 CvIntHaarFeatures* haarFeatures;
178 CvUserdata cvUserdata( CvHaarTrainingData* trainingData,
179 CvIntHaarFeatures* haarFeatures );
182 CvUserdata cvUserdata( CvHaarTrainingData* trainingData,
183 CvIntHaarFeatures* haarFeatures )
187 userdata.trainingData = trainingData;
188 userdata.haarFeatures = haarFeatures;
194 #define CV_INT_HAAR_CLASSIFIER_FIELDS() \
195 float (*eval)( CvIntHaarClassifier*, sum_type*, sum_type*, float ); \
196 void (*save)( CvIntHaarClassifier*, FILE* file ); \
197 void (*release)( CvIntHaarClassifier** );
199 /* internal weak classifier*/
200 typedef struct CvIntHaarClassifier
202 CV_INT_HAAR_CLASSIFIER_FIELDS()
203 } CvIntHaarClassifier;
208 typedef struct CvCARTHaarClassifier
210 CV_INT_HAAR_CLASSIFIER_FIELDS()
214 CvTHaarFeature* feature;
215 CvFastHaarFeature* fastfeature;
220 } CvCARTHaarClassifier;
222 /* internal stage classifier */
223 typedef struct CvStageHaarClassifier
225 CV_INT_HAAR_CLASSIFIER_FIELDS()
229 CvIntHaarClassifier** classifier;
230 } CvStageHaarClassifier;
232 /* internal cascade classifier */
233 typedef struct CvCascadeHaarClassifier
235 CV_INT_HAAR_CLASSIFIER_FIELDS()
238 CvIntHaarClassifier** classifier;
239 } CvCascadeHaarClassifier;
242 /* internal tree cascade classifier node */
243 typedef struct CvTreeCascadeNode
245 CvStageHaarClassifier* stage;
247 struct CvTreeCascadeNode* next;
248 struct CvTreeCascadeNode* child;
249 struct CvTreeCascadeNode* parent;
251 struct CvTreeCascadeNode* next_same_level;
252 struct CvTreeCascadeNode* child_eval;
257 /* internal tree cascade classifier */
258 typedef struct CvTreeCascadeClassifier
260 CV_INT_HAAR_CLASSIFIER_FIELDS()
262 CvTreeCascadeNode* root; /* root of the tree */
263 CvTreeCascadeNode* root_eval; /* root node for the filtering */
266 } CvTreeCascadeClassifier;
269 CV_INLINE float cvEvalFastHaarFeature( const CvFastHaarFeature* feature,
270 const sum_type* sum, const sum_type* tilted )
272 const sum_type* img = feature->tilted ? tilted : sum;
273 float ret = feature->rect[0].weight*
274 (img[feature->rect[0].p0] - img[feature->rect[0].p1] -
275 img[feature->rect[0].p2] + img[feature->rect[0].p3]) +
276 feature->rect[1].weight*
277 (img[feature->rect[1].p0] - img[feature->rect[1].p1] -
278 img[feature->rect[1].p2] + img[feature->rect[1].p3]);
280 if( feature->rect[2].weight != 0.0f )
281 ret += feature->rect[2].weight *
282 ( img[feature->rect[2].p0] - img[feature->rect[2].p1] -
283 img[feature->rect[2].p2] + img[feature->rect[2].p3] );
288 typedef struct CvSampleDistortionData
299 } CvSampleDistortionData;
302 * icvConvertToFastHaarFeature
304 * Convert to fast representation of haar features
306 * haarFeature - input array
307 * fastHaarFeature - output array
308 * size - size of arrays
309 * step - row step for the integral image
311 void icvConvertToFastHaarFeature( CvTHaarFeature* haarFeature,
312 CvFastHaarFeature* fastHaarFeature,
313 int size, int step );
316 void icvWriteVecHeader( FILE* file, int count, int width, int height );
317 void icvWriteVecSample( FILE* file, CvArr* sample );
318 void icvPlaceDistortedSample( CvArr* background,
319 int inverse, int maxintensitydev,
320 double maxxangle, double maxyangle, double maxzangle,
321 int inscribe, double maxshiftf, double maxscalef,
322 CvSampleDistortionData* data );
323 void icvEndSampleDistortion( CvSampleDistortionData* data );
325 int icvStartSampleDistortion( const char* imgfilename, int bgcolor, int bgthreshold,
326 CvSampleDistortionData* data );
328 typedef int (*CvGetHaarTrainingDataCallback)( CvMat* img, void* userdata );
330 typedef struct CvVecFile
339 int icvGetHaarTraininDataFromVecCallback( CvMat* img, void* userdata );
342 * icvGetHaarTrainingDataFromVec
344 * Fill <data> with samples from .vec file, passed <cascade>
345 int icvGetHaarTrainingDataFromVec( CvHaarTrainingData* data, int first, int count,
346 CvIntHaarClassifier* cascade,
347 const char* filename,
351 CvIntHaarClassifier* icvCreateCARTHaarClassifier( int count );
353 void icvReleaseHaarClassifier( CvIntHaarClassifier** classifier );
355 void icvInitCARTHaarClassifier( CvCARTHaarClassifier* carthaar, CvCARTClassifier* cart,
356 CvIntHaarFeatures* intHaarFeatures );
358 float icvEvalCARTHaarClassifier( CvIntHaarClassifier* classifier,
359 sum_type* sum, sum_type* tilted, float normfactor );
361 CvIntHaarClassifier* icvCreateStageHaarClassifier( int count, float threshold );
363 void icvReleaseStageHaarClassifier( CvIntHaarClassifier** classifier );
365 float icvEvalStageHaarClassifier( CvIntHaarClassifier* classifier,
366 sum_type* sum, sum_type* tilted, float normfactor );
368 CvIntHaarClassifier* icvCreateCascadeHaarClassifier( int count );
370 void icvReleaseCascadeHaarClassifier( CvIntHaarClassifier** classifier );
372 float icvEvalCascadeHaarClassifier( CvIntHaarClassifier* classifier,
373 sum_type* sum, sum_type* tilted, float normfactor );
375 void icvSaveHaarFeature( CvTHaarFeature* feature, FILE* file );
377 void icvLoadHaarFeature( CvTHaarFeature* feature, FILE* file );
379 void icvSaveCARTHaarClassifier( CvIntHaarClassifier* classifier, FILE* file );
381 CvIntHaarClassifier* icvLoadCARTHaarClassifier( FILE* file, int step );
383 void icvSaveStageHaarClassifier( CvIntHaarClassifier* classifier, FILE* file );
385 CvIntHaarClassifier* icvLoadCARTStageHaarClassifier( const char* filename, int step );
388 /* tree cascade classifier */
390 float icvEvalTreeCascadeClassifier( CvIntHaarClassifier* classifier,
391 sum_type* sum, sum_type* tilted, float normfactor );
393 void icvSetLeafNode( CvTreeCascadeClassifier* tree, CvTreeCascadeNode* leaf );
395 float icvEvalTreeCascadeClassifierFilter( CvIntHaarClassifier* classifier, sum_type* sum,
396 sum_type* tilted, float normfactor );
398 CvTreeCascadeNode* icvCreateTreeCascadeNode();
400 void icvReleaseTreeCascadeNodes( CvTreeCascadeNode** node );
402 void icvReleaseTreeCascadeClassifier( CvIntHaarClassifier** classifier );
404 /* Prints out current tree structure to <stdout> */
405 void icvPrintTreeCascade( CvTreeCascadeNode* root );
407 /* Loads tree cascade classifier */
408 CvIntHaarClassifier* icvLoadTreeCascadeClassifier( const char* filename, int step,
411 /* Finds leaves belonging to maximal level and connects them via leaf->next_same_level */
412 CvTreeCascadeNode* icvFindDeepestLeaves( CvTreeCascadeClassifier* tree );
414 #endif /* __CVHAARTRAINING_H_ */