Update to 2.0.0 tree from current Fremantle build
[opencv] / apps / haartraining / _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 <cstring>
54 #include <cstdio>
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 /* print statistic info */
65 #define CV_VERBOSE 1
66
67 #define CV_STAGE_CART_FILE_NAME "AdaBoostCARTHaarClassifier.txt"
68
69 #define CV_HAAR_FEATURE_MAX      3
70 #define CV_HAAR_FEATURE_DESC_MAX 20
71
72 typedef int    sum_type;
73 typedef double sqsum_type;
74 typedef short  idx_type;
75
76 #define CV_SUM_MAT_TYPE CV_32SC1
77 #define CV_SQSUM_MAT_TYPE CV_64FC1
78 #define CV_IDX_MAT_TYPE CV_16SC1
79
80 #define CV_STUMP_TRAIN_PORTION 100
81
82 #define CV_THRESHOLD_EPS (0.00001F)
83
84 typedef struct CvTHaarFeature
85 {
86     char desc[CV_HAAR_FEATURE_DESC_MAX];
87     int  tilted;
88     struct
89     {
90         CvRect r;
91         float weight;
92     } rect[CV_HAAR_FEATURE_MAX];
93 } CvTHaarFeature;
94
95 typedef struct CvFastHaarFeature
96 {
97     int tilted;
98     struct
99     {
100         int p0, p1, p2, p3;
101         float weight;
102     } rect[CV_HAAR_FEATURE_MAX];
103 } CvFastHaarFeature;
104
105 typedef struct CvIntHaarFeatures
106 {
107     CvSize winsize;
108     int count;
109     CvTHaarFeature* feature;
110     CvFastHaarFeature* fastfeature;
111 } CvIntHaarFeatures;
112
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 ) );
119
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 )
124 {
125     CvTHaarFeature hf;
126
127     assert( CV_HAAR_FEATURE_MAX >= 3 );
128     assert( strlen( desc ) < CV_HAAR_FEATURE_DESC_MAX );
129
130     strcpy( &(hf.desc[0]), desc );
131     hf.tilted = ( hf.desc[0] == 't' );
132     
133     hf.rect[0].r.x = x0;
134     hf.rect[0].r.y = y0;
135     hf.rect[0].r.width  = w0;
136     hf.rect[0].r.height = h0;
137     hf.rect[0].weight   = wt0;
138     
139     hf.rect[1].r.x = x1;
140     hf.rect[1].r.y = y1;
141     hf.rect[1].r.width  = w1;
142     hf.rect[1].r.height = h1;
143     hf.rect[1].weight   = wt1;
144     
145     hf.rect[2].r.x = x2;
146     hf.rect[2].r.y = y2;
147     hf.rect[2].r.width  = w2;
148     hf.rect[2].r.height = h2;
149     hf.rect[2].weight   = wt2;
150     
151     return hf;
152 }
153
154 /* Prepared for training samples */
155 typedef struct CvHaarTrainingData
156 {
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 */
164
165     CvMat* valcache;    /* precalculated feature values (CV_32FC1) */
166     CvMat* idxcache;    /* presorted indices (CV_IDX_MAT_TYPE) */
167 } CvHaarTrainigData;
168
169
170 /* Passed to callback functions */
171 typedef struct CvUserdata
172 {
173     CvHaarTrainingData* trainingData;
174     CvIntHaarFeatures* haarFeatures;
175 } CvUserdata;
176
177 CV_INLINE
178 CvUserdata cvUserdata( CvHaarTrainingData* trainingData,
179                        CvIntHaarFeatures* haarFeatures );
180
181 CV_INLINE
182 CvUserdata cvUserdata( CvHaarTrainingData* trainingData,
183                        CvIntHaarFeatures* haarFeatures )
184 {
185     CvUserdata userdata;
186
187     userdata.trainingData = trainingData;
188     userdata.haarFeatures = haarFeatures;
189
190     return userdata;
191 }
192
193
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** );
198
199 /* internal weak classifier*/
200 typedef struct CvIntHaarClassifier
201 {
202     CV_INT_HAAR_CLASSIFIER_FIELDS()
203 } CvIntHaarClassifier;
204
205 /*
206  * CART classifier
207  */
208 typedef struct CvCARTHaarClassifier
209 {
210     CV_INT_HAAR_CLASSIFIER_FIELDS()
211
212     int count;
213     int* compidx;
214     CvTHaarFeature* feature;
215     CvFastHaarFeature* fastfeature;
216     float* threshold;
217     int* left;
218     int* right;
219     float* val;
220 } CvCARTHaarClassifier;
221
222 /* internal stage classifier */
223 typedef struct CvStageHaarClassifier
224 {
225     CV_INT_HAAR_CLASSIFIER_FIELDS()
226         
227     int count;
228     float threshold;
229     CvIntHaarClassifier** classifier;
230 } CvStageHaarClassifier;
231
232 /* internal cascade classifier */
233 typedef struct CvCascadeHaarClassifier
234 {
235     CV_INT_HAAR_CLASSIFIER_FIELDS()
236
237     int count;
238     CvIntHaarClassifier** classifier;
239 } CvCascadeHaarClassifier;
240
241
242 /* internal tree cascade classifier node */
243 typedef struct CvTreeCascadeNode
244 {
245     CvStageHaarClassifier* stage;
246
247     struct CvTreeCascadeNode* next;
248     struct CvTreeCascadeNode* child;
249     struct CvTreeCascadeNode* parent;
250
251     struct CvTreeCascadeNode* next_same_level;
252     struct CvTreeCascadeNode* child_eval;
253     int idx;
254     int leaf;
255 } CvTreeCascadeNode;
256
257 /* internal tree cascade classifier */
258 typedef struct CvTreeCascadeClassifier
259 {
260     CV_INT_HAAR_CLASSIFIER_FIELDS()
261
262     CvTreeCascadeNode* root;      /* root of the tree */
263     CvTreeCascadeNode* root_eval; /* root node for the filtering */
264
265     int next_idx;
266 } CvTreeCascadeClassifier;
267
268
269 CV_INLINE float cvEvalFastHaarFeature( const CvFastHaarFeature* feature,
270                                        const sum_type* sum, const sum_type* tilted )
271 {
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]);
279
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] );
284     return ret;
285 }
286
287
288 typedef struct CvSampleDistortionData
289 {
290     IplImage* src;
291     IplImage* erode;
292     IplImage* dilate;
293     IplImage* mask;
294     IplImage* img;
295     IplImage* maskimg;
296     int dx;
297     int dy;
298     int bgcolor;
299 } CvSampleDistortionData;
300
301 /*
302  * icvConvertToFastHaarFeature
303  *
304  * Convert to fast representation of haar features
305  *
306  * haarFeature     - input array
307  * fastHaarFeature - output array
308  * size            - size of arrays
309  * step            - row step for the integral image
310  */
311 void icvConvertToFastHaarFeature( CvTHaarFeature* haarFeature,
312                                   CvFastHaarFeature* fastHaarFeature,
313                                   int size, int step );
314
315
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 );
324
325 int icvStartSampleDistortion( const char* imgfilename, int bgcolor, int bgthreshold,
326                               CvSampleDistortionData* data );
327
328 typedef int (*CvGetHaarTrainingDataCallback)( CvMat* img, void* userdata );
329
330 typedef struct CvVecFile
331 {
332     FILE*  input;
333     int    count;
334     int    vecsize;
335     int    last;
336     short* vector;
337 } CvVecFile;
338
339 int icvGetHaarTraininDataFromVecCallback( CvMat* img, void* userdata );
340
341 /*
342  * icvGetHaarTrainingDataFromVec
343  *
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,
348                                    int* consumed );
349  */
350
351 CvIntHaarClassifier* icvCreateCARTHaarClassifier( int count );
352
353 void icvReleaseHaarClassifier( CvIntHaarClassifier** classifier );
354
355 void icvInitCARTHaarClassifier( CvCARTHaarClassifier* carthaar, CvCARTClassifier* cart,
356                                 CvIntHaarFeatures* intHaarFeatures );
357
358 float icvEvalCARTHaarClassifier( CvIntHaarClassifier* classifier,
359                                  sum_type* sum, sum_type* tilted, float normfactor );
360
361 CvIntHaarClassifier* icvCreateStageHaarClassifier( int count, float threshold );
362
363 void icvReleaseStageHaarClassifier( CvIntHaarClassifier** classifier );
364
365 float icvEvalStageHaarClassifier( CvIntHaarClassifier* classifier,
366                                   sum_type* sum, sum_type* tilted, float normfactor );
367
368 CvIntHaarClassifier* icvCreateCascadeHaarClassifier( int count );
369
370 void icvReleaseCascadeHaarClassifier( CvIntHaarClassifier** classifier );
371
372 float icvEvalCascadeHaarClassifier( CvIntHaarClassifier* classifier,
373                                     sum_type* sum, sum_type* tilted, float normfactor );
374
375 void icvSaveHaarFeature( CvTHaarFeature* feature, FILE* file );
376
377 void icvLoadHaarFeature( CvTHaarFeature* feature, FILE* file );
378
379 void icvSaveCARTHaarClassifier( CvIntHaarClassifier* classifier, FILE* file );
380
381 CvIntHaarClassifier* icvLoadCARTHaarClassifier( FILE* file, int step );
382
383 void icvSaveStageHaarClassifier( CvIntHaarClassifier* classifier, FILE* file );
384
385 CvIntHaarClassifier* icvLoadCARTStageHaarClassifier( const char* filename, int step );
386
387
388 /* tree cascade classifier */
389
390 float icvEvalTreeCascadeClassifier( CvIntHaarClassifier* classifier,
391                                     sum_type* sum, sum_type* tilted, float normfactor );
392
393 void icvSetLeafNode( CvTreeCascadeClassifier* tree, CvTreeCascadeNode* leaf );
394
395 float icvEvalTreeCascadeClassifierFilter( CvIntHaarClassifier* classifier, sum_type* sum,
396                                           sum_type* tilted, float normfactor );
397
398 CvTreeCascadeNode* icvCreateTreeCascadeNode();
399
400 void icvReleaseTreeCascadeNodes( CvTreeCascadeNode** node );
401
402 void icvReleaseTreeCascadeClassifier( CvIntHaarClassifier** classifier );
403
404 /* Prints out current tree structure to <stdout> */
405 void icvPrintTreeCascade( CvTreeCascadeNode* root );
406
407 /* Loads tree cascade classifier */
408 CvIntHaarClassifier* icvLoadTreeCascadeClassifier( const char* filename, int step,
409                                                    int* splits );
410
411 /* Finds leaves belonging to maximal level and connects them via leaf->next_same_level */
412 CvTreeCascadeNode* icvFindDeepestLeaves( CvTreeCascadeClassifier* tree );
413
414 #endif /* __CVHAARTRAINING_H_ */