Move the sources to trunk
[opencv] / cv / src / cvhaar.cpp
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 /* Haar features calculation */
43
44 #include "_cv.h"
45 #include <stdio.h>
46
47 /* these settings affect the quality of detection: change with care */
48 #define CV_ADJUST_FEATURES 1
49 #define CV_ADJUST_WEIGHTS  0
50
51 typedef int sumtype;
52 typedef double sqsumtype;
53
54 typedef struct CvHidHaarFeature
55 {
56     struct
57     {
58         sumtype *p0, *p1, *p2, *p3;
59         float weight;
60     }
61     rect[CV_HAAR_FEATURE_MAX];
62 }
63 CvHidHaarFeature;
64
65
66 typedef struct CvHidHaarTreeNode
67 {
68     CvHidHaarFeature feature;
69     float threshold;
70     int left;
71     int right;
72 }
73 CvHidHaarTreeNode;
74
75
76 typedef struct CvHidHaarClassifier
77 {
78     int count;
79     //CvHaarFeature* orig_feature;
80     CvHidHaarTreeNode* node;
81     float* alpha;
82 }
83 CvHidHaarClassifier;
84
85
86 typedef struct CvHidHaarStageClassifier
87 {
88     int  count;
89     float threshold;
90     CvHidHaarClassifier* classifier;
91     int two_rects;
92
93     struct CvHidHaarStageClassifier* next;
94     struct CvHidHaarStageClassifier* child;
95     struct CvHidHaarStageClassifier* parent;
96 }
97 CvHidHaarStageClassifier;
98
99
100 struct CvHidHaarClassifierCascade
101 {
102     int  count;
103     int  is_stump_based;
104     int  has_tilted_features;
105     int  is_tree;
106     double inv_window_area;
107     CvMat sum, sqsum, tilted;
108     CvHidHaarStageClassifier* stage_classifier;
109     sqsumtype *pq0, *pq1, *pq2, *pq3;
110     sumtype *p0, *p1, *p2, *p3;
111
112     void** ipp_stages;
113 };
114
115
116 /* IPP functions for object detection */
117 icvHaarClassifierInitAlloc_32f_t icvHaarClassifierInitAlloc_32f_p = 0;
118 icvHaarClassifierFree_32f_t icvHaarClassifierFree_32f_p = 0;
119 icvApplyHaarClassifier_32s32f_C1R_t icvApplyHaarClassifier_32s32f_C1R_p = 0;
120 icvRectStdDev_32s32f_C1R_t icvRectStdDev_32s32f_C1R_p = 0;
121
122 const int icv_object_win_border = 1;
123 const float icv_stage_threshold_bias = 0.0001f;
124
125 static CvHaarClassifierCascade*
126 icvCreateHaarClassifierCascade( int stage_count )
127 {
128     CvHaarClassifierCascade* cascade = 0;
129
130     CV_FUNCNAME( "icvCreateHaarClassifierCascade" );
131
132     __BEGIN__;
133
134     int block_size = sizeof(*cascade) + stage_count*sizeof(*cascade->stage_classifier);
135
136     if( stage_count <= 0 )
137         CV_ERROR( CV_StsOutOfRange, "Number of stages should be positive" );
138
139     CV_CALL( cascade = (CvHaarClassifierCascade*)cvAlloc( block_size ));
140     memset( cascade, 0, block_size );
141
142     cascade->stage_classifier = (CvHaarStageClassifier*)(cascade + 1);
143     cascade->flags = CV_HAAR_MAGIC_VAL;
144     cascade->count = stage_count;
145
146     __END__;
147
148     return cascade;
149 }
150
151 static void
152 icvReleaseHidHaarClassifierCascade( CvHidHaarClassifierCascade** _cascade )
153 {
154     if( _cascade && *_cascade )
155     {
156         CvHidHaarClassifierCascade* cascade = *_cascade;
157         if( cascade->ipp_stages && icvHaarClassifierFree_32f_p )
158         {
159             int i;
160             for( i = 0; i < cascade->count; i++ )
161             {
162                 if( cascade->ipp_stages[i] )
163                     icvHaarClassifierFree_32f_p( cascade->ipp_stages[i] );
164             }
165         }
166         cvFree( &cascade->ipp_stages );
167         cvFree( _cascade );
168     }
169 }
170
171 /* create more efficient internal representation of haar classifier cascade */
172 static CvHidHaarClassifierCascade*
173 icvCreateHidHaarClassifierCascade( CvHaarClassifierCascade* cascade )
174 {
175     CvRect* ipp_features = 0;
176     float *ipp_weights = 0, *ipp_thresholds = 0, *ipp_val1 = 0, *ipp_val2 = 0;
177     int* ipp_counts = 0;
178
179     CvHidHaarClassifierCascade* out = 0;
180
181     CV_FUNCNAME( "icvCreateHidHaarClassifierCascade" );
182
183     __BEGIN__;
184
185     int i, j, k, l;
186     int datasize;
187     int total_classifiers = 0;
188     int total_nodes = 0;
189     char errorstr[100];
190     CvHidHaarClassifier* haar_classifier_ptr;
191     CvHidHaarTreeNode* haar_node_ptr;
192     CvSize orig_window_size;
193     int has_tilted_features = 0;
194     int max_count = 0;
195
196     if( !CV_IS_HAAR_CLASSIFIER(cascade) )
197         CV_ERROR( !cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier pointer" );
198
199     if( cascade->hid_cascade )
200         CV_ERROR( CV_StsError, "hid_cascade has been already created" );
201
202     if( !cascade->stage_classifier )
203         CV_ERROR( CV_StsNullPtr, "" );
204
205     if( cascade->count <= 0 )
206         CV_ERROR( CV_StsOutOfRange, "Negative number of cascade stages" );
207
208     orig_window_size = cascade->orig_window_size;
209
210     /* check input structure correctness and calculate total memory size needed for
211        internal representation of the classifier cascade */
212     for( i = 0; i < cascade->count; i++ )
213     {
214         CvHaarStageClassifier* stage_classifier = cascade->stage_classifier + i;
215
216         if( !stage_classifier->classifier ||
217             stage_classifier->count <= 0 )
218         {
219             sprintf( errorstr, "header of the stage classifier #%d is invalid "
220                      "(has null pointers or non-positive classfier count)", i );
221             CV_ERROR( CV_StsError, errorstr );
222         }
223
224         max_count = MAX( max_count, stage_classifier->count );
225         total_classifiers += stage_classifier->count;
226
227         for( j = 0; j < stage_classifier->count; j++ )
228         {
229             CvHaarClassifier* classifier = stage_classifier->classifier + j;
230
231             total_nodes += classifier->count;
232             for( l = 0; l < classifier->count; l++ )
233             {
234                 for( k = 0; k < CV_HAAR_FEATURE_MAX; k++ )
235                 {
236                     if( classifier->haar_feature[l].rect[k].r.width )
237                     {
238                         CvRect r = classifier->haar_feature[l].rect[k].r;
239                         int tilted = classifier->haar_feature[l].tilted;
240                         has_tilted_features |= tilted != 0;
241                         if( r.width < 0 || r.height < 0 || r.y < 0 ||
242                             r.x + r.width > orig_window_size.width
243                             ||
244                             (!tilted &&
245                             (r.x < 0 || r.y + r.height > orig_window_size.height))
246                             ||
247                             (tilted && (r.x - r.height < 0 ||
248                             r.y + r.width + r.height > orig_window_size.height)))
249                         {
250                             sprintf( errorstr, "rectangle #%d of the classifier #%d of "
251                                      "the stage classifier #%d is not inside "
252                                      "the reference (original) cascade window", k, j, i );
253                             CV_ERROR( CV_StsNullPtr, errorstr );
254                         }
255                     }
256                 }
257             }
258         }
259     }
260
261     // this is an upper boundary for the whole hidden cascade size
262     datasize = sizeof(CvHidHaarClassifierCascade) +
263                sizeof(CvHidHaarStageClassifier)*cascade->count +
264                sizeof(CvHidHaarClassifier) * total_classifiers +
265                sizeof(CvHidHaarTreeNode) * total_nodes +
266                sizeof(void*)*(total_nodes + total_classifiers);
267
268     CV_CALL( out = (CvHidHaarClassifierCascade*)cvAlloc( datasize ));
269     memset( out, 0, sizeof(*out) );
270
271     /* init header */
272     out->count = cascade->count;
273     out->stage_classifier = (CvHidHaarStageClassifier*)(out + 1);
274     haar_classifier_ptr = (CvHidHaarClassifier*)(out->stage_classifier + cascade->count);
275     haar_node_ptr = (CvHidHaarTreeNode*)(haar_classifier_ptr + total_classifiers);
276
277     out->is_stump_based = 1;
278     out->has_tilted_features = has_tilted_features;
279     out->is_tree = 0;
280
281     /* initialize internal representation */
282     for( i = 0; i < cascade->count; i++ )
283     {
284         CvHaarStageClassifier* stage_classifier = cascade->stage_classifier + i;
285         CvHidHaarStageClassifier* hid_stage_classifier = out->stage_classifier + i;
286
287         hid_stage_classifier->count = stage_classifier->count;
288         hid_stage_classifier->threshold = stage_classifier->threshold - icv_stage_threshold_bias;
289         hid_stage_classifier->classifier = haar_classifier_ptr;
290         hid_stage_classifier->two_rects = 1;
291         haar_classifier_ptr += stage_classifier->count;
292
293         hid_stage_classifier->parent = (stage_classifier->parent == -1)
294             ? NULL : out->stage_classifier + stage_classifier->parent;
295         hid_stage_classifier->next = (stage_classifier->next == -1)
296             ? NULL : out->stage_classifier + stage_classifier->next;
297         hid_stage_classifier->child = (stage_classifier->child == -1)
298             ? NULL : out->stage_classifier + stage_classifier->child;
299
300         out->is_tree |= hid_stage_classifier->next != NULL;
301
302         for( j = 0; j < stage_classifier->count; j++ )
303         {
304             CvHaarClassifier* classifier = stage_classifier->classifier + j;
305             CvHidHaarClassifier* hid_classifier = hid_stage_classifier->classifier + j;
306             int node_count = classifier->count;
307             float* alpha_ptr = (float*)(haar_node_ptr + node_count);
308
309             hid_classifier->count = node_count;
310             hid_classifier->node = haar_node_ptr;
311             hid_classifier->alpha = alpha_ptr;
312
313             for( l = 0; l < node_count; l++ )
314             {
315                 CvHidHaarTreeNode* node = hid_classifier->node + l;
316                 CvHaarFeature* feature = classifier->haar_feature + l;
317                 memset( node, -1, sizeof(*node) );
318                 node->threshold = classifier->threshold[l];
319                 node->left = classifier->left[l];
320                 node->right = classifier->right[l];
321
322                 if( fabs(feature->rect[2].weight) < DBL_EPSILON ||
323                     feature->rect[2].r.width == 0 ||
324                     feature->rect[2].r.height == 0 )
325                     memset( &(node->feature.rect[2]), 0, sizeof(node->feature.rect[2]) );
326                 else
327                     hid_stage_classifier->two_rects = 0;
328             }
329
330             memcpy( alpha_ptr, classifier->alpha, (node_count+1)*sizeof(alpha_ptr[0]));
331             haar_node_ptr =
332                 (CvHidHaarTreeNode*)cvAlignPtr(alpha_ptr+node_count+1, sizeof(void*));
333
334             out->is_stump_based &= node_count == 1;
335         }
336     }
337
338     //
339     // NOTE: Currently, OpenMP is implemented and IPP modes are incompatible.
340     //
341 #ifndef _OPENMP
342     {
343     int can_use_ipp = icvHaarClassifierInitAlloc_32f_p != 0 &&
344         icvHaarClassifierFree_32f_p != 0 &&
345                       icvApplyHaarClassifier_32s32f_C1R_p != 0 &&
346                       icvRectStdDev_32s32f_C1R_p != 0 &&
347                       !out->has_tilted_features && !out->is_tree && out->is_stump_based;
348
349     if( can_use_ipp )
350     {
351         int ipp_datasize = cascade->count*sizeof(out->ipp_stages[0]);
352         float ipp_weight_scale=(float)(1./((orig_window_size.width-icv_object_win_border*2)*
353             (orig_window_size.height-icv_object_win_border*2)));
354
355         CV_CALL( out->ipp_stages = (void**)cvAlloc( ipp_datasize ));
356         memset( out->ipp_stages, 0, ipp_datasize );
357
358         CV_CALL( ipp_features = (CvRect*)cvAlloc( max_count*3*sizeof(ipp_features[0]) ));
359         CV_CALL( ipp_weights = (float*)cvAlloc( max_count*3*sizeof(ipp_weights[0]) ));
360         CV_CALL( ipp_thresholds = (float*)cvAlloc( max_count*sizeof(ipp_thresholds[0]) ));
361         CV_CALL( ipp_val1 = (float*)cvAlloc( max_count*sizeof(ipp_val1[0]) ));
362         CV_CALL( ipp_val2 = (float*)cvAlloc( max_count*sizeof(ipp_val2[0]) ));
363         CV_CALL( ipp_counts = (int*)cvAlloc( max_count*sizeof(ipp_counts[0]) ));
364
365         for( i = 0; i < cascade->count; i++ )
366         {
367             CvHaarStageClassifier* stage_classifier = cascade->stage_classifier + i;
368             for( j = 0, k = 0; j < stage_classifier->count; j++ )
369             {
370                 CvHaarClassifier* classifier = stage_classifier->classifier + j;
371                 int rect_count = 2 + (classifier->haar_feature->rect[2].r.width != 0);
372
373                 ipp_thresholds[j] = classifier->threshold[0];
374                 ipp_val1[j] = classifier->alpha[0];
375                 ipp_val2[j] = classifier->alpha[1];
376                 ipp_counts[j] = rect_count;
377
378                 for( l = 0; l < rect_count; l++, k++ )
379                 {
380                     ipp_features[k] = classifier->haar_feature->rect[l].r;
381                     //ipp_features[k].y = orig_window_size.height - ipp_features[k].y - ipp_features[k].height;
382                     ipp_weights[k] = classifier->haar_feature->rect[l].weight*ipp_weight_scale;
383                 }
384             }
385
386             if( icvHaarClassifierInitAlloc_32f_p( &out->ipp_stages[i],
387                 ipp_features, ipp_weights, ipp_thresholds,
388                 ipp_val1, ipp_val2, ipp_counts, stage_classifier->count ) < 0 )
389                 break;
390         }
391
392         if( i < cascade->count )
393         {
394             for( j = 0; j < i; j++ )
395                 if( icvHaarClassifierFree_32f_p && out->ipp_stages[i] )
396                     icvHaarClassifierFree_32f_p( out->ipp_stages[i] );
397             cvFree( &out->ipp_stages );
398         }
399     }
400     }
401 #endif
402
403     cascade->hid_cascade = out;
404     assert( (char*)haar_node_ptr - (char*)out <= datasize );
405
406     __END__;
407
408     if( cvGetErrStatus() < 0 )
409         icvReleaseHidHaarClassifierCascade( &out );
410
411     cvFree( &ipp_features );
412     cvFree( &ipp_weights );
413     cvFree( &ipp_thresholds );
414     cvFree( &ipp_val1 );
415     cvFree( &ipp_val2 );
416     cvFree( &ipp_counts );
417
418     return out;
419 }
420
421
422 #define sum_elem_ptr(sum,row,col)  \
423     ((sumtype*)CV_MAT_ELEM_PTR_FAST((sum),(row),(col),sizeof(sumtype)))
424
425 #define sqsum_elem_ptr(sqsum,row,col)  \
426     ((sqsumtype*)CV_MAT_ELEM_PTR_FAST((sqsum),(row),(col),sizeof(sqsumtype)))
427
428 #define calc_sum(rect,offset) \
429     ((rect).p0[offset] - (rect).p1[offset] - (rect).p2[offset] + (rect).p3[offset])
430
431
432 CV_IMPL void
433 cvSetImagesForHaarClassifierCascade( CvHaarClassifierCascade* _cascade,
434                                      const CvArr* _sum,
435                                      const CvArr* _sqsum,
436                                      const CvArr* _tilted_sum,
437                                      double scale )
438 {
439     CV_FUNCNAME("cvSetImagesForHaarClassifierCascade");
440
441     __BEGIN__;
442
443     CvMat sum_stub, *sum = (CvMat*)_sum;
444     CvMat sqsum_stub, *sqsum = (CvMat*)_sqsum;
445     CvMat tilted_stub, *tilted = (CvMat*)_tilted_sum;
446     CvHidHaarClassifierCascade* cascade;
447     int coi0 = 0, coi1 = 0;
448     int i;
449     CvRect equ_rect;
450     double weight_scale;
451
452     if( !CV_IS_HAAR_CLASSIFIER(_cascade) )
453         CV_ERROR( !_cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier pointer" );
454
455     if( scale <= 0 )
456         CV_ERROR( CV_StsOutOfRange, "Scale must be positive" );
457
458     CV_CALL( sum = cvGetMat( sum, &sum_stub, &coi0 ));
459     CV_CALL( sqsum = cvGetMat( sqsum, &sqsum_stub, &coi1 ));
460
461     if( coi0 || coi1 )
462         CV_ERROR( CV_BadCOI, "COI is not supported" );
463
464     if( !CV_ARE_SIZES_EQ( sum, sqsum ))
465         CV_ERROR( CV_StsUnmatchedSizes, "All integral images must have the same size" );
466
467     if( CV_MAT_TYPE(sqsum->type) != CV_64FC1 ||
468         CV_MAT_TYPE(sum->type) != CV_32SC1 )
469         CV_ERROR( CV_StsUnsupportedFormat,
470         "Only (32s, 64f, 32s) combination of (sum,sqsum,tilted_sum) formats is allowed" );
471
472     if( !_cascade->hid_cascade )
473         CV_CALL( icvCreateHidHaarClassifierCascade(_cascade) );
474
475     cascade = _cascade->hid_cascade;
476
477     if( cascade->has_tilted_features )
478     {
479         CV_CALL( tilted = cvGetMat( tilted, &tilted_stub, &coi1 ));
480
481         if( CV_MAT_TYPE(tilted->type) != CV_32SC1 )
482             CV_ERROR( CV_StsUnsupportedFormat,
483             "Only (32s, 64f, 32s) combination of (sum,sqsum,tilted_sum) formats is allowed" );
484
485         if( sum->step != tilted->step )
486             CV_ERROR( CV_StsUnmatchedSizes,
487             "Sum and tilted_sum must have the same stride (step, widthStep)" );
488
489         if( !CV_ARE_SIZES_EQ( sum, tilted ))
490             CV_ERROR( CV_StsUnmatchedSizes, "All integral images must have the same size" );
491         cascade->tilted = *tilted;
492     }
493
494     _cascade->scale = scale;
495     _cascade->real_window_size.width = cvRound( _cascade->orig_window_size.width * scale );
496     _cascade->real_window_size.height = cvRound( _cascade->orig_window_size.height * scale );
497
498     cascade->sum = *sum;
499     cascade->sqsum = *sqsum;
500
501     equ_rect.x = equ_rect.y = cvRound(scale);
502     equ_rect.width = cvRound((_cascade->orig_window_size.width-2)*scale);
503     equ_rect.height = cvRound((_cascade->orig_window_size.height-2)*scale);
504     weight_scale = 1./(equ_rect.width*equ_rect.height);
505     cascade->inv_window_area = weight_scale;
506
507     cascade->p0 = sum_elem_ptr(*sum, equ_rect.y, equ_rect.x);
508     cascade->p1 = sum_elem_ptr(*sum, equ_rect.y, equ_rect.x + equ_rect.width );
509     cascade->p2 = sum_elem_ptr(*sum, equ_rect.y + equ_rect.height, equ_rect.x );
510     cascade->p3 = sum_elem_ptr(*sum, equ_rect.y + equ_rect.height,
511                                      equ_rect.x + equ_rect.width );
512
513     cascade->pq0 = sqsum_elem_ptr(*sqsum, equ_rect.y, equ_rect.x);
514     cascade->pq1 = sqsum_elem_ptr(*sqsum, equ_rect.y, equ_rect.x + equ_rect.width );
515     cascade->pq2 = sqsum_elem_ptr(*sqsum, equ_rect.y + equ_rect.height, equ_rect.x );
516     cascade->pq3 = sqsum_elem_ptr(*sqsum, equ_rect.y + equ_rect.height,
517                                           equ_rect.x + equ_rect.width );
518
519     /* init pointers in haar features according to real window size and
520        given image pointers */
521     {
522 #ifdef _OPENMP
523     int max_threads = cvGetNumThreads();
524     #pragma omp parallel for num_threads(max_threads), schedule(dynamic)
525 #endif // _OPENMP
526     for( i = 0; i < _cascade->count; i++ )
527     {
528         int j, k, l;
529         for( j = 0; j < cascade->stage_classifier[i].count; j++ )
530         {
531             for( l = 0; l < cascade->stage_classifier[i].classifier[j].count; l++ )
532             {
533                 CvHaarFeature* feature =
534                     &_cascade->stage_classifier[i].classifier[j].haar_feature[l];
535                 /* CvHidHaarClassifier* classifier =
536                     cascade->stage_classifier[i].classifier + j; */
537                 CvHidHaarFeature* hidfeature =
538                     &cascade->stage_classifier[i].classifier[j].node[l].feature;
539                 double sum0 = 0, area0 = 0;
540                 CvRect r[3];
541 #if CV_ADJUST_FEATURES
542                 int base_w = -1, base_h = -1;
543                 int new_base_w = 0, new_base_h = 0;
544                 int kx, ky;
545                 int flagx = 0, flagy = 0;
546                 int x0 = 0, y0 = 0;
547 #endif
548                 int nr;
549
550                 /* align blocks */
551                 for( k = 0; k < CV_HAAR_FEATURE_MAX; k++ )
552                 {
553                     if( !hidfeature->rect[k].p0 )
554                         break;
555 #if CV_ADJUST_FEATURES
556                     r[k] = feature->rect[k].r;
557                     base_w = (int)CV_IMIN( (unsigned)base_w, (unsigned)(r[k].width-1) );
558                     base_w = (int)CV_IMIN( (unsigned)base_w, (unsigned)(r[k].x - r[0].x-1) );
559                     base_h = (int)CV_IMIN( (unsigned)base_h, (unsigned)(r[k].height-1) );
560                     base_h = (int)CV_IMIN( (unsigned)base_h, (unsigned)(r[k].y - r[0].y-1) );
561 #endif
562                 }
563
564                 nr = k;
565
566 #if CV_ADJUST_FEATURES
567                 base_w += 1;
568                 base_h += 1;
569                 kx = r[0].width / base_w;
570                 ky = r[0].height / base_h;
571
572                 if( kx <= 0 )
573                 {
574                     flagx = 1;
575                     new_base_w = cvRound( r[0].width * scale ) / kx;
576                     x0 = cvRound( r[0].x * scale );
577                 }
578
579                 if( ky <= 0 )
580                 {
581                     flagy = 1;
582                     new_base_h = cvRound( r[0].height * scale ) / ky;
583                     y0 = cvRound( r[0].y * scale );
584                 }
585 #endif
586
587                 for( k = 0; k < nr; k++ )
588                 {
589                     CvRect tr;
590                     double correction_ratio;
591
592 #if CV_ADJUST_FEATURES
593                     if( flagx )
594                     {
595                         tr.x = (r[k].x - r[0].x) * new_base_w / base_w + x0;
596                         tr.width = r[k].width * new_base_w / base_w;
597                     }
598                     else
599 #endif
600                     {
601                         tr.x = cvRound( r[k].x * scale );
602                         tr.width = cvRound( r[k].width * scale );
603                     }
604
605 #if CV_ADJUST_FEATURES
606                     if( flagy )
607                     {
608                         tr.y = (r[k].y - r[0].y) * new_base_h / base_h + y0;
609                         tr.height = r[k].height * new_base_h / base_h;
610                     }
611                     else
612 #endif
613                     {
614                         tr.y = cvRound( r[k].y * scale );
615                         tr.height = cvRound( r[k].height * scale );
616                     }
617
618 #if CV_ADJUST_WEIGHTS
619                     {
620                     // RAINER START
621                     const float orig_feature_size =  (float)(feature->rect[k].r.width)*feature->rect[k].r.height;
622                     const float orig_norm_size = (float)(_cascade->orig_window_size.width)*(_cascade->orig_window_size.height);
623                     const float feature_size = float(tr.width*tr.height);
624                     //const float normSize    = float(equ_rect.width*equ_rect.height);
625                     float target_ratio = orig_feature_size / orig_norm_size;
626                     //float isRatio = featureSize / normSize;
627                     //correctionRatio = targetRatio / isRatio / normSize;
628                     correction_ratio = target_ratio / feature_size;
629                     // RAINER END
630                     }
631 #else
632                     correction_ratio = weight_scale * (!feature->tilted ? 1 : 0.5);
633 #endif
634
635                     if( !feature->tilted )
636                     {
637                         hidfeature->rect[k].p0 = sum_elem_ptr(*sum, tr.y, tr.x);
638                         hidfeature->rect[k].p1 = sum_elem_ptr(*sum, tr.y, tr.x + tr.width);
639                         hidfeature->rect[k].p2 = sum_elem_ptr(*sum, tr.y + tr.height, tr.x);
640                         hidfeature->rect[k].p3 = sum_elem_ptr(*sum, tr.y + tr.height, tr.x + tr.width);
641                     }
642                     else
643                     {
644                         hidfeature->rect[k].p2 = sum_elem_ptr(*tilted, tr.y + tr.width, tr.x + tr.width);
645                         hidfeature->rect[k].p3 = sum_elem_ptr(*tilted, tr.y + tr.width + tr.height,
646                                                               tr.x + tr.width - tr.height);
647                         hidfeature->rect[k].p0 = sum_elem_ptr(*tilted, tr.y, tr.x);
648                         hidfeature->rect[k].p1 = sum_elem_ptr(*tilted, tr.y + tr.height, tr.x - tr.height);
649                     }
650
651                     hidfeature->rect[k].weight = (float)(feature->rect[k].weight * correction_ratio);
652
653                     if( k == 0 )
654                         area0 = tr.width * tr.height;
655                     else
656                         sum0 += hidfeature->rect[k].weight * tr.width * tr.height;
657                 }
658
659                 hidfeature->rect[0].weight = (float)(-sum0/area0);
660             } /* l */
661         } /* j */
662     }
663     }
664
665     __END__;
666 }
667
668
669 CV_INLINE
670 double icvEvalHidHaarClassifier( CvHidHaarClassifier* classifier,
671                                  double variance_norm_factor,
672                                  size_t p_offset )
673 {
674     int idx = 0;
675     do
676     {
677         CvHidHaarTreeNode* node = classifier->node + idx;
678         double t = node->threshold * variance_norm_factor;
679
680         double sum = calc_sum(node->feature.rect[0],p_offset) * node->feature.rect[0].weight;
681         sum += calc_sum(node->feature.rect[1],p_offset) * node->feature.rect[1].weight;
682
683         if( node->feature.rect[2].p0 )
684             sum += calc_sum(node->feature.rect[2],p_offset) * node->feature.rect[2].weight;
685
686         idx = sum < t ? node->left : node->right;
687     }
688     while( idx > 0 );
689     return classifier->alpha[-idx];
690 }
691
692
693 CV_IMPL int
694 cvRunHaarClassifierCascade( CvHaarClassifierCascade* _cascade,
695                             CvPoint pt, int start_stage )
696 {
697     int result = -1;
698     CV_FUNCNAME("cvRunHaarClassifierCascade");
699
700     __BEGIN__;
701
702     int p_offset, pq_offset;
703     int i, j;
704     double mean, variance_norm_factor;
705     CvHidHaarClassifierCascade* cascade;
706
707     if( !CV_IS_HAAR_CLASSIFIER(_cascade) )
708         CV_ERROR( !_cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid cascade pointer" );
709
710     cascade = _cascade->hid_cascade;
711     if( !cascade )
712         CV_ERROR( CV_StsNullPtr, "Hidden cascade has not been created.\n"
713             "Use cvSetImagesForHaarClassifierCascade" );
714
715     if( pt.x < 0 || pt.y < 0 ||
716         pt.x + _cascade->real_window_size.width >= cascade->sum.width-2 ||
717         pt.y + _cascade->real_window_size.height >= cascade->sum.height-2 )
718         EXIT;
719
720     p_offset = pt.y * (cascade->sum.step/sizeof(sumtype)) + pt.x;
721     pq_offset = pt.y * (cascade->sqsum.step/sizeof(sqsumtype)) + pt.x;
722     mean = calc_sum(*cascade,p_offset)*cascade->inv_window_area;
723     variance_norm_factor = cascade->pq0[pq_offset] - cascade->pq1[pq_offset] -
724                            cascade->pq2[pq_offset] + cascade->pq3[pq_offset];
725     variance_norm_factor = variance_norm_factor*cascade->inv_window_area - mean*mean;
726     if( variance_norm_factor >= 0. )
727         variance_norm_factor = sqrt(variance_norm_factor);
728     else
729         variance_norm_factor = 1.;
730
731     if( cascade->is_tree )
732     {
733         CvHidHaarStageClassifier* ptr;
734         assert( start_stage == 0 );
735
736         result = 1;
737         ptr = cascade->stage_classifier;
738
739         while( ptr )
740         {
741             double stage_sum = 0;
742
743             for( j = 0; j < ptr->count; j++ )
744             {
745                 stage_sum += icvEvalHidHaarClassifier( ptr->classifier + j,
746                     variance_norm_factor, p_offset );
747             }
748
749             if( stage_sum >= ptr->threshold )
750             {
751                 ptr = ptr->child;
752             }
753             else
754             {
755                 while( ptr && ptr->next == NULL ) ptr = ptr->parent;
756                 if( ptr == NULL )
757                 {
758                     result = 0;
759                     EXIT;
760                 }
761                 ptr = ptr->next;
762             }
763         }
764     }
765     else if( cascade->is_stump_based )
766     {
767         for( i = start_stage; i < cascade->count; i++ )
768         {
769             double stage_sum = 0;
770
771             if( cascade->stage_classifier[i].two_rects )
772             {
773                 for( j = 0; j < cascade->stage_classifier[i].count; j++ )
774                 {
775                     CvHidHaarClassifier* classifier = cascade->stage_classifier[i].classifier + j;
776                     CvHidHaarTreeNode* node = classifier->node;
777                     double sum, t = node->threshold*variance_norm_factor, a, b;
778
779                     sum = calc_sum(node->feature.rect[0],p_offset) * node->feature.rect[0].weight;
780                     sum += calc_sum(node->feature.rect[1],p_offset) * node->feature.rect[1].weight;
781
782                     a = classifier->alpha[0];
783                     b = classifier->alpha[1];
784                     stage_sum += sum < t ? a : b;
785                 }
786             }
787             else
788             {
789                 for( j = 0; j < cascade->stage_classifier[i].count; j++ )
790                 {
791                     CvHidHaarClassifier* classifier = cascade->stage_classifier[i].classifier + j;
792                     CvHidHaarTreeNode* node = classifier->node;
793                     double sum, t = node->threshold*variance_norm_factor, a, b;
794
795                     sum = calc_sum(node->feature.rect[0],p_offset) * node->feature.rect[0].weight;
796                     sum += calc_sum(node->feature.rect[1],p_offset) * node->feature.rect[1].weight;
797
798                     if( node->feature.rect[2].p0 )
799                         sum += calc_sum(node->feature.rect[2],p_offset) * node->feature.rect[2].weight;
800
801                     a = classifier->alpha[0];
802                     b = classifier->alpha[1];
803                     stage_sum += sum < t ? a : b;
804                 }
805             }
806
807             if( stage_sum < cascade->stage_classifier[i].threshold )
808             {
809                 result = -i;
810                 EXIT;
811             }
812         }
813     }
814     else
815     {
816         for( i = start_stage; i < cascade->count; i++ )
817         {
818             double stage_sum = 0;
819
820             for( j = 0; j < cascade->stage_classifier[i].count; j++ )
821             {
822                 stage_sum += icvEvalHidHaarClassifier(
823                     cascade->stage_classifier[i].classifier + j,
824                     variance_norm_factor, p_offset );
825             }
826
827             if( stage_sum < cascade->stage_classifier[i].threshold )
828             {
829                 result = -i;
830                 EXIT;
831             }
832         }
833     }
834
835     result = 1;
836
837     __END__;
838
839     return result;
840 }
841
842
843 static int is_equal( const void* _r1, const void* _r2, void* )
844 {
845     const CvRect* r1 = (const CvRect*)_r1;
846     const CvRect* r2 = (const CvRect*)_r2;
847     int distance = cvRound(r1->width*0.2);
848
849     return r2->x <= r1->x + distance &&
850            r2->x >= r1->x - distance &&
851            r2->y <= r1->y + distance &&
852            r2->y >= r1->y - distance &&
853            r2->width <= cvRound( r1->width * 1.2 ) &&
854            cvRound( r2->width * 1.2 ) >= r1->width;
855 }
856
857
858 #define VERY_ROUGH_SEARCH 0
859
860 CV_IMPL CvSeq*
861 cvHaarDetectObjects( const CvArr* _img,
862                      CvHaarClassifierCascade* cascade,
863                      CvMemStorage* storage, double scale_factor,
864                      int min_neighbors, int flags, CvSize min_size )
865 {
866     int split_stage = 2;
867
868     CvMat stub, *img = (CvMat*)_img;
869     CvMat *temp = 0, *sum = 0, *tilted = 0, *sqsum = 0, *norm_img = 0, *sumcanny = 0, *img_small = 0;
870     CvSeq* result_seq = 0;
871     CvMemStorage* temp_storage = 0;
872     CvAvgComp* comps = 0;
873     int i;
874
875 #ifdef _OPENMP
876     CvSeq* seq_thread[CV_MAX_THREADS] = {0};
877     int max_threads = 0;
878 #endif
879
880     CV_FUNCNAME( "cvHaarDetectObjects" );
881
882     __BEGIN__;
883
884     CvSeq *seq = 0, *seq2 = 0, *idx_seq = 0, *big_seq = 0;
885     CvAvgComp result_comp = {{0,0,0,0},0};
886     double factor;
887     int npass = 2, coi;
888     bool do_canny_pruning = (flags & CV_HAAR_DO_CANNY_PRUNING) != 0;
889     bool find_biggest_object = (flags & CV_HAAR_FIND_BIGGEST_OBJECT) != 0;
890     bool rough_search = (flags & CV_HAAR_DO_ROUGH_SEARCH) != 0;
891
892     if( !CV_IS_HAAR_CLASSIFIER(cascade) )
893         CV_ERROR( !cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier cascade" );
894
895     if( !storage )
896         CV_ERROR( CV_StsNullPtr, "Null storage pointer" );
897
898     CV_CALL( img = cvGetMat( img, &stub, &coi ));
899     if( coi )
900         CV_ERROR( CV_BadCOI, "COI is not supported" );
901
902     if( CV_MAT_DEPTH(img->type) != CV_8U )
903         CV_ERROR( CV_StsUnsupportedFormat, "Only 8-bit images are supported" );
904
905     if( find_biggest_object )
906         flags &= ~CV_HAAR_SCALE_IMAGE;
907
908     CV_CALL( temp = cvCreateMat( img->rows, img->cols, CV_8UC1 ));
909     CV_CALL( sum = cvCreateMat( img->rows + 1, img->cols + 1, CV_32SC1 ));
910     CV_CALL( sqsum = cvCreateMat( img->rows + 1, img->cols + 1, CV_64FC1 ));
911     CV_CALL( temp_storage = cvCreateChildMemStorage( storage ));
912
913 #ifdef _OPENMP
914     max_threads = cvGetNumThreads();
915     for( i = 0; i < max_threads; i++ )
916     {
917         CvMemStorage* temp_storage_thread;
918         CV_CALL( temp_storage_thread = cvCreateMemStorage(0));
919         CV_CALL( seq_thread[i] = cvCreateSeq( 0, sizeof(CvSeq),
920                         sizeof(CvRect), temp_storage_thread ));
921     }
922 #endif
923
924     if( !cascade->hid_cascade )
925         CV_CALL( icvCreateHidHaarClassifierCascade(cascade) );
926
927     if( cascade->hid_cascade->has_tilted_features )
928         tilted = cvCreateMat( img->rows + 1, img->cols + 1, CV_32SC1 );
929
930     seq = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvRect), temp_storage );
931     seq2 = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvAvgComp), temp_storage );
932     result_seq = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvAvgComp), storage );
933
934     if( CV_MAT_CN(img->type) > 1 )
935     {
936         cvCvtColor( img, temp, CV_BGR2GRAY );
937         img = temp;
938     }
939
940     if( flags & CV_HAAR_SCALE_IMAGE )
941     {
942         CvSize win_size0 = cascade->orig_window_size;
943         int use_ipp = cascade->hid_cascade->ipp_stages != 0 &&
944                     icvApplyHaarClassifier_32s32f_C1R_p != 0;
945
946         if( use_ipp )
947             CV_CALL( norm_img = cvCreateMat( img->rows, img->cols, CV_32FC1 ));
948         CV_CALL( img_small = cvCreateMat( img->rows + 1, img->cols + 1, CV_8UC1 ));
949
950         for( factor = 1; ; factor *= scale_factor )
951         {
952             int positive = 0;
953             int x, y;
954             CvSize win_size = { cvRound(win_size0.width*factor),
955                                 cvRound(win_size0.height*factor) };
956             CvSize sz = { cvRound( img->cols/factor ), cvRound( img->rows/factor ) };
957             CvSize sz1 = { sz.width - win_size0.width, sz.height - win_size0.height };
958             CvRect rect1 = { icv_object_win_border, icv_object_win_border,
959                 win_size0.width - icv_object_win_border*2,
960                 win_size0.height - icv_object_win_border*2 };
961             CvMat img1, sum1, sqsum1, norm1, tilted1, mask1;
962             CvMat* _tilted = 0;
963
964             if( sz1.width <= 0 || sz1.height <= 0 )
965                 break;
966             if( win_size.width < min_size.width || win_size.height < min_size.height )
967                 continue;
968
969             img1 = cvMat( sz.height, sz.width, CV_8UC1, img_small->data.ptr );
970             sum1 = cvMat( sz.height+1, sz.width+1, CV_32SC1, sum->data.ptr );
971             sqsum1 = cvMat( sz.height+1, sz.width+1, CV_64FC1, sqsum->data.ptr );
972             if( tilted )
973             {
974                 tilted1 = cvMat( sz.height+1, sz.width+1, CV_32SC1, tilted->data.ptr );
975                 _tilted = &tilted1;
976             }
977             norm1 = cvMat( sz1.height, sz1.width, CV_32FC1, norm_img ? norm_img->data.ptr : 0 );
978             mask1 = cvMat( sz1.height, sz1.width, CV_8UC1, temp->data.ptr );
979
980             cvResize( img, &img1, CV_INTER_LINEAR );
981             cvIntegral( &img1, &sum1, &sqsum1, _tilted );
982
983             if( use_ipp && icvRectStdDev_32s32f_C1R_p( sum1.data.i, sum1.step,
984                 sqsum1.data.db, sqsum1.step, norm1.data.fl, norm1.step, sz1, rect1 ) < 0 )
985                 use_ipp = 0;
986
987             if( use_ipp )
988             {
989                 positive = mask1.cols*mask1.rows;
990                 cvSet( &mask1, cvScalarAll(255) );
991                 for( i = 0; i < cascade->count; i++ )
992                 {
993                     if( icvApplyHaarClassifier_32s32f_C1R_p(sum1.data.i, sum1.step,
994                         norm1.data.fl, norm1.step, mask1.data.ptr, mask1.step,
995                         sz1, &positive, cascade->hid_cascade->stage_classifier[i].threshold,
996                         cascade->hid_cascade->ipp_stages[i]) < 0 )
997                     {
998                         use_ipp = 0;
999                         break;
1000                     }
1001                     if( positive <= 0 )
1002                         break;
1003                 }
1004             }
1005
1006             if( !use_ipp )
1007             {
1008                 cvSetImagesForHaarClassifierCascade( cascade, &sum1, &sqsum1, 0, 1. );
1009                 for( y = 0, positive = 0; y < sz1.height; y++ )
1010                     for( x = 0; x < sz1.width; x++ )
1011                     {
1012                         mask1.data.ptr[mask1.step*y + x] =
1013                             cvRunHaarClassifierCascade( cascade, cvPoint(x,y), 0 ) > 0;
1014                         positive += mask1.data.ptr[mask1.step*y + x];
1015                     }
1016             }
1017
1018             if( positive > 0 )
1019             {
1020                 for( y = 0; y < sz1.height; y++ )
1021                     for( x = 0; x < sz1.width; x++ )
1022                         if( mask1.data.ptr[mask1.step*y + x] != 0 )
1023                         {
1024                             CvRect obj_rect = { cvRound(x*factor), cvRound(y*factor),
1025                                                 win_size.width, win_size.height };
1026                             cvSeqPush( seq, &obj_rect );
1027                         }
1028             }
1029         }
1030     }
1031     else
1032     {
1033         int n_factors = 0;
1034         CvRect scan_roi_rect = {0,0,0,0};
1035         bool is_found = false, scan_roi = false;
1036
1037         cvIntegral( img, sum, sqsum, tilted );
1038
1039         if( do_canny_pruning )
1040         {
1041             sumcanny = cvCreateMat( img->rows + 1, img->cols + 1, CV_32SC1 );
1042             cvCanny( img, temp, 0, 50, 3 );
1043             cvIntegral( temp, sumcanny );
1044         }
1045
1046         if( (unsigned)split_stage >= (unsigned)cascade->count ||
1047             cascade->hid_cascade->is_tree )
1048         {
1049             split_stage = cascade->count;
1050             npass = 1;
1051         }
1052
1053         for( n_factors = 0, factor = 1;
1054              factor*cascade->orig_window_size.width < img->cols - 10 &&
1055              factor*cascade->orig_window_size.height < img->rows - 10;
1056              n_factors++, factor *= scale_factor )
1057             ;
1058
1059         if( find_biggest_object )
1060         {
1061             scale_factor = 1./scale_factor;
1062             factor *= scale_factor;
1063             big_seq = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvRect), temp_storage );
1064         }
1065         else
1066             factor = 1;
1067
1068         for( ; n_factors-- > 0 && !is_found; factor *= scale_factor )
1069         {
1070             const double ystep = MAX( 2, factor );
1071             CvSize win_size = { cvRound( cascade->orig_window_size.width * factor ),
1072                                 cvRound( cascade->orig_window_size.height * factor )};
1073             CvRect equ_rect = { 0, 0, 0, 0 };
1074             int *p0 = 0, *p1 = 0, *p2 = 0, *p3 = 0;
1075             int *pq0 = 0, *pq1 = 0, *pq2 = 0, *pq3 = 0;
1076             int pass, stage_offset = 0;
1077             int start_x = 0, start_y = 0;
1078             int end_x = cvRound((img->cols - win_size.width) / ystep);
1079             int end_y = cvRound((img->rows - win_size.height) / ystep);
1080
1081             if( win_size.width < min_size.width || win_size.height < min_size.height )
1082             {
1083                 if( find_biggest_object )
1084                     break;
1085                 continue;
1086             }
1087
1088             cvSetImagesForHaarClassifierCascade( cascade, sum, sqsum, tilted, factor );
1089             cvZero( temp );
1090
1091             if( do_canny_pruning )
1092             {
1093                 equ_rect.x = cvRound(win_size.width*0.15);
1094                 equ_rect.y = cvRound(win_size.height*0.15);
1095                 equ_rect.width = cvRound(win_size.width*0.7);
1096                 equ_rect.height = cvRound(win_size.height*0.7);
1097
1098                 p0 = (int*)(sumcanny->data.ptr + equ_rect.y*sumcanny->step) + equ_rect.x;
1099                 p1 = (int*)(sumcanny->data.ptr + equ_rect.y*sumcanny->step)
1100                             + equ_rect.x + equ_rect.width;
1101                 p2 = (int*)(sumcanny->data.ptr + (equ_rect.y + equ_rect.height)*sumcanny->step) + equ_rect.x;
1102                 p3 = (int*)(sumcanny->data.ptr + (equ_rect.y + equ_rect.height)*sumcanny->step)
1103                             + equ_rect.x + equ_rect.width;
1104
1105                 pq0 = (int*)(sum->data.ptr + equ_rect.y*sum->step) + equ_rect.x;
1106                 pq1 = (int*)(sum->data.ptr + equ_rect.y*sum->step)
1107                             + equ_rect.x + equ_rect.width;
1108                 pq2 = (int*)(sum->data.ptr + (equ_rect.y + equ_rect.height)*sum->step) + equ_rect.x;
1109                 pq3 = (int*)(sum->data.ptr + (equ_rect.y + equ_rect.height)*sum->step)
1110                             + equ_rect.x + equ_rect.width;
1111             }
1112
1113             if( scan_roi )
1114             {
1115                 //adjust start_height and stop_height
1116                 start_y = cvRound(scan_roi_rect.y / ystep);
1117                 end_y = cvRound((scan_roi_rect.y + scan_roi_rect.height - win_size.height) / ystep);
1118
1119                 start_x = cvRound(scan_roi_rect.x / ystep);
1120                 end_x = cvRound((scan_roi_rect.x + scan_roi_rect.width - win_size.width) / ystep);
1121             }
1122
1123             cascade->hid_cascade->count = split_stage;
1124
1125             for( pass = 0; pass < npass; pass++ )
1126             {
1127 #ifdef _OPENMP
1128     #pragma omp parallel for num_threads(max_threads), schedule(dynamic)
1129 #endif
1130                 for( int _iy = start_y; _iy < end_y; _iy++ )
1131                 {
1132                     int iy = cvRound(_iy*ystep);
1133                     int _ix, _xstep = 1;
1134                     uchar* mask_row = temp->data.ptr + temp->step * iy;
1135
1136                     for( _ix = start_x; _ix < end_x; _ix += _xstep )
1137                     {
1138                         int ix = cvRound(_ix*ystep); // it really should be ystep
1139
1140                         if( pass == 0 )
1141                         {
1142                             int result;
1143                             _xstep = 2;
1144
1145                             if( do_canny_pruning )
1146                             {
1147                                 int offset;
1148                                 int s, sq;
1149
1150                                 offset = iy*(sum->step/sizeof(p0[0])) + ix;
1151                                 s = p0[offset] - p1[offset] - p2[offset] + p3[offset];
1152                                 sq = pq0[offset] - pq1[offset] - pq2[offset] + pq3[offset];
1153                                 if( s < 100 || sq < 20 )
1154                                     continue;
1155                             }
1156
1157                             result = cvRunHaarClassifierCascade( cascade, cvPoint(ix,iy), 0 );
1158                             if( result > 0 )
1159                             {
1160                                 if( pass < npass - 1 )
1161                                     mask_row[ix] = 1;
1162                                 else
1163                                 {
1164                                     CvRect rect = cvRect(ix,iy,win_size.width,win_size.height);
1165 #ifndef _OPENMP
1166                                     cvSeqPush( seq, &rect );
1167 #else
1168                                     cvSeqPush( seq_thread[omp_get_thread_num()], &rect );
1169 #endif
1170                                 }
1171                             }
1172                             if( result < 0 )
1173                                 _xstep = 1;
1174                         }
1175                         else if( mask_row[ix] )
1176                         {
1177                             int result = cvRunHaarClassifierCascade( cascade, cvPoint(ix,iy),
1178                                                                      stage_offset );
1179                             if( result > 0 )
1180                             {
1181                                 if( pass == npass - 1 )
1182                                 {
1183                                     CvRect rect = cvRect(ix,iy,win_size.width,win_size.height);
1184 #ifndef _OPENMP
1185                                     cvSeqPush( seq, &rect );
1186 #else
1187                                     cvSeqPush( seq_thread[omp_get_thread_num()], &rect );
1188 #endif
1189                                 }
1190                             }
1191                             else
1192                                 mask_row[ix] = 0;
1193                         }
1194                     }
1195                 }
1196                 stage_offset = cascade->hid_cascade->count;
1197                 cascade->hid_cascade->count = cascade->count;
1198             }
1199
1200 #ifdef _OPENMP
1201                 // gather the results
1202                 for( i = 0; i < max_threads; i++ )
1203                 {
1204                         CvSeq* s = seq_thread[i];
1205                 int j, total = s->total;
1206                 CvSeqBlock* b = s->first;
1207                 for( j = 0; j < total; j += b->count, b = b->next )
1208                     cvSeqPushMulti( seq, b->data, b->count );
1209                 }
1210 #endif
1211
1212             if( find_biggest_object )
1213             {
1214                 CvSeq* bseq = min_neighbors > 0 ? big_seq : seq;
1215                 
1216                 if( min_neighbors > 0 && !scan_roi )
1217                 {
1218                     // group retrieved rectangles in order to filter out noise
1219                     int ncomp = cvSeqPartition( seq, 0, &idx_seq, is_equal, 0 );
1220                     CV_CALL( comps = (CvAvgComp*)cvAlloc( (ncomp+1)*sizeof(comps[0])));
1221                     memset( comps, 0, (ncomp+1)*sizeof(comps[0]));
1222
1223                 #if VERY_ROUGH_SEARCH
1224                     if( rough_search )
1225                     {
1226                         for( i = 0; i < seq->total; i++ )
1227                         {
1228                             CvRect r1 = *(CvRect*)cvGetSeqElem( seq, i );
1229                             int idx = *(int*)cvGetSeqElem( idx_seq, i );
1230                             assert( (unsigned)idx < (unsigned)ncomp );
1231
1232                             comps[idx].neighbors++;
1233                             comps[idx].rect.x += r1.x;
1234                             comps[idx].rect.y += r1.y;
1235                             comps[idx].rect.width += r1.width;
1236                             comps[idx].rect.height += r1.height;
1237                         }
1238
1239                         // calculate average bounding box
1240                         for( i = 0; i < ncomp; i++ )
1241                         {
1242                             int n = comps[i].neighbors;
1243                             if( n >= min_neighbors )
1244                             {
1245                                 CvAvgComp comp;
1246                                 comp.rect.x = (comps[i].rect.x*2 + n)/(2*n);
1247                                 comp.rect.y = (comps[i].rect.y*2 + n)/(2*n);
1248                                 comp.rect.width = (comps[i].rect.width*2 + n)/(2*n);
1249                                 comp.rect.height = (comps[i].rect.height*2 + n)/(2*n);
1250                                 comp.neighbors = n;
1251                                 cvSeqPush( bseq, &comp );
1252                             }
1253                         }
1254                     }
1255                     else
1256                 #endif
1257                     {
1258                         for( i = 0 ; i <= ncomp; i++ )
1259                             comps[i].rect.x = comps[i].rect.y = INT_MAX;
1260
1261                         // count number of neighbors
1262                         for( i = 0; i < seq->total; i++ )
1263                         {
1264                             CvRect r1 = *(CvRect*)cvGetSeqElem( seq, i );
1265                             int idx = *(int*)cvGetSeqElem( idx_seq, i );
1266                             assert( (unsigned)idx < (unsigned)ncomp );
1267
1268                             comps[idx].neighbors++;
1269
1270                             // rect.width and rect.height will store coordinate of right-bottom corner
1271                             comps[idx].rect.x = MIN(comps[idx].rect.x, r1.x);
1272                             comps[idx].rect.y = MIN(comps[idx].rect.y, r1.y);
1273                             comps[idx].rect.width = MAX(comps[idx].rect.width, r1.x+r1.width-1);
1274                             comps[idx].rect.height = MAX(comps[idx].rect.height, r1.y+r1.height-1);
1275                         }
1276
1277                         // calculate enclosing box
1278                         for( i = 0; i < ncomp; i++ )
1279                         {
1280                             int n = comps[i].neighbors;
1281                             if( n >= min_neighbors )
1282                             {
1283                                 CvAvgComp comp;
1284                                 int t;
1285                                 double min_scale = rough_search ? 0.6 : 0.4;
1286                                 comp.rect.x = comps[i].rect.x;
1287                                 comp.rect.y = comps[i].rect.y;
1288                                 comp.rect.width = comps[i].rect.width - comps[i].rect.x + 1;
1289                                 comp.rect.height = comps[i].rect.height - comps[i].rect.y + 1;
1290
1291                                 // update min_size
1292                                 t = cvRound( comp.rect.width*min_scale );
1293                                 min_size.width = MAX( min_size.width, t );
1294
1295                                 t = cvRound( comp.rect.height*min_scale );
1296                                 min_size.height = MAX( min_size.height, t );
1297
1298                                 //expand the box by 20% because we could miss some neighbours
1299                                 //see 'is_equal' function
1300                             #if 1
1301                                 int offset = cvRound(comp.rect.width * 0.2);
1302                                 int right = MIN( img->cols-1, comp.rect.x+comp.rect.width-1 + offset );
1303                                 int bottom = MIN( img->rows-1, comp.rect.y+comp.rect.height-1 + offset);
1304                                 comp.rect.x = MAX( comp.rect.x - offset, 0 );
1305                                 comp.rect.y = MAX( comp.rect.y - offset, 0 );
1306                                 comp.rect.width = right - comp.rect.x + 1;
1307                                 comp.rect.height = bottom - comp.rect.y + 1;
1308                             #endif
1309
1310                                 comp.neighbors = n;
1311                                 cvSeqPush( bseq, &comp );
1312                             }
1313                         }
1314                     }
1315
1316                     cvFree( &comps );
1317                 }
1318
1319                 // extract the biggest rect
1320                 if( bseq->total > 0 )
1321                 {
1322                     int max_area = 0;
1323                     for( i = 0; i < bseq->total; i++ )
1324                     {
1325                         CvAvgComp* comp = (CvAvgComp*)cvGetSeqElem( bseq, i );
1326                         int area = comp->rect.width * comp->rect.height;
1327                         if( max_area < area )
1328                         {
1329                             max_area = area;
1330                             result_comp.rect = comp->rect;
1331                             result_comp.neighbors = bseq == seq ? 1 : comp->neighbors;
1332                         }
1333                     }
1334
1335                     //Prepare information for further scanning inside the biggest rectangle
1336
1337                 #if VERY_ROUGH_SEARCH
1338                     // change scan ranges to roi in case of required
1339                     if( !rough_search && !scan_roi )
1340                     {
1341                         scan_roi = true;
1342                         scan_roi_rect = result_comp.rect;
1343                         cvClearSeq(bseq);
1344                     }
1345                     else if( rough_search )
1346                         is_found = true;
1347                 #else
1348                     if( !scan_roi )
1349                     {
1350                         scan_roi = true;
1351                         scan_roi_rect = result_comp.rect;
1352                         cvClearSeq(bseq);
1353                     }
1354                 #endif
1355                 }
1356             }
1357         }
1358     }
1359
1360     if( min_neighbors == 0 && !find_biggest_object )
1361     {
1362         for( i = 0; i < seq->total; i++ )
1363         {
1364             CvRect* rect = (CvRect*)cvGetSeqElem( seq, i );
1365             CvAvgComp comp;
1366             comp.rect = *rect;
1367             comp.neighbors = 1;
1368             cvSeqPush( result_seq, &comp );
1369         }
1370     }
1371
1372     if( min_neighbors != 0
1373 #if VERY_ROUGH_SEARCH        
1374         && (!find_biggest_object || !rough_search)
1375 #endif        
1376         )
1377     {
1378         // group retrieved rectangles in order to filter out noise
1379         int ncomp = cvSeqPartition( seq, 0, &idx_seq, is_equal, 0 );
1380         CV_CALL( comps = (CvAvgComp*)cvAlloc( (ncomp+1)*sizeof(comps[0])));
1381         memset( comps, 0, (ncomp+1)*sizeof(comps[0]));
1382
1383         // count number of neighbors
1384         for( i = 0; i < seq->total; i++ )
1385         {
1386             CvRect r1 = *(CvRect*)cvGetSeqElem( seq, i );
1387             int idx = *(int*)cvGetSeqElem( idx_seq, i );
1388             assert( (unsigned)idx < (unsigned)ncomp );
1389
1390             comps[idx].neighbors++;
1391
1392             comps[idx].rect.x += r1.x;
1393             comps[idx].rect.y += r1.y;
1394             comps[idx].rect.width += r1.width;
1395             comps[idx].rect.height += r1.height;
1396         }
1397
1398         // calculate average bounding box
1399         for( i = 0; i < ncomp; i++ )
1400         {
1401             int n = comps[i].neighbors;
1402             if( n >= min_neighbors )
1403             {
1404                 CvAvgComp comp;
1405                 comp.rect.x = (comps[i].rect.x*2 + n)/(2*n);
1406                 comp.rect.y = (comps[i].rect.y*2 + n)/(2*n);
1407                 comp.rect.width = (comps[i].rect.width*2 + n)/(2*n);
1408                 comp.rect.height = (comps[i].rect.height*2 + n)/(2*n);
1409                 comp.neighbors = comps[i].neighbors;
1410
1411                 cvSeqPush( seq2, &comp );
1412             }
1413         }
1414
1415         if( !find_biggest_object )
1416         {
1417             // filter out small face rectangles inside large face rectangles
1418             for( i = 0; i < seq2->total; i++ )
1419             {
1420                 CvAvgComp r1 = *(CvAvgComp*)cvGetSeqElem( seq2, i );
1421                 int j, flag = 1;
1422
1423                 for( j = 0; j < seq2->total; j++ )
1424                 {
1425                     CvAvgComp r2 = *(CvAvgComp*)cvGetSeqElem( seq2, j );
1426                     int distance = cvRound( r2.rect.width * 0.2 );
1427
1428                     if( i != j &&
1429                         r1.rect.x >= r2.rect.x - distance &&
1430                         r1.rect.y >= r2.rect.y - distance &&
1431                         r1.rect.x + r1.rect.width <= r2.rect.x + r2.rect.width + distance &&
1432                         r1.rect.y + r1.rect.height <= r2.rect.y + r2.rect.height + distance &&
1433                         (r2.neighbors > MAX( 3, r1.neighbors ) || r1.neighbors < 3) )
1434                     {
1435                         flag = 0;
1436                         break;
1437                     }
1438                 }
1439
1440                 if( flag )
1441                     cvSeqPush( result_seq, &r1 );
1442             }
1443         }
1444         else
1445         {
1446             int max_area = 0;
1447             for( i = 0; i < seq2->total; i++ )
1448             {
1449                 CvAvgComp* comp = (CvAvgComp*)cvGetSeqElem( seq2, i );
1450                 int area = comp->rect.width * comp->rect.height;
1451                 if( max_area < area )
1452                 {
1453                     max_area = area;
1454                     result_comp = *comp;
1455                 }                
1456             }
1457         }
1458     }
1459
1460     if( find_biggest_object && result_comp.rect.width > 0 )
1461         cvSeqPush( result_seq, &result_comp );
1462
1463     __END__;
1464
1465 #ifdef _OPENMP
1466         for( i = 0; i < max_threads; i++ )
1467         {
1468                 if( seq_thread[i] )
1469             cvReleaseMemStorage( &seq_thread[i]->storage );
1470         }
1471 #endif
1472
1473     cvReleaseMemStorage( &temp_storage );
1474     cvReleaseMat( &sum );
1475     cvReleaseMat( &sqsum );
1476     cvReleaseMat( &tilted );
1477     cvReleaseMat( &temp );
1478     cvReleaseMat( &sumcanny );
1479     cvReleaseMat( &norm_img );
1480     cvReleaseMat( &img_small );
1481     cvFree( &comps );
1482
1483     return result_seq;
1484 }
1485
1486
1487 static CvHaarClassifierCascade*
1488 icvLoadCascadeCART( const char** input_cascade, int n, CvSize orig_window_size )
1489 {
1490     int i;
1491     CvHaarClassifierCascade* cascade = icvCreateHaarClassifierCascade(n);
1492     cascade->orig_window_size = orig_window_size;
1493
1494     for( i = 0; i < n; i++ )
1495     {
1496         int j, count, l;
1497         float threshold = 0;
1498         const char* stage = input_cascade[i];
1499         int dl = 0;
1500
1501         /* tree links */
1502         int parent = -1;
1503         int next = -1;
1504
1505         sscanf( stage, "%d%n", &count, &dl );
1506         stage += dl;
1507
1508         assert( count > 0 );
1509         cascade->stage_classifier[i].count = count;
1510         cascade->stage_classifier[i].classifier =
1511             (CvHaarClassifier*)cvAlloc( count*sizeof(cascade->stage_classifier[i].classifier[0]));
1512
1513         for( j = 0; j < count; j++ )
1514         {
1515             CvHaarClassifier* classifier = cascade->stage_classifier[i].classifier + j;
1516             int k, rects = 0;
1517             char str[100];
1518
1519             sscanf( stage, "%d%n", &classifier->count, &dl );
1520             stage += dl;
1521
1522             classifier->haar_feature = (CvHaarFeature*) cvAlloc(
1523                 classifier->count * ( sizeof( *classifier->haar_feature ) +
1524                                       sizeof( *classifier->threshold ) +
1525                                       sizeof( *classifier->left ) +
1526                                       sizeof( *classifier->right ) ) +
1527                 (classifier->count + 1) * sizeof( *classifier->alpha ) );
1528             classifier->threshold = (float*) (classifier->haar_feature+classifier->count);
1529             classifier->left = (int*) (classifier->threshold + classifier->count);
1530             classifier->right = (int*) (classifier->left + classifier->count);
1531             classifier->alpha = (float*) (classifier->right + classifier->count);
1532
1533             for( l = 0; l < classifier->count; l++ )
1534             {
1535                 sscanf( stage, "%d%n", &rects, &dl );
1536                 stage += dl;
1537
1538                 assert( rects >= 2 && rects <= CV_HAAR_FEATURE_MAX );
1539
1540                 for( k = 0; k < rects; k++ )
1541                 {
1542                     CvRect r;
1543                     int band = 0;
1544                     sscanf( stage, "%d%d%d%d%d%f%n",
1545                             &r.x, &r.y, &r.width, &r.height, &band,
1546                             &(classifier->haar_feature[l].rect[k].weight), &dl );
1547                     stage += dl;
1548                     classifier->haar_feature[l].rect[k].r = r;
1549                 }
1550                 sscanf( stage, "%s%n", str, &dl );
1551                 stage += dl;
1552
1553                 classifier->haar_feature[l].tilted = strncmp( str, "tilted", 6 ) == 0;
1554
1555                 for( k = rects; k < CV_HAAR_FEATURE_MAX; k++ )
1556                 {
1557                     memset( classifier->haar_feature[l].rect + k, 0,
1558                             sizeof(classifier->haar_feature[l].rect[k]) );
1559                 }
1560
1561                 sscanf( stage, "%f%d%d%n", &(classifier->threshold[l]),
1562                                        &(classifier->left[l]),
1563                                        &(classifier->right[l]), &dl );
1564                 stage += dl;
1565             }
1566             for( l = 0; l <= classifier->count; l++ )
1567             {
1568                 sscanf( stage, "%f%n", &(classifier->alpha[l]), &dl );
1569                 stage += dl;
1570             }
1571         }
1572
1573         sscanf( stage, "%f%n", &threshold, &dl );
1574         stage += dl;
1575
1576         cascade->stage_classifier[i].threshold = threshold;
1577
1578         /* load tree links */
1579         if( sscanf( stage, "%d%d%n", &parent, &next, &dl ) != 2 )
1580         {
1581             parent = i - 1;
1582             next = -1;
1583         }
1584         stage += dl;
1585
1586         cascade->stage_classifier[i].parent = parent;
1587         cascade->stage_classifier[i].next = next;
1588         cascade->stage_classifier[i].child = -1;
1589
1590         if( parent != -1 && cascade->stage_classifier[parent].child == -1 )
1591         {
1592             cascade->stage_classifier[parent].child = i;
1593         }
1594     }
1595
1596     return cascade;
1597 }
1598
1599 #ifndef _MAX_PATH
1600 #define _MAX_PATH 1024
1601 #endif
1602
1603 CV_IMPL CvHaarClassifierCascade*
1604 cvLoadHaarClassifierCascade( const char* directory, CvSize orig_window_size )
1605 {
1606     const char** input_cascade = 0;
1607     CvHaarClassifierCascade *cascade = 0;
1608
1609     CV_FUNCNAME( "cvLoadHaarClassifierCascade" );
1610
1611     __BEGIN__;
1612
1613     int i, n;
1614     const char* slash;
1615     char name[_MAX_PATH];
1616     int size = 0;
1617     char* ptr = 0;
1618
1619     if( !directory )
1620         CV_ERROR( CV_StsNullPtr, "Null path is passed" );
1621
1622     n = (int)strlen(directory)-1;
1623     slash = directory[n] == '\\' || directory[n] == '/' ? "" : "/";
1624
1625     /* try to read the classifier from directory */
1626     for( n = 0; ; n++ )
1627     {
1628         sprintf( name, "%s%s%d/AdaBoostCARTHaarClassifier.txt", directory, slash, n );
1629         FILE* f = fopen( name, "rb" );
1630         if( !f )
1631             break;
1632         fseek( f, 0, SEEK_END );
1633         size += ftell( f ) + 1;
1634         fclose(f);
1635     }
1636
1637     if( n == 0 && slash[0] )
1638     {
1639         CV_CALL( cascade = (CvHaarClassifierCascade*)cvLoad( directory ));
1640         EXIT;
1641     }
1642     else if( n == 0 )
1643         CV_ERROR( CV_StsBadArg, "Invalid path" );
1644
1645     size += (n+1)*sizeof(char*);
1646     CV_CALL( input_cascade = (const char**)cvAlloc( size ));
1647     ptr = (char*)(input_cascade + n + 1);
1648
1649     for( i = 0; i < n; i++ )
1650     {
1651         sprintf( name, "%s/%d/AdaBoostCARTHaarClassifier.txt", directory, i );
1652         FILE* f = fopen( name, "rb" );
1653         if( !f )
1654             CV_ERROR( CV_StsError, "" );
1655         fseek( f, 0, SEEK_END );
1656         size = ftell( f );
1657         fseek( f, 0, SEEK_SET );
1658         fread( ptr, 1, size, f );
1659         fclose(f);
1660         input_cascade[i] = ptr;
1661         ptr += size;
1662         *ptr++ = '\0';
1663     }
1664
1665     input_cascade[n] = 0;
1666     cascade = icvLoadCascadeCART( input_cascade, n, orig_window_size );
1667
1668     __END__;
1669
1670     if( input_cascade )
1671         cvFree( &input_cascade );
1672
1673     if( cvGetErrStatus() < 0 )
1674         cvReleaseHaarClassifierCascade( &cascade );
1675
1676     return cascade;
1677 }
1678
1679
1680 CV_IMPL void
1681 cvReleaseHaarClassifierCascade( CvHaarClassifierCascade** _cascade )
1682 {
1683     if( _cascade && *_cascade )
1684     {
1685         int i, j;
1686         CvHaarClassifierCascade* cascade = *_cascade;
1687
1688         for( i = 0; i < cascade->count; i++ )
1689         {
1690             for( j = 0; j < cascade->stage_classifier[i].count; j++ )
1691                 cvFree( &cascade->stage_classifier[i].classifier[j].haar_feature );
1692             cvFree( &cascade->stage_classifier[i].classifier );
1693         }
1694         icvReleaseHidHaarClassifierCascade( &cascade->hid_cascade );
1695         cvFree( _cascade );
1696     }
1697 }
1698
1699
1700 /****************************************************************************************\
1701 *                                  Persistence functions                                 *
1702 \****************************************************************************************/
1703
1704 /* field names */
1705
1706 #define ICV_HAAR_SIZE_NAME            "size"
1707 #define ICV_HAAR_STAGES_NAME          "stages"
1708 #define ICV_HAAR_TREES_NAME             "trees"
1709 #define ICV_HAAR_FEATURE_NAME             "feature"
1710 #define ICV_HAAR_RECTS_NAME                 "rects"
1711 #define ICV_HAAR_TILTED_NAME                "tilted"
1712 #define ICV_HAAR_THRESHOLD_NAME           "threshold"
1713 #define ICV_HAAR_LEFT_NODE_NAME           "left_node"
1714 #define ICV_HAAR_LEFT_VAL_NAME            "left_val"
1715 #define ICV_HAAR_RIGHT_NODE_NAME          "right_node"
1716 #define ICV_HAAR_RIGHT_VAL_NAME           "right_val"
1717 #define ICV_HAAR_STAGE_THRESHOLD_NAME   "stage_threshold"
1718 #define ICV_HAAR_PARENT_NAME            "parent"
1719 #define ICV_HAAR_NEXT_NAME              "next"
1720
1721 static int
1722 icvIsHaarClassifier( const void* struct_ptr )
1723 {
1724     return CV_IS_HAAR_CLASSIFIER( struct_ptr );
1725 }
1726
1727 static void*
1728 icvReadHaarClassifier( CvFileStorage* fs, CvFileNode* node )
1729 {
1730     CvHaarClassifierCascade* cascade = NULL;
1731
1732     CV_FUNCNAME( "cvReadHaarClassifier" );
1733
1734     __BEGIN__;
1735
1736     char buf[256];
1737     CvFileNode* seq_fn = NULL; /* sequence */
1738     CvFileNode* fn = NULL;
1739     CvFileNode* stages_fn = NULL;
1740     CvSeqReader stages_reader;
1741     int n;
1742     int i, j, k, l;
1743     int parent, next;
1744
1745     CV_CALL( stages_fn = cvGetFileNodeByName( fs, node, ICV_HAAR_STAGES_NAME ) );
1746     if( !stages_fn || !CV_NODE_IS_SEQ( stages_fn->tag) )
1747         CV_ERROR( CV_StsError, "Invalid stages node" );
1748
1749     n = stages_fn->data.seq->total;
1750     CV_CALL( cascade = icvCreateHaarClassifierCascade(n) );
1751
1752     /* read size */
1753     CV_CALL( seq_fn = cvGetFileNodeByName( fs, node, ICV_HAAR_SIZE_NAME ) );
1754     if( !seq_fn || !CV_NODE_IS_SEQ( seq_fn->tag ) || seq_fn->data.seq->total != 2 )
1755         CV_ERROR( CV_StsError, "size node is not a valid sequence." );
1756     CV_CALL( fn = (CvFileNode*) cvGetSeqElem( seq_fn->data.seq, 0 ) );
1757     if( !CV_NODE_IS_INT( fn->tag ) || fn->data.i <= 0 )
1758         CV_ERROR( CV_StsError, "Invalid size node: width must be positive integer" );
1759     cascade->orig_window_size.width = fn->data.i;
1760     CV_CALL( fn = (CvFileNode*) cvGetSeqElem( seq_fn->data.seq, 1 ) );
1761     if( !CV_NODE_IS_INT( fn->tag ) || fn->data.i <= 0 )
1762         CV_ERROR( CV_StsError, "Invalid size node: height must be positive integer" );
1763     cascade->orig_window_size.height = fn->data.i;
1764
1765     CV_CALL( cvStartReadSeq( stages_fn->data.seq, &stages_reader ) );
1766     for( i = 0; i < n; ++i )
1767     {
1768         CvFileNode* stage_fn;
1769         CvFileNode* trees_fn;
1770         CvSeqReader trees_reader;
1771
1772         stage_fn = (CvFileNode*) stages_reader.ptr;
1773         if( !CV_NODE_IS_MAP( stage_fn->tag ) )
1774         {
1775             sprintf( buf, "Invalid stage %d", i );
1776             CV_ERROR( CV_StsError, buf );
1777         }
1778
1779         CV_CALL( trees_fn = cvGetFileNodeByName( fs, stage_fn, ICV_HAAR_TREES_NAME ) );
1780         if( !trees_fn || !CV_NODE_IS_SEQ( trees_fn->tag )
1781             || trees_fn->data.seq->total <= 0 )
1782         {
1783             sprintf( buf, "Trees node is not a valid sequence. (stage %d)", i );
1784             CV_ERROR( CV_StsError, buf );
1785         }
1786
1787         CV_CALL( cascade->stage_classifier[i].classifier =
1788             (CvHaarClassifier*) cvAlloc( trees_fn->data.seq->total
1789                 * sizeof( cascade->stage_classifier[i].classifier[0] ) ) );
1790         for( j = 0; j < trees_fn->data.seq->total; ++j )
1791         {
1792             cascade->stage_classifier[i].classifier[j].haar_feature = NULL;
1793         }
1794         cascade->stage_classifier[i].count = trees_fn->data.seq->total;
1795
1796         CV_CALL( cvStartReadSeq( trees_fn->data.seq, &trees_reader ) );
1797         for( j = 0; j < trees_fn->data.seq->total; ++j )
1798         {
1799             CvFileNode* tree_fn;
1800             CvSeqReader tree_reader;
1801             CvHaarClassifier* classifier;
1802             int last_idx;
1803
1804             classifier = &cascade->stage_classifier[i].classifier[j];
1805             tree_fn = (CvFileNode*) trees_reader.ptr;
1806             if( !CV_NODE_IS_SEQ( tree_fn->tag ) || tree_fn->data.seq->total <= 0 )
1807             {
1808                 sprintf( buf, "Tree node is not a valid sequence."
1809                          " (stage %d, tree %d)", i, j );
1810                 CV_ERROR( CV_StsError, buf );
1811             }
1812
1813             classifier->count = tree_fn->data.seq->total;
1814             CV_CALL( classifier->haar_feature = (CvHaarFeature*) cvAlloc(
1815                 classifier->count * ( sizeof( *classifier->haar_feature ) +
1816                                       sizeof( *classifier->threshold ) +
1817                                       sizeof( *classifier->left ) +
1818                                       sizeof( *classifier->right ) ) +
1819                 (classifier->count + 1) * sizeof( *classifier->alpha ) ) );
1820             classifier->threshold = (float*) (classifier->haar_feature+classifier->count);
1821             classifier->left = (int*) (classifier->threshold + classifier->count);
1822             classifier->right = (int*) (classifier->left + classifier->count);
1823             classifier->alpha = (float*) (classifier->right + classifier->count);
1824
1825             CV_CALL( cvStartReadSeq( tree_fn->data.seq, &tree_reader ) );
1826             for( k = 0, last_idx = 0; k < tree_fn->data.seq->total; ++k )
1827             {
1828                 CvFileNode* node_fn;
1829                 CvFileNode* feature_fn;
1830                 CvFileNode* rects_fn;
1831                 CvSeqReader rects_reader;
1832
1833                 node_fn = (CvFileNode*) tree_reader.ptr;
1834                 if( !CV_NODE_IS_MAP( node_fn->tag ) )
1835                 {
1836                     sprintf( buf, "Tree node %d is not a valid map. (stage %d, tree %d)",
1837                              k, i, j );
1838                     CV_ERROR( CV_StsError, buf );
1839                 }
1840                 CV_CALL( feature_fn = cvGetFileNodeByName( fs, node_fn,
1841                     ICV_HAAR_FEATURE_NAME ) );
1842                 if( !feature_fn || !CV_NODE_IS_MAP( feature_fn->tag ) )
1843                 {
1844                     sprintf( buf, "Feature node is not a valid map. "
1845                              "(stage %d, tree %d, node %d)", i, j, k );
1846                     CV_ERROR( CV_StsError, buf );
1847                 }
1848                 CV_CALL( rects_fn = cvGetFileNodeByName( fs, feature_fn,
1849                     ICV_HAAR_RECTS_NAME ) );
1850                 if( !rects_fn || !CV_NODE_IS_SEQ( rects_fn->tag )
1851                     || rects_fn->data.seq->total < 1
1852                     || rects_fn->data.seq->total > CV_HAAR_FEATURE_MAX )
1853                 {
1854                     sprintf( buf, "Rects node is not a valid sequence. "
1855                              "(stage %d, tree %d, node %d)", i, j, k );
1856                     CV_ERROR( CV_StsError, buf );
1857                 }
1858                 CV_CALL( cvStartReadSeq( rects_fn->data.seq, &rects_reader ) );
1859                 for( l = 0; l < rects_fn->data.seq->total; ++l )
1860                 {
1861                     CvFileNode* rect_fn;
1862                     CvRect r;
1863
1864                     rect_fn = (CvFileNode*) rects_reader.ptr;
1865                     if( !CV_NODE_IS_SEQ( rect_fn->tag ) || rect_fn->data.seq->total != 5 )
1866                     {
1867                         sprintf( buf, "Rect %d is not a valid sequence. "
1868                                  "(stage %d, tree %d, node %d)", l, i, j, k );
1869                         CV_ERROR( CV_StsError, buf );
1870                     }
1871
1872                     fn = CV_SEQ_ELEM( rect_fn->data.seq, CvFileNode, 0 );
1873                     if( !CV_NODE_IS_INT( fn->tag ) || fn->data.i < 0 )
1874                     {
1875                         sprintf( buf, "x coordinate must be non-negative integer. "
1876                                  "(stage %d, tree %d, node %d, rect %d)", i, j, k, l );
1877                         CV_ERROR( CV_StsError, buf );
1878                     }
1879                     r.x = fn->data.i;
1880                     fn = CV_SEQ_ELEM( rect_fn->data.seq, CvFileNode, 1 );
1881                     if( !CV_NODE_IS_INT( fn->tag ) || fn->data.i < 0 )
1882                     {
1883                         sprintf( buf, "y coordinate must be non-negative integer. "
1884                                  "(stage %d, tree %d, node %d, rect %d)", i, j, k, l );
1885                         CV_ERROR( CV_StsError, buf );
1886                     }
1887                     r.y = fn->data.i;
1888                     fn = CV_SEQ_ELEM( rect_fn->data.seq, CvFileNode, 2 );
1889                     if( !CV_NODE_IS_INT( fn->tag ) || fn->data.i <= 0
1890                         || r.x + fn->data.i > cascade->orig_window_size.width )
1891                     {
1892                         sprintf( buf, "width must be positive integer and "
1893                                  "(x + width) must not exceed window width. "
1894                                  "(stage %d, tree %d, node %d, rect %d)", i, j, k, l );
1895                         CV_ERROR( CV_StsError, buf );
1896                     }
1897                     r.width = fn->data.i;
1898                     fn = CV_SEQ_ELEM( rect_fn->data.seq, CvFileNode, 3 );
1899                     if( !CV_NODE_IS_INT( fn->tag ) || fn->data.i <= 0
1900                         || r.y + fn->data.i > cascade->orig_window_size.height )
1901                     {
1902                         sprintf( buf, "height must be positive integer and "
1903                                  "(y + height) must not exceed window height. "
1904                                  "(stage %d, tree %d, node %d, rect %d)", i, j, k, l );
1905                         CV_ERROR( CV_StsError, buf );
1906                     }
1907                     r.height = fn->data.i;
1908                     fn = CV_SEQ_ELEM( rect_fn->data.seq, CvFileNode, 4 );
1909                     if( !CV_NODE_IS_REAL( fn->tag ) )
1910                     {
1911                         sprintf( buf, "weight must be real number. "
1912                                  "(stage %d, tree %d, node %d, rect %d)", i, j, k, l );
1913                         CV_ERROR( CV_StsError, buf );
1914                     }
1915
1916                     classifier->haar_feature[k].rect[l].weight = (float) fn->data.f;
1917                     classifier->haar_feature[k].rect[l].r = r;
1918
1919                     CV_NEXT_SEQ_ELEM( sizeof( *rect_fn ), rects_reader );
1920                 } /* for each rect */
1921                 for( l = rects_fn->data.seq->total; l < CV_HAAR_FEATURE_MAX; ++l )
1922                 {
1923                     classifier->haar_feature[k].rect[l].weight = 0;
1924                     classifier->haar_feature[k].rect[l].r = cvRect( 0, 0, 0, 0 );
1925                 }
1926
1927                 CV_CALL( fn = cvGetFileNodeByName( fs, feature_fn, ICV_HAAR_TILTED_NAME));
1928                 if( !fn || !CV_NODE_IS_INT( fn->tag ) )
1929                 {
1930                     sprintf( buf, "tilted must be 0 or 1. "
1931                              "(stage %d, tree %d, node %d)", i, j, k );
1932                     CV_ERROR( CV_StsError, buf );
1933                 }
1934                 classifier->haar_feature[k].tilted = ( fn->data.i != 0 );
1935                 CV_CALL( fn = cvGetFileNodeByName( fs, node_fn, ICV_HAAR_THRESHOLD_NAME));
1936                 if( !fn || !CV_NODE_IS_REAL( fn->tag ) )
1937                 {
1938                     sprintf( buf, "threshold must be real number. "
1939                              "(stage %d, tree %d, node %d)", i, j, k );
1940                     CV_ERROR( CV_StsError, buf );
1941                 }
1942                 classifier->threshold[k] = (float) fn->data.f;
1943                 CV_CALL( fn = cvGetFileNodeByName( fs, node_fn, ICV_HAAR_LEFT_NODE_NAME));
1944                 if( fn )
1945                 {
1946                     if( !CV_NODE_IS_INT( fn->tag ) || fn->data.i <= k
1947                         || fn->data.i >= tree_fn->data.seq->total )
1948                     {
1949                         sprintf( buf, "left node must be valid node number. "
1950                                  "(stage %d, tree %d, node %d)", i, j, k );
1951                         CV_ERROR( CV_StsError, buf );
1952                     }
1953                     /* left node */
1954                     classifier->left[k] = fn->data.i;
1955                 }
1956                 else
1957                 {
1958                     CV_CALL( fn = cvGetFileNodeByName( fs, node_fn,
1959                         ICV_HAAR_LEFT_VAL_NAME ) );
1960                     if( !fn )
1961                     {
1962                         sprintf( buf, "left node or left value must be specified. "
1963                                  "(stage %d, tree %d, node %d)", i, j, k );
1964                         CV_ERROR( CV_StsError, buf );
1965                     }
1966                     if( !CV_NODE_IS_REAL( fn->tag ) )
1967                     {
1968                         sprintf( buf, "left value must be real number. "
1969                                  "(stage %d, tree %d, node %d)", i, j, k );
1970                         CV_ERROR( CV_StsError, buf );
1971                     }
1972                     /* left value */
1973                     if( last_idx >= classifier->count + 1 )
1974                     {
1975                         sprintf( buf, "Tree structure is broken: too many values. "
1976                                  "(stage %d, tree %d, node %d)", i, j, k );
1977                         CV_ERROR( CV_StsError, buf );
1978                     }
1979                     classifier->left[k] = -last_idx;
1980                     classifier->alpha[last_idx++] = (float) fn->data.f;
1981                 }
1982                 CV_CALL( fn = cvGetFileNodeByName( fs, node_fn,ICV_HAAR_RIGHT_NODE_NAME));
1983                 if( fn )
1984                 {
1985                     if( !CV_NODE_IS_INT( fn->tag ) || fn->data.i <= k
1986                         || fn->data.i >= tree_fn->data.seq->total )
1987                     {
1988                         sprintf( buf, "right node must be valid node number. "
1989                                  "(stage %d, tree %d, node %d)", i, j, k );
1990                         CV_ERROR( CV_StsError, buf );
1991                     }
1992                     /* right node */
1993                     classifier->right[k] = fn->data.i;
1994                 }
1995                 else
1996                 {
1997                     CV_CALL( fn = cvGetFileNodeByName( fs, node_fn,
1998                         ICV_HAAR_RIGHT_VAL_NAME ) );
1999                     if( !fn )
2000                     {
2001                         sprintf( buf, "right node or right value must be specified. "
2002                                  "(stage %d, tree %d, node %d)", i, j, k );
2003                         CV_ERROR( CV_StsError, buf );
2004                     }
2005                     if( !CV_NODE_IS_REAL( fn->tag ) )
2006                     {
2007                         sprintf( buf, "right value must be real number. "
2008                                  "(stage %d, tree %d, node %d)", i, j, k );
2009                         CV_ERROR( CV_StsError, buf );
2010                     }
2011                     /* right value */
2012                     if( last_idx >= classifier->count + 1 )
2013                     {
2014                         sprintf( buf, "Tree structure is broken: too many values. "
2015                                  "(stage %d, tree %d, node %d)", i, j, k );
2016                         CV_ERROR( CV_StsError, buf );
2017                     }
2018                     classifier->right[k] = -last_idx;
2019                     classifier->alpha[last_idx++] = (float) fn->data.f;
2020                 }
2021
2022                 CV_NEXT_SEQ_ELEM( sizeof( *node_fn ), tree_reader );
2023             } /* for each node */
2024             if( last_idx != classifier->count + 1 )
2025             {
2026                 sprintf( buf, "Tree structure is broken: too few values. "
2027                          "(stage %d, tree %d)", i, j );
2028                 CV_ERROR( CV_StsError, buf );
2029             }
2030
2031             CV_NEXT_SEQ_ELEM( sizeof( *tree_fn ), trees_reader );
2032         } /* for each tree */
2033
2034         CV_CALL( fn = cvGetFileNodeByName( fs, stage_fn, ICV_HAAR_STAGE_THRESHOLD_NAME));
2035         if( !fn || !CV_NODE_IS_REAL( fn->tag ) )
2036         {
2037             sprintf( buf, "stage threshold must be real number. (stage %d)", i );
2038             CV_ERROR( CV_StsError, buf );
2039         }
2040         cascade->stage_classifier[i].threshold = (float) fn->data.f;
2041
2042         parent = i - 1;
2043         next = -1;
2044
2045         CV_CALL( fn = cvGetFileNodeByName( fs, stage_fn, ICV_HAAR_PARENT_NAME ) );
2046         if( !fn || !CV_NODE_IS_INT( fn->tag )
2047             || fn->data.i < -1 || fn->data.i >= cascade->count )
2048         {
2049             sprintf( buf, "parent must be integer number. (stage %d)", i );
2050             CV_ERROR( CV_StsError, buf );
2051         }
2052         parent = fn->data.i;
2053         CV_CALL( fn = cvGetFileNodeByName( fs, stage_fn, ICV_HAAR_NEXT_NAME ) );
2054         if( !fn || !CV_NODE_IS_INT( fn->tag )
2055             || fn->data.i < -1 || fn->data.i >= cascade->count )
2056         {
2057             sprintf( buf, "next must be integer number. (stage %d)", i );
2058             CV_ERROR( CV_StsError, buf );
2059         }
2060         next = fn->data.i;
2061
2062         cascade->stage_classifier[i].parent = parent;
2063         cascade->stage_classifier[i].next = next;
2064         cascade->stage_classifier[i].child = -1;
2065
2066         if( parent != -1 && cascade->stage_classifier[parent].child == -1 )
2067         {
2068             cascade->stage_classifier[parent].child = i;
2069         }
2070
2071         CV_NEXT_SEQ_ELEM( sizeof( *stage_fn ), stages_reader );
2072     } /* for each stage */
2073
2074     __END__;
2075
2076     if( cvGetErrStatus() < 0 )
2077     {
2078         cvReleaseHaarClassifierCascade( &cascade );
2079         cascade = NULL;
2080     }
2081
2082     return cascade;
2083 }
2084
2085 static void
2086 icvWriteHaarClassifier( CvFileStorage* fs, const char* name, const void* struct_ptr,
2087                         CvAttrList attributes )
2088 {
2089     CV_FUNCNAME( "cvWriteHaarClassifier" );
2090
2091     __BEGIN__;
2092
2093     int i, j, k, l;
2094     char buf[256];
2095     const CvHaarClassifierCascade* cascade = (const CvHaarClassifierCascade*) struct_ptr;
2096
2097     /* TODO: parameters check */
2098
2099     CV_CALL( cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_HAAR, attributes ) );
2100
2101     CV_CALL( cvStartWriteStruct( fs, ICV_HAAR_SIZE_NAME, CV_NODE_SEQ | CV_NODE_FLOW ) );
2102     CV_CALL( cvWriteInt( fs, NULL, cascade->orig_window_size.width ) );
2103     CV_CALL( cvWriteInt( fs, NULL, cascade->orig_window_size.height ) );
2104     CV_CALL( cvEndWriteStruct( fs ) ); /* size */
2105
2106     CV_CALL( cvStartWriteStruct( fs, ICV_HAAR_STAGES_NAME, CV_NODE_SEQ ) );
2107     for( i = 0; i < cascade->count; ++i )
2108     {
2109         CV_CALL( cvStartWriteStruct( fs, NULL, CV_NODE_MAP ) );
2110         sprintf( buf, "stage %d", i );
2111         CV_CALL( cvWriteComment( fs, buf, 1 ) );
2112
2113         CV_CALL( cvStartWriteStruct( fs, ICV_HAAR_TREES_NAME, CV_NODE_SEQ ) );
2114
2115         for( j = 0; j < cascade->stage_classifier[i].count; ++j )
2116         {
2117             CvHaarClassifier* tree = &cascade->stage_classifier[i].classifier[j];
2118
2119             CV_CALL( cvStartWriteStruct( fs, NULL, CV_NODE_SEQ ) );
2120             sprintf( buf, "tree %d", j );
2121             CV_CALL( cvWriteComment( fs, buf, 1 ) );
2122
2123             for( k = 0; k < tree->count; ++k )
2124             {
2125                 CvHaarFeature* feature = &tree->haar_feature[k];
2126
2127                 CV_CALL( cvStartWriteStruct( fs, NULL, CV_NODE_MAP ) );
2128                 if( k )
2129                 {
2130                     sprintf( buf, "node %d", k );
2131                 }
2132                 else
2133                 {
2134                     sprintf( buf, "root node" );
2135                 }
2136                 CV_CALL( cvWriteComment( fs, buf, 1 ) );
2137
2138                 CV_CALL( cvStartWriteStruct( fs, ICV_HAAR_FEATURE_NAME, CV_NODE_MAP ) );
2139
2140                 CV_CALL( cvStartWriteStruct( fs, ICV_HAAR_RECTS_NAME, CV_NODE_SEQ ) );
2141                 for( l = 0; l < CV_HAAR_FEATURE_MAX && feature->rect[l].r.width != 0; ++l )
2142                 {
2143                     CV_CALL( cvStartWriteStruct( fs, NULL, CV_NODE_SEQ | CV_NODE_FLOW ) );
2144                     CV_CALL( cvWriteInt(  fs, NULL, feature->rect[l].r.x ) );
2145                     CV_CALL( cvWriteInt(  fs, NULL, feature->rect[l].r.y ) );
2146                     CV_CALL( cvWriteInt(  fs, NULL, feature->rect[l].r.width ) );
2147                     CV_CALL( cvWriteInt(  fs, NULL, feature->rect[l].r.height ) );
2148                     CV_CALL( cvWriteReal( fs, NULL, feature->rect[l].weight ) );
2149                     CV_CALL( cvEndWriteStruct( fs ) ); /* rect */
2150                 }
2151                 CV_CALL( cvEndWriteStruct( fs ) ); /* rects */
2152                 CV_CALL( cvWriteInt( fs, ICV_HAAR_TILTED_NAME, feature->tilted ) );
2153                 CV_CALL( cvEndWriteStruct( fs ) ); /* feature */
2154
2155                 CV_CALL( cvWriteReal( fs, ICV_HAAR_THRESHOLD_NAME, tree->threshold[k]) );
2156
2157                 if( tree->left[k] > 0 )
2158                 {
2159                     CV_CALL( cvWriteInt( fs, ICV_HAAR_LEFT_NODE_NAME, tree->left[k] ) );
2160                 }
2161                 else
2162                 {
2163                     CV_CALL( cvWriteReal( fs, ICV_HAAR_LEFT_VAL_NAME,
2164                         tree->alpha[-tree->left[k]] ) );
2165                 }
2166
2167                 if( tree->right[k] > 0 )
2168                 {
2169                     CV_CALL( cvWriteInt( fs, ICV_HAAR_RIGHT_NODE_NAME, tree->right[k] ) );
2170                 }
2171                 else
2172                 {
2173                     CV_CALL( cvWriteReal( fs, ICV_HAAR_RIGHT_VAL_NAME,
2174                         tree->alpha[-tree->right[k]] ) );
2175                 }
2176
2177                 CV_CALL( cvEndWriteStruct( fs ) ); /* split */
2178             }
2179
2180             CV_CALL( cvEndWriteStruct( fs ) ); /* tree */
2181         }
2182
2183         CV_CALL( cvEndWriteStruct( fs ) ); /* trees */
2184
2185         CV_CALL( cvWriteReal( fs, ICV_HAAR_STAGE_THRESHOLD_NAME,
2186                               cascade->stage_classifier[i].threshold) );
2187
2188         CV_CALL( cvWriteInt( fs, ICV_HAAR_PARENT_NAME,
2189                               cascade->stage_classifier[i].parent ) );
2190         CV_CALL( cvWriteInt( fs, ICV_HAAR_NEXT_NAME,
2191                               cascade->stage_classifier[i].next ) );
2192
2193         CV_CALL( cvEndWriteStruct( fs ) ); /* stage */
2194     } /* for each stage */
2195
2196     CV_CALL( cvEndWriteStruct( fs ) ); /* stages */
2197     CV_CALL( cvEndWriteStruct( fs ) ); /* root */
2198
2199     __END__;
2200 }
2201
2202 static void*
2203 icvCloneHaarClassifier( const void* struct_ptr )
2204 {
2205     CvHaarClassifierCascade* cascade = NULL;
2206
2207     CV_FUNCNAME( "cvCloneHaarClassifier" );
2208
2209     __BEGIN__;
2210
2211     int i, j, k, n;
2212     const CvHaarClassifierCascade* cascade_src =
2213         (const CvHaarClassifierCascade*) struct_ptr;
2214
2215     n = cascade_src->count;
2216     CV_CALL( cascade = icvCreateHaarClassifierCascade(n) );
2217     cascade->orig_window_size = cascade_src->orig_window_size;
2218
2219     for( i = 0; i < n; ++i )
2220     {
2221         cascade->stage_classifier[i].parent = cascade_src->stage_classifier[i].parent;
2222         cascade->stage_classifier[i].next = cascade_src->stage_classifier[i].next;
2223         cascade->stage_classifier[i].child = cascade_src->stage_classifier[i].child;
2224         cascade->stage_classifier[i].threshold = cascade_src->stage_classifier[i].threshold;
2225
2226         cascade->stage_classifier[i].count = 0;
2227         CV_CALL( cascade->stage_classifier[i].classifier =
2228             (CvHaarClassifier*) cvAlloc( cascade_src->stage_classifier[i].count
2229                 * sizeof( cascade->stage_classifier[i].classifier[0] ) ) );
2230
2231         cascade->stage_classifier[i].count = cascade_src->stage_classifier[i].count;
2232
2233         for( j = 0; j < cascade->stage_classifier[i].count; ++j )
2234         {
2235             cascade->stage_classifier[i].classifier[j].haar_feature = NULL;
2236         }
2237
2238         for( j = 0; j < cascade->stage_classifier[i].count; ++j )
2239         {
2240             const CvHaarClassifier* classifier_src =
2241                 &cascade_src->stage_classifier[i].classifier[j];
2242             CvHaarClassifier* classifier =
2243                 &cascade->stage_classifier[i].classifier[j];
2244
2245             classifier->count = classifier_src->count;
2246             CV_CALL( classifier->haar_feature = (CvHaarFeature*) cvAlloc(
2247                 classifier->count * ( sizeof( *classifier->haar_feature ) +
2248                                       sizeof( *classifier->threshold ) +
2249                                       sizeof( *classifier->left ) +
2250                                       sizeof( *classifier->right ) ) +
2251                 (classifier->count + 1) * sizeof( *classifier->alpha ) ) );
2252             classifier->threshold = (float*) (classifier->haar_feature+classifier->count);
2253             classifier->left = (int*) (classifier->threshold + classifier->count);
2254             classifier->right = (int*) (classifier->left + classifier->count);
2255             classifier->alpha = (float*) (classifier->right + classifier->count);
2256             for( k = 0; k < classifier->count; ++k )
2257             {
2258                 classifier->haar_feature[k] = classifier_src->haar_feature[k];
2259                 classifier->threshold[k] = classifier_src->threshold[k];
2260                 classifier->left[k] = classifier_src->left[k];
2261                 classifier->right[k] = classifier_src->right[k];
2262                 classifier->alpha[k] = classifier_src->alpha[k];
2263             }
2264             classifier->alpha[classifier->count] =
2265                 classifier_src->alpha[classifier->count];
2266         }
2267     }
2268
2269     __END__;
2270
2271     return cascade;
2272 }
2273
2274
2275 CvType haar_type( CV_TYPE_NAME_HAAR, icvIsHaarClassifier,
2276                   (CvReleaseFunc)cvReleaseHaarClassifierCascade,
2277                   icvReadHaarClassifier, icvWriteHaarClassifier,
2278                   icvCloneHaarClassifier );
2279
2280 /* End of file. */