Update the changelog
[opencv] / apps / haartraining / src / _cvhaartraining.h
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 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
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.
25 //
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.
28 //
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.
39 //
40 //M*/
41
42 /*
43  * _cvhaartraining.h
44  *
45  * training of cascade of boosted classifiers based on haar features
46  */
47
48 #ifndef __CVHAARTRAINING_H_
49 #define __CVHAARTRAINING_H_
50
51 #include <_cvcommon.h>
52 #include <cvclassifier.h>
53 #include <string.h>
54 #include <stdio.h>
55
56 /* parameters for tree cascade classifier training */
57
58 /* max number of clusters */
59 #define CV_MAX_CLUSTERS 3
60
61 /* term criteria for K-Means */
62 #define CV_TERM_CRITERIA() cvTermCriteria( CV_TERMCRIT_EPS, 1000, 1E-5 )
63
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
67
68 /* print statistic info */
69 #define CV_VERBOSE 1
70
71 #define CV_STAGE_CART_FILE_NAME "AdaBoostCARTHaarClassifier.txt"
72
73 #define CV_HAAR_FEATURE_MAX      3
74 #define CV_HAAR_FEATURE_DESC_MAX 20
75
76 typedef int    sum_type;
77 typedef double sqsum_type;
78 typedef short  idx_type;
79
80 #define CV_SUM_MAT_TYPE CV_32SC1
81 #define CV_SQSUM_MAT_TYPE CV_64FC1
82 #define CV_IDX_MAT_TYPE CV_16SC1
83
84 #define CV_STUMP_TRAIN_PORTION 100
85
86 #define CV_THRESHOLD_EPS (0.00001F)
87
88 typedef struct CvTHaarFeature
89 {
90     char desc[CV_HAAR_FEATURE_DESC_MAX];
91     int  tilted;
92     struct
93     {
94         CvRect r;
95         float weight;
96     } rect[CV_HAAR_FEATURE_MAX];
97 } CvTHaarFeature;
98
99 typedef struct CvFastHaarFeature
100 {
101     int tilted;
102     struct
103     {
104         int p0, p1, p2, p3;
105         float weight;
106     } rect[CV_HAAR_FEATURE_MAX];
107 } CvFastHaarFeature;
108
109 typedef struct CvIntHaarFeatures
110 {
111     CvSize winsize;
112     int count;
113     CvTHaarFeature* feature;
114     CvFastHaarFeature* fastfeature;
115 } CvIntHaarFeatures;
116
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 ) );
123
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 )
128 {
129     CvTHaarFeature hf;
130
131     assert( CV_HAAR_FEATURE_MAX >= 3 );
132     assert( strlen( desc ) < CV_HAAR_FEATURE_DESC_MAX );
133
134     strcpy( &(hf.desc[0]), desc );
135     hf.tilted = ( hf.desc[0] == 't' );
136     
137     hf.rect[0].r.x = x0;
138     hf.rect[0].r.y = y0;
139     hf.rect[0].r.width  = w0;
140     hf.rect[0].r.height = h0;
141     hf.rect[0].weight   = wt0;
142     
143     hf.rect[1].r.x = x1;
144     hf.rect[1].r.y = y1;
145     hf.rect[1].r.width  = w1;
146     hf.rect[1].r.height = h1;
147     hf.rect[1].weight   = wt1;
148     
149     hf.rect[2].r.x = x2;
150     hf.rect[2].r.y = y2;
151     hf.rect[2].r.width  = w2;
152     hf.rect[2].r.height = h2;
153     hf.rect[2].weight   = wt2;
154     
155     return hf;
156 }
157
158 /* Prepared for training samples */
159 typedef struct CvHaarTrainingData
160 {
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 */
168
169     CvMat* valcache;    /* precalculated feature values (CV_32FC1) */
170     CvMat* idxcache;    /* presorted indices (CV_IDX_MAT_TYPE) */
171 } CvHaarTrainigData;
172
173
174 /* Passed to callback functions */
175 typedef struct CvUserdata
176 {
177     CvHaarTrainingData* trainingData;
178     CvIntHaarFeatures* haarFeatures;
179 } CvUserdata;
180
181 CV_INLINE
182 CvUserdata cvUserdata( CvHaarTrainingData* trainingData,
183                        CvIntHaarFeatures* haarFeatures );
184
185 CV_INLINE
186 CvUserdata cvUserdata( CvHaarTrainingData* trainingData,
187                        CvIntHaarFeatures* haarFeatures )
188 {
189     CvUserdata userdata;
190
191     userdata.trainingData = trainingData;
192     userdata.haarFeatures = haarFeatures;
193
194     return userdata;
195 }
196
197
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** );
202
203 /* internal weak classifier*/
204 typedef struct CvIntHaarClassifier
205 {
206     CV_INT_HAAR_CLASSIFIER_FIELDS()
207 } CvIntHaarClassifier;
208
209 /*
210  * CART classifier
211  */
212 typedef struct CvCARTHaarClassifier
213 {
214     CV_INT_HAAR_CLASSIFIER_FIELDS()
215
216     int count;
217     int* compidx;
218     CvTHaarFeature* feature;
219     CvFastHaarFeature* fastfeature;
220     float* threshold;
221     int* left;
222     int* right;
223     float* val;
224 } CvCARTHaarClassifier;
225
226 /* internal stage classifier */
227 typedef struct CvStageHaarClassifier
228 {
229     CV_INT_HAAR_CLASSIFIER_FIELDS()
230         
231     int count;
232     float threshold;
233     CvIntHaarClassifier** classifier;
234 } CvStageHaarClassifier;
235
236 /* internal cascade classifier */
237 typedef struct CvCascadeHaarClassifier
238 {
239     CV_INT_HAAR_CLASSIFIER_FIELDS()
240
241     int count;
242     CvIntHaarClassifier** classifier;
243 } CvCascadeHaarClassifier;
244
245
246 /* internal tree cascade classifier node */
247 typedef struct CvTreeCascadeNode
248 {
249     CvStageHaarClassifier* stage;
250
251     struct CvTreeCascadeNode* next;
252     struct CvTreeCascadeNode* child;
253     struct CvTreeCascadeNode* parent;
254
255     struct CvTreeCascadeNode* next_same_level;
256     struct CvTreeCascadeNode* child_eval;
257     int idx;
258     int leaf;
259 } CvTreeCascadeNode;
260
261 /* internal tree cascade classifier */
262 typedef struct CvTreeCascadeClassifier
263 {
264     CV_INT_HAAR_CLASSIFIER_FIELDS()
265
266     CvTreeCascadeNode* root;      /* root of the tree */
267     CvTreeCascadeNode* root_eval; /* root node for the filtering */
268
269     int next_idx;
270 } CvTreeCascadeClassifier;
271
272
273 CV_INLINE float cvEvalFastHaarFeature( CvFastHaarFeature* feature,
274                                        sum_type* sum, sum_type* tilted )
275 {
276     sum_type* img = NULL;
277     int i = 0;
278     float ret = 0.0F;
279     
280     assert( feature );
281     
282     img = ( feature->tilted ) ? tilted : sum;
283
284     assert( img );
285
286     for( i = 0; feature->rect[i].weight != 0.0F && i < CV_HAAR_FEATURE_MAX; i++ )
287     {
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] );
291     }
292
293     return ret;
294 }
295
296 typedef struct CvSampleDistortionData
297 {
298     IplImage* src;
299     IplImage* erode;
300     IplImage* dilate;
301     IplImage* mask;
302     IplImage* img;
303     IplImage* maskimg;
304     int dx;
305     int dy;
306     int bgcolor;
307 } CvSampleDistortionData;
308
309 /*
310  * icvConvertToFastHaarFeature
311  *
312  * Convert to fast representation of haar features
313  *
314  * haarFeature     - input array
315  * fastHaarFeature - output array
316  * size            - size of arrays
317  * step            - row step for the integral image
318  */
319 void icvConvertToFastHaarFeature( CvTHaarFeature* haarFeature,
320                                   CvFastHaarFeature* fastHaarFeature,
321                                   int size, int step );
322
323
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 );
332
333 int icvStartSampleDistortion( const char* imgfilename, int bgcolor, int bgthreshold,
334                               CvSampleDistortionData* data );
335
336 typedef int (*CvGetHaarTrainingDataCallback)( CvMat* img, void* userdata );
337
338 typedef struct CvVecFile
339 {
340     FILE*  input;
341     int    count;
342     int    vecsize;
343     int    last;
344     short* vector;
345 } CvVecFile;
346
347 int icvGetHaarTraininDataFromVecCallback( CvMat* img, void* userdata );
348
349 /*
350  * icvGetHaarTrainingDataFromVec
351  *
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,
356                                    int* consumed );
357  */
358
359 CvIntHaarClassifier* icvCreateCARTHaarClassifier( int count );
360
361 void icvReleaseHaarClassifier( CvIntHaarClassifier** classifier );
362
363 void icvInitCARTHaarClassifier( CvCARTHaarClassifier* carthaar, CvCARTClassifier* cart,
364                                 CvIntHaarFeatures* intHaarFeatures );
365
366 float icvEvalCARTHaarClassifier( CvIntHaarClassifier* classifier,
367                                  sum_type* sum, sum_type* tilted, float normfactor );
368
369 CvIntHaarClassifier* icvCreateStageHaarClassifier( int count, float threshold );
370
371 void icvReleaseStageHaarClassifier( CvIntHaarClassifier** classifier );
372
373 float icvEvalStageHaarClassifier( CvIntHaarClassifier* classifier,
374                                   sum_type* sum, sum_type* tilted, float normfactor );
375
376 CvIntHaarClassifier* icvCreateCascadeHaarClassifier( int count );
377
378 void icvReleaseCascadeHaarClassifier( CvIntHaarClassifier** classifier );
379
380 float icvEvalCascadeHaarClassifier( CvIntHaarClassifier* classifier,
381                                     sum_type* sum, sum_type* tilted, float normfactor );
382
383 void icvSaveHaarFeature( CvTHaarFeature* feature, FILE* file );
384
385 void icvLoadHaarFeature( CvTHaarFeature* feature, FILE* file );
386
387 void icvSaveCARTHaarClassifier( CvIntHaarClassifier* classifier, FILE* file );
388
389 CvIntHaarClassifier* icvLoadCARTHaarClassifier( FILE* file, int step );
390
391 void icvSaveStageHaarClassifier( CvIntHaarClassifier* classifier, FILE* file );
392
393 CvIntHaarClassifier* icvLoadCARTStageHaarClassifier( const char* filename, int step );
394
395
396 /* tree cascade classifier */
397
398 float icvEvalTreeCascadeClassifier( CvIntHaarClassifier* classifier,
399                                     sum_type* sum, sum_type* tilted, float normfactor );
400
401 void icvSetLeafNode( CvTreeCascadeClassifier* tree, CvTreeCascadeNode* leaf );
402
403 float icvEvalTreeCascadeClassifierFilter( CvIntHaarClassifier* classifier, sum_type* sum,
404                                           sum_type* tilted, float normfactor );
405
406 CvTreeCascadeNode* icvCreateTreeCascadeNode();
407
408 void icvReleaseTreeCascadeNodes( CvTreeCascadeNode** node );
409
410 void icvReleaseTreeCascadeClassifier( CvIntHaarClassifier** classifier );
411
412 /* Prints out current tree structure to <stdout> */
413 void icvPrintTreeCascade( CvTreeCascadeNode* root );
414
415 /* Loads tree cascade classifier */
416 CvIntHaarClassifier* icvLoadTreeCascadeClassifier( const char* filename, int step,
417                                                    int* splits );
418
419 /* Finds leaves belonging to maximal level and connects them via leaf->next_same_level */
420 CvTreeCascadeNode* icvFindDeepestLeaves( CvTreeCascadeClassifier* tree );
421
422 #endif /* __CVHAARTRAINING_H_ */