1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
42 /* Haar features calculation */
47 /* these settings affect the quality of detection: change with care */
48 #define CV_ADJUST_FEATURES 1
49 #define CV_ADJUST_WEIGHTS 0
52 typedef double sqsumtype;
54 typedef struct CvHidHaarFeature
58 sumtype *p0, *p1, *p2, *p3;
61 rect[CV_HAAR_FEATURE_MAX];
66 typedef struct CvHidHaarTreeNode
68 CvHidHaarFeature feature;
76 typedef struct CvHidHaarClassifier
79 //CvHaarFeature* orig_feature;
80 CvHidHaarTreeNode* node;
86 typedef struct CvHidHaarStageClassifier
90 CvHidHaarClassifier* classifier;
93 struct CvHidHaarStageClassifier* next;
94 struct CvHidHaarStageClassifier* child;
95 struct CvHidHaarStageClassifier* parent;
97 CvHidHaarStageClassifier;
100 struct CvHidHaarClassifierCascade
104 int has_tilted_features;
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;
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;
122 const int icv_object_win_border = 1;
123 const float icv_stage_threshold_bias = 0.0001f;
125 static CvHaarClassifierCascade*
126 icvCreateHaarClassifierCascade( int stage_count )
128 CvHaarClassifierCascade* cascade = 0;
130 CV_FUNCNAME( "icvCreateHaarClassifierCascade" );
134 int block_size = sizeof(*cascade) + stage_count*sizeof(*cascade->stage_classifier);
136 if( stage_count <= 0 )
137 CV_ERROR( CV_StsOutOfRange, "Number of stages should be positive" );
139 CV_CALL( cascade = (CvHaarClassifierCascade*)cvAlloc( block_size ));
140 memset( cascade, 0, block_size );
142 cascade->stage_classifier = (CvHaarStageClassifier*)(cascade + 1);
143 cascade->flags = CV_HAAR_MAGIC_VAL;
144 cascade->count = stage_count;
152 icvReleaseHidHaarClassifierCascade( CvHidHaarClassifierCascade** _cascade )
154 if( _cascade && *_cascade )
156 CvHidHaarClassifierCascade* cascade = *_cascade;
157 if( cascade->ipp_stages && icvHaarClassifierFree_32f_p )
160 for( i = 0; i < cascade->count; i++ )
162 if( cascade->ipp_stages[i] )
163 icvHaarClassifierFree_32f_p( cascade->ipp_stages[i] );
166 cvFree( &cascade->ipp_stages );
171 /* create more efficient internal representation of haar classifier cascade */
172 static CvHidHaarClassifierCascade*
173 icvCreateHidHaarClassifierCascade( CvHaarClassifierCascade* cascade )
175 CvRect* ipp_features = 0;
176 float *ipp_weights = 0, *ipp_thresholds = 0, *ipp_val1 = 0, *ipp_val2 = 0;
179 CvHidHaarClassifierCascade* out = 0;
181 CV_FUNCNAME( "icvCreateHidHaarClassifierCascade" );
187 int total_classifiers = 0;
190 CvHidHaarClassifier* haar_classifier_ptr;
191 CvHidHaarTreeNode* haar_node_ptr;
192 CvSize orig_window_size;
193 int has_tilted_features = 0;
196 if( !CV_IS_HAAR_CLASSIFIER(cascade) )
197 CV_ERROR( !cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier pointer" );
199 if( cascade->hid_cascade )
200 CV_ERROR( CV_StsError, "hid_cascade has been already created" );
202 if( !cascade->stage_classifier )
203 CV_ERROR( CV_StsNullPtr, "" );
205 if( cascade->count <= 0 )
206 CV_ERROR( CV_StsOutOfRange, "Negative number of cascade stages" );
208 orig_window_size = cascade->orig_window_size;
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++ )
214 CvHaarStageClassifier* stage_classifier = cascade->stage_classifier + i;
216 if( !stage_classifier->classifier ||
217 stage_classifier->count <= 0 )
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 );
224 max_count = MAX( max_count, stage_classifier->count );
225 total_classifiers += stage_classifier->count;
227 for( j = 0; j < stage_classifier->count; j++ )
229 CvHaarClassifier* classifier = stage_classifier->classifier + j;
231 total_nodes += classifier->count;
232 for( l = 0; l < classifier->count; l++ )
234 for( k = 0; k < CV_HAAR_FEATURE_MAX; k++ )
236 if( classifier->haar_feature[l].rect[k].r.width )
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
245 (r.x < 0 || r.y + r.height > orig_window_size.height))
247 (tilted && (r.x - r.height < 0 ||
248 r.y + r.width + r.height > orig_window_size.height)))
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 );
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);
268 CV_CALL( out = (CvHidHaarClassifierCascade*)cvAlloc( datasize ));
269 memset( out, 0, sizeof(*out) );
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);
277 out->is_stump_based = 1;
278 out->has_tilted_features = has_tilted_features;
281 /* initialize internal representation */
282 for( i = 0; i < cascade->count; i++ )
284 CvHaarStageClassifier* stage_classifier = cascade->stage_classifier + i;
285 CvHidHaarStageClassifier* hid_stage_classifier = out->stage_classifier + i;
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;
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;
300 out->is_tree |= hid_stage_classifier->next != NULL;
302 for( j = 0; j < stage_classifier->count; j++ )
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);
309 hid_classifier->count = node_count;
310 hid_classifier->node = haar_node_ptr;
311 hid_classifier->alpha = alpha_ptr;
313 for( l = 0; l < node_count; l++ )
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];
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]) );
327 hid_stage_classifier->two_rects = 0;
330 memcpy( alpha_ptr, classifier->alpha, (node_count+1)*sizeof(alpha_ptr[0]));
332 (CvHidHaarTreeNode*)cvAlignPtr(alpha_ptr+node_count+1, sizeof(void*));
334 out->is_stump_based &= node_count == 1;
339 // NOTE: Currently, OpenMP is implemented and IPP modes are incompatible.
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;
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)));
355 CV_CALL( out->ipp_stages = (void**)cvAlloc( ipp_datasize ));
356 memset( out->ipp_stages, 0, ipp_datasize );
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]) ));
365 for( i = 0; i < cascade->count; i++ )
367 CvHaarStageClassifier* stage_classifier = cascade->stage_classifier + i;
368 for( j = 0, k = 0; j < stage_classifier->count; j++ )
370 CvHaarClassifier* classifier = stage_classifier->classifier + j;
371 int rect_count = 2 + (classifier->haar_feature->rect[2].r.width != 0);
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;
378 for( l = 0; l < rect_count; l++, k++ )
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;
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 )
392 if( i < cascade->count )
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 );
403 cascade->hid_cascade = out;
404 assert( (char*)haar_node_ptr - (char*)out <= datasize );
408 if( cvGetErrStatus() < 0 )
409 icvReleaseHidHaarClassifierCascade( &out );
411 cvFree( &ipp_features );
412 cvFree( &ipp_weights );
413 cvFree( &ipp_thresholds );
416 cvFree( &ipp_counts );
422 #define sum_elem_ptr(sum,row,col) \
423 ((sumtype*)CV_MAT_ELEM_PTR_FAST((sum),(row),(col),sizeof(sumtype)))
425 #define sqsum_elem_ptr(sqsum,row,col) \
426 ((sqsumtype*)CV_MAT_ELEM_PTR_FAST((sqsum),(row),(col),sizeof(sqsumtype)))
428 #define calc_sum(rect,offset) \
429 ((rect).p0[offset] - (rect).p1[offset] - (rect).p2[offset] + (rect).p3[offset])
433 cvSetImagesForHaarClassifierCascade( CvHaarClassifierCascade* _cascade,
436 const CvArr* _tilted_sum,
439 CV_FUNCNAME("cvSetImagesForHaarClassifierCascade");
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;
452 if( !CV_IS_HAAR_CLASSIFIER(_cascade) )
453 CV_ERROR( !_cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier pointer" );
456 CV_ERROR( CV_StsOutOfRange, "Scale must be positive" );
458 CV_CALL( sum = cvGetMat( sum, &sum_stub, &coi0 ));
459 CV_CALL( sqsum = cvGetMat( sqsum, &sqsum_stub, &coi1 ));
462 CV_ERROR( CV_BadCOI, "COI is not supported" );
464 if( !CV_ARE_SIZES_EQ( sum, sqsum ))
465 CV_ERROR( CV_StsUnmatchedSizes, "All integral images must have the same size" );
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" );
472 if( !_cascade->hid_cascade )
473 CV_CALL( icvCreateHidHaarClassifierCascade(_cascade) );
475 cascade = _cascade->hid_cascade;
477 if( cascade->has_tilted_features )
479 CV_CALL( tilted = cvGetMat( tilted, &tilted_stub, &coi1 ));
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" );
485 if( sum->step != tilted->step )
486 CV_ERROR( CV_StsUnmatchedSizes,
487 "Sum and tilted_sum must have the same stride (step, widthStep)" );
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;
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 );
499 cascade->sqsum = *sqsum;
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;
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 );
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 );
519 /* init pointers in haar features according to real window size and
520 given image pointers */
523 int max_threads = cvGetNumThreads();
524 #pragma omp parallel for num_threads(max_threads), schedule(dynamic)
526 for( i = 0; i < _cascade->count; i++ )
529 for( j = 0; j < cascade->stage_classifier[i].count; j++ )
531 for( l = 0; l < cascade->stage_classifier[i].classifier[j].count; l++ )
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;
541 #if CV_ADJUST_FEATURES
542 int base_w = -1, base_h = -1;
543 int new_base_w = 0, new_base_h = 0;
545 int flagx = 0, flagy = 0;
551 for( k = 0; k < CV_HAAR_FEATURE_MAX; k++ )
553 if( !hidfeature->rect[k].p0 )
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) );
566 #if CV_ADJUST_FEATURES
569 kx = r[0].width / base_w;
570 ky = r[0].height / base_h;
575 new_base_w = cvRound( r[0].width * scale ) / kx;
576 x0 = cvRound( r[0].x * scale );
582 new_base_h = cvRound( r[0].height * scale ) / ky;
583 y0 = cvRound( r[0].y * scale );
587 for( k = 0; k < nr; k++ )
590 double correction_ratio;
592 #if CV_ADJUST_FEATURES
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;
601 tr.x = cvRound( r[k].x * scale );
602 tr.width = cvRound( r[k].width * scale );
605 #if CV_ADJUST_FEATURES
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;
614 tr.y = cvRound( r[k].y * scale );
615 tr.height = cvRound( r[k].height * scale );
618 #if CV_ADJUST_WEIGHTS
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;
632 correction_ratio = weight_scale * (!feature->tilted ? 1 : 0.5);
635 if( !feature->tilted )
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);
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);
651 hidfeature->rect[k].weight = (float)(feature->rect[k].weight * correction_ratio);
654 area0 = tr.width * tr.height;
656 sum0 += hidfeature->rect[k].weight * tr.width * tr.height;
659 hidfeature->rect[0].weight = (float)(-sum0/area0);
670 double icvEvalHidHaarClassifier( CvHidHaarClassifier* classifier,
671 double variance_norm_factor,
677 CvHidHaarTreeNode* node = classifier->node + idx;
678 double t = node->threshold * variance_norm_factor;
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;
683 if( node->feature.rect[2].p0 )
684 sum += calc_sum(node->feature.rect[2],p_offset) * node->feature.rect[2].weight;
686 idx = sum < t ? node->left : node->right;
689 return classifier->alpha[-idx];
694 cvRunHaarClassifierCascade( CvHaarClassifierCascade* _cascade,
695 CvPoint pt, int start_stage )
698 CV_FUNCNAME("cvRunHaarClassifierCascade");
702 int p_offset, pq_offset;
704 double mean, variance_norm_factor;
705 CvHidHaarClassifierCascade* cascade;
707 if( !CV_IS_HAAR_CLASSIFIER(_cascade) )
708 CV_ERROR( !_cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid cascade pointer" );
710 cascade = _cascade->hid_cascade;
712 CV_ERROR( CV_StsNullPtr, "Hidden cascade has not been created.\n"
713 "Use cvSetImagesForHaarClassifierCascade" );
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 )
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);
729 variance_norm_factor = 1.;
731 if( cascade->is_tree )
733 CvHidHaarStageClassifier* ptr;
734 assert( start_stage == 0 );
737 ptr = cascade->stage_classifier;
741 double stage_sum = 0;
743 for( j = 0; j < ptr->count; j++ )
745 stage_sum += icvEvalHidHaarClassifier( ptr->classifier + j,
746 variance_norm_factor, p_offset );
749 if( stage_sum >= ptr->threshold )
755 while( ptr && ptr->next == NULL ) ptr = ptr->parent;
765 else if( cascade->is_stump_based )
767 for( i = start_stage; i < cascade->count; i++ )
769 double stage_sum = 0;
771 if( cascade->stage_classifier[i].two_rects )
773 for( j = 0; j < cascade->stage_classifier[i].count; j++ )
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;
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;
782 a = classifier->alpha[0];
783 b = classifier->alpha[1];
784 stage_sum += sum < t ? a : b;
789 for( j = 0; j < cascade->stage_classifier[i].count; j++ )
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;
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;
798 if( node->feature.rect[2].p0 )
799 sum += calc_sum(node->feature.rect[2],p_offset) * node->feature.rect[2].weight;
801 a = classifier->alpha[0];
802 b = classifier->alpha[1];
803 stage_sum += sum < t ? a : b;
807 if( stage_sum < cascade->stage_classifier[i].threshold )
816 for( i = start_stage; i < cascade->count; i++ )
818 double stage_sum = 0;
820 for( j = 0; j < cascade->stage_classifier[i].count; j++ )
822 stage_sum += icvEvalHidHaarClassifier(
823 cascade->stage_classifier[i].classifier + j,
824 variance_norm_factor, p_offset );
827 if( stage_sum < cascade->stage_classifier[i].threshold )
843 static int is_equal( const void* _r1, const void* _r2, void* )
845 const CvRect* r1 = (const CvRect*)_r1;
846 const CvRect* r2 = (const CvRect*)_r2;
847 int distance = cvRound(r1->width*0.2);
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;
858 #define VERY_ROUGH_SEARCH 0
861 cvHaarDetectObjects( const CvArr* _img,
862 CvHaarClassifierCascade* cascade,
863 CvMemStorage* storage, double scale_factor,
864 int min_neighbors, int flags, CvSize min_size )
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;
876 CvSeq* seq_thread[CV_MAX_THREADS] = {0};
880 CV_FUNCNAME( "cvHaarDetectObjects" );
884 CvSeq *seq = 0, *seq2 = 0, *idx_seq = 0, *big_seq = 0;
885 CvAvgComp result_comp = {{0,0,0,0},0};
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;
892 if( !CV_IS_HAAR_CLASSIFIER(cascade) )
893 CV_ERROR( !cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier cascade" );
896 CV_ERROR( CV_StsNullPtr, "Null storage pointer" );
898 CV_CALL( img = cvGetMat( img, &stub, &coi ));
900 CV_ERROR( CV_BadCOI, "COI is not supported" );
902 if( CV_MAT_DEPTH(img->type) != CV_8U )
903 CV_ERROR( CV_StsUnsupportedFormat, "Only 8-bit images are supported" );
905 if( find_biggest_object )
906 flags &= ~CV_HAAR_SCALE_IMAGE;
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 ));
914 max_threads = cvGetNumThreads();
915 for( i = 0; i < max_threads; i++ )
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 ));
924 if( !cascade->hid_cascade )
925 CV_CALL( icvCreateHidHaarClassifierCascade(cascade) );
927 if( cascade->hid_cascade->has_tilted_features )
928 tilted = cvCreateMat( img->rows + 1, img->cols + 1, CV_32SC1 );
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 );
934 if( CV_MAT_CN(img->type) > 1 )
936 cvCvtColor( img, temp, CV_BGR2GRAY );
940 if( flags & CV_HAAR_SCALE_IMAGE )
942 CvSize win_size0 = cascade->orig_window_size;
943 int use_ipp = cascade->hid_cascade->ipp_stages != 0 &&
944 icvApplyHaarClassifier_32s32f_C1R_p != 0;
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 ));
950 for( factor = 1; ; factor *= scale_factor )
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;
964 if( sz1.width <= 0 || sz1.height <= 0 )
966 if( win_size.width < min_size.width || win_size.height < min_size.height )
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 );
974 tilted1 = cvMat( sz.height+1, sz.width+1, CV_32SC1, tilted->data.ptr );
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 );
980 cvResize( img, &img1, CV_INTER_LINEAR );
981 cvIntegral( &img1, &sum1, &sqsum1, _tilted );
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 )
989 positive = mask1.cols*mask1.rows;
990 cvSet( &mask1, cvScalarAll(255) );
991 for( i = 0; i < cascade->count; i++ )
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 )
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++ )
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];
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 )
1024 CvRect obj_rect = { cvRound(x*factor), cvRound(y*factor),
1025 win_size.width, win_size.height };
1026 cvSeqPush( seq, &obj_rect );
1034 CvRect scan_roi_rect = {0,0,0,0};
1035 bool is_found = false, scan_roi = false;
1037 cvIntegral( img, sum, sqsum, tilted );
1039 if( do_canny_pruning )
1041 sumcanny = cvCreateMat( img->rows + 1, img->cols + 1, CV_32SC1 );
1042 cvCanny( img, temp, 0, 50, 3 );
1043 cvIntegral( temp, sumcanny );
1046 if( (unsigned)split_stage >= (unsigned)cascade->count ||
1047 cascade->hid_cascade->is_tree )
1049 split_stage = cascade->count;
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 )
1059 if( find_biggest_object )
1061 scale_factor = 1./scale_factor;
1062 factor *= scale_factor;
1063 big_seq = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvRect), temp_storage );
1068 for( ; n_factors-- > 0 && !is_found; factor *= scale_factor )
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);
1081 if( win_size.width < min_size.width || win_size.height < min_size.height )
1083 if( find_biggest_object )
1088 cvSetImagesForHaarClassifierCascade( cascade, sum, sqsum, tilted, factor );
1091 if( do_canny_pruning )
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);
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;
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;
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);
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);
1123 cascade->hid_cascade->count = split_stage;
1125 for( pass = 0; pass < npass; pass++ )
1128 #pragma omp parallel for num_threads(max_threads), schedule(dynamic)
1130 for( int _iy = start_y; _iy < end_y; _iy++ )
1132 int iy = cvRound(_iy*ystep);
1133 int _ix, _xstep = 1;
1134 uchar* mask_row = temp->data.ptr + temp->step * iy;
1136 for( _ix = start_x; _ix < end_x; _ix += _xstep )
1138 int ix = cvRound(_ix*ystep); // it really should be ystep
1145 if( do_canny_pruning )
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 )
1157 result = cvRunHaarClassifierCascade( cascade, cvPoint(ix,iy), 0 );
1160 if( pass < npass - 1 )
1164 CvRect rect = cvRect(ix,iy,win_size.width,win_size.height);
1166 cvSeqPush( seq, &rect );
1168 cvSeqPush( seq_thread[omp_get_thread_num()], &rect );
1175 else if( mask_row[ix] )
1177 int result = cvRunHaarClassifierCascade( cascade, cvPoint(ix,iy),
1181 if( pass == npass - 1 )
1183 CvRect rect = cvRect(ix,iy,win_size.width,win_size.height);
1185 cvSeqPush( seq, &rect );
1187 cvSeqPush( seq_thread[omp_get_thread_num()], &rect );
1196 stage_offset = cascade->hid_cascade->count;
1197 cascade->hid_cascade->count = cascade->count;
1201 // gather the results
1202 for( i = 0; i < max_threads; i++ )
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 );
1212 if( find_biggest_object )
1214 CvSeq* bseq = min_neighbors > 0 ? big_seq : seq;
1216 if( min_neighbors > 0 && !scan_roi )
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]));
1223 #if VERY_ROUGH_SEARCH
1226 for( i = 0; i < seq->total; i++ )
1228 CvRect r1 = *(CvRect*)cvGetSeqElem( seq, i );
1229 int idx = *(int*)cvGetSeqElem( idx_seq, i );
1230 assert( (unsigned)idx < (unsigned)ncomp );
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;
1239 // calculate average bounding box
1240 for( i = 0; i < ncomp; i++ )
1242 int n = comps[i].neighbors;
1243 if( n >= min_neighbors )
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);
1251 cvSeqPush( bseq, &comp );
1258 for( i = 0 ; i <= ncomp; i++ )
1259 comps[i].rect.x = comps[i].rect.y = INT_MAX;
1261 // count number of neighbors
1262 for( i = 0; i < seq->total; i++ )
1264 CvRect r1 = *(CvRect*)cvGetSeqElem( seq, i );
1265 int idx = *(int*)cvGetSeqElem( idx_seq, i );
1266 assert( (unsigned)idx < (unsigned)ncomp );
1268 comps[idx].neighbors++;
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);
1277 // calculate enclosing box
1278 for( i = 0; i < ncomp; i++ )
1280 int n = comps[i].neighbors;
1281 if( n >= min_neighbors )
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;
1292 t = cvRound( comp.rect.width*min_scale );
1293 min_size.width = MAX( min_size.width, t );
1295 t = cvRound( comp.rect.height*min_scale );
1296 min_size.height = MAX( min_size.height, t );
1298 //expand the box by 20% because we could miss some neighbours
1299 //see 'is_equal' function
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;
1311 cvSeqPush( bseq, &comp );
1319 // extract the biggest rect
1320 if( bseq->total > 0 )
1323 for( i = 0; i < bseq->total; i++ )
1325 CvAvgComp* comp = (CvAvgComp*)cvGetSeqElem( bseq, i );
1326 int area = comp->rect.width * comp->rect.height;
1327 if( max_area < area )
1330 result_comp.rect = comp->rect;
1331 result_comp.neighbors = bseq == seq ? 1 : comp->neighbors;
1335 //Prepare information for further scanning inside the biggest rectangle
1337 #if VERY_ROUGH_SEARCH
1338 // change scan ranges to roi in case of required
1339 if( !rough_search && !scan_roi )
1342 scan_roi_rect = result_comp.rect;
1345 else if( rough_search )
1351 scan_roi_rect = result_comp.rect;
1360 if( min_neighbors == 0 && !find_biggest_object )
1362 for( i = 0; i < seq->total; i++ )
1364 CvRect* rect = (CvRect*)cvGetSeqElem( seq, i );
1368 cvSeqPush( result_seq, &comp );
1372 if( min_neighbors != 0
1373 #if VERY_ROUGH_SEARCH
1374 && (!find_biggest_object || !rough_search)
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]));
1383 // count number of neighbors
1384 for( i = 0; i < seq->total; i++ )
1386 CvRect r1 = *(CvRect*)cvGetSeqElem( seq, i );
1387 int idx = *(int*)cvGetSeqElem( idx_seq, i );
1388 assert( (unsigned)idx < (unsigned)ncomp );
1390 comps[idx].neighbors++;
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;
1398 // calculate average bounding box
1399 for( i = 0; i < ncomp; i++ )
1401 int n = comps[i].neighbors;
1402 if( n >= min_neighbors )
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;
1411 cvSeqPush( seq2, &comp );
1415 if( !find_biggest_object )
1417 // filter out small face rectangles inside large face rectangles
1418 for( i = 0; i < seq2->total; i++ )
1420 CvAvgComp r1 = *(CvAvgComp*)cvGetSeqElem( seq2, i );
1423 for( j = 0; j < seq2->total; j++ )
1425 CvAvgComp r2 = *(CvAvgComp*)cvGetSeqElem( seq2, j );
1426 int distance = cvRound( r2.rect.width * 0.2 );
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) )
1441 cvSeqPush( result_seq, &r1 );
1447 for( i = 0; i < seq2->total; i++ )
1449 CvAvgComp* comp = (CvAvgComp*)cvGetSeqElem( seq2, i );
1450 int area = comp->rect.width * comp->rect.height;
1451 if( max_area < area )
1454 result_comp = *comp;
1460 if( find_biggest_object && result_comp.rect.width > 0 )
1461 cvSeqPush( result_seq, &result_comp );
1466 for( i = 0; i < max_threads; i++ )
1469 cvReleaseMemStorage( &seq_thread[i]->storage );
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 );
1487 static CvHaarClassifierCascade*
1488 icvLoadCascadeCART( const char** input_cascade, int n, CvSize orig_window_size )
1491 CvHaarClassifierCascade* cascade = icvCreateHaarClassifierCascade(n);
1492 cascade->orig_window_size = orig_window_size;
1494 for( i = 0; i < n; i++ )
1497 float threshold = 0;
1498 const char* stage = input_cascade[i];
1505 sscanf( stage, "%d%n", &count, &dl );
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]));
1513 for( j = 0; j < count; j++ )
1515 CvHaarClassifier* classifier = cascade->stage_classifier[i].classifier + j;
1519 sscanf( stage, "%d%n", &classifier->count, &dl );
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);
1533 for( l = 0; l < classifier->count; l++ )
1535 sscanf( stage, "%d%n", &rects, &dl );
1538 assert( rects >= 2 && rects <= CV_HAAR_FEATURE_MAX );
1540 for( k = 0; k < rects; k++ )
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 );
1548 classifier->haar_feature[l].rect[k].r = r;
1550 sscanf( stage, "%s%n", str, &dl );
1553 classifier->haar_feature[l].tilted = strncmp( str, "tilted", 6 ) == 0;
1555 for( k = rects; k < CV_HAAR_FEATURE_MAX; k++ )
1557 memset( classifier->haar_feature[l].rect + k, 0,
1558 sizeof(classifier->haar_feature[l].rect[k]) );
1561 sscanf( stage, "%f%d%d%n", &(classifier->threshold[l]),
1562 &(classifier->left[l]),
1563 &(classifier->right[l]), &dl );
1566 for( l = 0; l <= classifier->count; l++ )
1568 sscanf( stage, "%f%n", &(classifier->alpha[l]), &dl );
1573 sscanf( stage, "%f%n", &threshold, &dl );
1576 cascade->stage_classifier[i].threshold = threshold;
1578 /* load tree links */
1579 if( sscanf( stage, "%d%d%n", &parent, &next, &dl ) != 2 )
1586 cascade->stage_classifier[i].parent = parent;
1587 cascade->stage_classifier[i].next = next;
1588 cascade->stage_classifier[i].child = -1;
1590 if( parent != -1 && cascade->stage_classifier[parent].child == -1 )
1592 cascade->stage_classifier[parent].child = i;
1600 #define _MAX_PATH 1024
1603 CV_IMPL CvHaarClassifierCascade*
1604 cvLoadHaarClassifierCascade( const char* directory, CvSize orig_window_size )
1606 const char** input_cascade = 0;
1607 CvHaarClassifierCascade *cascade = 0;
1609 CV_FUNCNAME( "cvLoadHaarClassifierCascade" );
1615 char name[_MAX_PATH];
1620 CV_ERROR( CV_StsNullPtr, "Null path is passed" );
1622 n = (int)strlen(directory)-1;
1623 slash = directory[n] == '\\' || directory[n] == '/' ? "" : "/";
1625 /* try to read the classifier from directory */
1628 sprintf( name, "%s%s%d/AdaBoostCARTHaarClassifier.txt", directory, slash, n );
1629 FILE* f = fopen( name, "rb" );
1632 fseek( f, 0, SEEK_END );
1633 size += ftell( f ) + 1;
1637 if( n == 0 && slash[0] )
1639 CV_CALL( cascade = (CvHaarClassifierCascade*)cvLoad( directory ));
1643 CV_ERROR( CV_StsBadArg, "Invalid path" );
1645 size += (n+1)*sizeof(char*);
1646 CV_CALL( input_cascade = (const char**)cvAlloc( size ));
1647 ptr = (char*)(input_cascade + n + 1);
1649 for( i = 0; i < n; i++ )
1651 sprintf( name, "%s/%d/AdaBoostCARTHaarClassifier.txt", directory, i );
1652 FILE* f = fopen( name, "rb" );
1654 CV_ERROR( CV_StsError, "" );
1655 fseek( f, 0, SEEK_END );
1657 fseek( f, 0, SEEK_SET );
1658 fread( ptr, 1, size, f );
1660 input_cascade[i] = ptr;
1665 input_cascade[n] = 0;
1666 cascade = icvLoadCascadeCART( input_cascade, n, orig_window_size );
1671 cvFree( &input_cascade );
1673 if( cvGetErrStatus() < 0 )
1674 cvReleaseHaarClassifierCascade( &cascade );
1681 cvReleaseHaarClassifierCascade( CvHaarClassifierCascade** _cascade )
1683 if( _cascade && *_cascade )
1686 CvHaarClassifierCascade* cascade = *_cascade;
1688 for( i = 0; i < cascade->count; i++ )
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 );
1694 icvReleaseHidHaarClassifierCascade( &cascade->hid_cascade );
1700 /****************************************************************************************\
1701 * Persistence functions *
1702 \****************************************************************************************/
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"
1722 icvIsHaarClassifier( const void* struct_ptr )
1724 return CV_IS_HAAR_CLASSIFIER( struct_ptr );
1728 icvReadHaarClassifier( CvFileStorage* fs, CvFileNode* node )
1730 CvHaarClassifierCascade* cascade = NULL;
1732 CV_FUNCNAME( "cvReadHaarClassifier" );
1737 CvFileNode* seq_fn = NULL; /* sequence */
1738 CvFileNode* fn = NULL;
1739 CvFileNode* stages_fn = NULL;
1740 CvSeqReader stages_reader;
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" );
1749 n = stages_fn->data.seq->total;
1750 CV_CALL( cascade = icvCreateHaarClassifierCascade(n) );
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;
1765 CV_CALL( cvStartReadSeq( stages_fn->data.seq, &stages_reader ) );
1766 for( i = 0; i < n; ++i )
1768 CvFileNode* stage_fn;
1769 CvFileNode* trees_fn;
1770 CvSeqReader trees_reader;
1772 stage_fn = (CvFileNode*) stages_reader.ptr;
1773 if( !CV_NODE_IS_MAP( stage_fn->tag ) )
1775 sprintf( buf, "Invalid stage %d", i );
1776 CV_ERROR( CV_StsError, buf );
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 )
1783 sprintf( buf, "Trees node is not a valid sequence. (stage %d)", i );
1784 CV_ERROR( CV_StsError, buf );
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 )
1792 cascade->stage_classifier[i].classifier[j].haar_feature = NULL;
1794 cascade->stage_classifier[i].count = trees_fn->data.seq->total;
1796 CV_CALL( cvStartReadSeq( trees_fn->data.seq, &trees_reader ) );
1797 for( j = 0; j < trees_fn->data.seq->total; ++j )
1799 CvFileNode* tree_fn;
1800 CvSeqReader tree_reader;
1801 CvHaarClassifier* classifier;
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 )
1808 sprintf( buf, "Tree node is not a valid sequence."
1809 " (stage %d, tree %d)", i, j );
1810 CV_ERROR( CV_StsError, buf );
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);
1825 CV_CALL( cvStartReadSeq( tree_fn->data.seq, &tree_reader ) );
1826 for( k = 0, last_idx = 0; k < tree_fn->data.seq->total; ++k )
1828 CvFileNode* node_fn;
1829 CvFileNode* feature_fn;
1830 CvFileNode* rects_fn;
1831 CvSeqReader rects_reader;
1833 node_fn = (CvFileNode*) tree_reader.ptr;
1834 if( !CV_NODE_IS_MAP( node_fn->tag ) )
1836 sprintf( buf, "Tree node %d is not a valid map. (stage %d, tree %d)",
1838 CV_ERROR( CV_StsError, buf );
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 ) )
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 );
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 )
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 );
1858 CV_CALL( cvStartReadSeq( rects_fn->data.seq, &rects_reader ) );
1859 for( l = 0; l < rects_fn->data.seq->total; ++l )
1861 CvFileNode* rect_fn;
1864 rect_fn = (CvFileNode*) rects_reader.ptr;
1865 if( !CV_NODE_IS_SEQ( rect_fn->tag ) || rect_fn->data.seq->total != 5 )
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 );
1872 fn = CV_SEQ_ELEM( rect_fn->data.seq, CvFileNode, 0 );
1873 if( !CV_NODE_IS_INT( fn->tag ) || fn->data.i < 0 )
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 );
1880 fn = CV_SEQ_ELEM( rect_fn->data.seq, CvFileNode, 1 );
1881 if( !CV_NODE_IS_INT( fn->tag ) || fn->data.i < 0 )
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 );
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 )
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 );
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 )
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 );
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 ) )
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 );
1916 classifier->haar_feature[k].rect[l].weight = (float) fn->data.f;
1917 classifier->haar_feature[k].rect[l].r = r;
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 )
1923 classifier->haar_feature[k].rect[l].weight = 0;
1924 classifier->haar_feature[k].rect[l].r = cvRect( 0, 0, 0, 0 );
1927 CV_CALL( fn = cvGetFileNodeByName( fs, feature_fn, ICV_HAAR_TILTED_NAME));
1928 if( !fn || !CV_NODE_IS_INT( fn->tag ) )
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 );
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 ) )
1938 sprintf( buf, "threshold must be real number. "
1939 "(stage %d, tree %d, node %d)", i, j, k );
1940 CV_ERROR( CV_StsError, buf );
1942 classifier->threshold[k] = (float) fn->data.f;
1943 CV_CALL( fn = cvGetFileNodeByName( fs, node_fn, ICV_HAAR_LEFT_NODE_NAME));
1946 if( !CV_NODE_IS_INT( fn->tag ) || fn->data.i <= k
1947 || fn->data.i >= tree_fn->data.seq->total )
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 );
1954 classifier->left[k] = fn->data.i;
1958 CV_CALL( fn = cvGetFileNodeByName( fs, node_fn,
1959 ICV_HAAR_LEFT_VAL_NAME ) );
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 );
1966 if( !CV_NODE_IS_REAL( fn->tag ) )
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 );
1973 if( last_idx >= classifier->count + 1 )
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 );
1979 classifier->left[k] = -last_idx;
1980 classifier->alpha[last_idx++] = (float) fn->data.f;
1982 CV_CALL( fn = cvGetFileNodeByName( fs, node_fn,ICV_HAAR_RIGHT_NODE_NAME));
1985 if( !CV_NODE_IS_INT( fn->tag ) || fn->data.i <= k
1986 || fn->data.i >= tree_fn->data.seq->total )
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 );
1993 classifier->right[k] = fn->data.i;
1997 CV_CALL( fn = cvGetFileNodeByName( fs, node_fn,
1998 ICV_HAAR_RIGHT_VAL_NAME ) );
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 );
2005 if( !CV_NODE_IS_REAL( fn->tag ) )
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 );
2012 if( last_idx >= classifier->count + 1 )
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 );
2018 classifier->right[k] = -last_idx;
2019 classifier->alpha[last_idx++] = (float) fn->data.f;
2022 CV_NEXT_SEQ_ELEM( sizeof( *node_fn ), tree_reader );
2023 } /* for each node */
2024 if( last_idx != classifier->count + 1 )
2026 sprintf( buf, "Tree structure is broken: too few values. "
2027 "(stage %d, tree %d)", i, j );
2028 CV_ERROR( CV_StsError, buf );
2031 CV_NEXT_SEQ_ELEM( sizeof( *tree_fn ), trees_reader );
2032 } /* for each tree */
2034 CV_CALL( fn = cvGetFileNodeByName( fs, stage_fn, ICV_HAAR_STAGE_THRESHOLD_NAME));
2035 if( !fn || !CV_NODE_IS_REAL( fn->tag ) )
2037 sprintf( buf, "stage threshold must be real number. (stage %d)", i );
2038 CV_ERROR( CV_StsError, buf );
2040 cascade->stage_classifier[i].threshold = (float) fn->data.f;
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 )
2049 sprintf( buf, "parent must be integer number. (stage %d)", i );
2050 CV_ERROR( CV_StsError, buf );
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 )
2057 sprintf( buf, "next must be integer number. (stage %d)", i );
2058 CV_ERROR( CV_StsError, buf );
2062 cascade->stage_classifier[i].parent = parent;
2063 cascade->stage_classifier[i].next = next;
2064 cascade->stage_classifier[i].child = -1;
2066 if( parent != -1 && cascade->stage_classifier[parent].child == -1 )
2068 cascade->stage_classifier[parent].child = i;
2071 CV_NEXT_SEQ_ELEM( sizeof( *stage_fn ), stages_reader );
2072 } /* for each stage */
2076 if( cvGetErrStatus() < 0 )
2078 cvReleaseHaarClassifierCascade( &cascade );
2086 icvWriteHaarClassifier( CvFileStorage* fs, const char* name, const void* struct_ptr,
2087 CvAttrList attributes )
2089 CV_FUNCNAME( "cvWriteHaarClassifier" );
2095 const CvHaarClassifierCascade* cascade = (const CvHaarClassifierCascade*) struct_ptr;
2097 /* TODO: parameters check */
2099 CV_CALL( cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_HAAR, attributes ) );
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 */
2106 CV_CALL( cvStartWriteStruct( fs, ICV_HAAR_STAGES_NAME, CV_NODE_SEQ ) );
2107 for( i = 0; i < cascade->count; ++i )
2109 CV_CALL( cvStartWriteStruct( fs, NULL, CV_NODE_MAP ) );
2110 sprintf( buf, "stage %d", i );
2111 CV_CALL( cvWriteComment( fs, buf, 1 ) );
2113 CV_CALL( cvStartWriteStruct( fs, ICV_HAAR_TREES_NAME, CV_NODE_SEQ ) );
2115 for( j = 0; j < cascade->stage_classifier[i].count; ++j )
2117 CvHaarClassifier* tree = &cascade->stage_classifier[i].classifier[j];
2119 CV_CALL( cvStartWriteStruct( fs, NULL, CV_NODE_SEQ ) );
2120 sprintf( buf, "tree %d", j );
2121 CV_CALL( cvWriteComment( fs, buf, 1 ) );
2123 for( k = 0; k < tree->count; ++k )
2125 CvHaarFeature* feature = &tree->haar_feature[k];
2127 CV_CALL( cvStartWriteStruct( fs, NULL, CV_NODE_MAP ) );
2130 sprintf( buf, "node %d", k );
2134 sprintf( buf, "root node" );
2136 CV_CALL( cvWriteComment( fs, buf, 1 ) );
2138 CV_CALL( cvStartWriteStruct( fs, ICV_HAAR_FEATURE_NAME, CV_NODE_MAP ) );
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 )
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 */
2151 CV_CALL( cvEndWriteStruct( fs ) ); /* rects */
2152 CV_CALL( cvWriteInt( fs, ICV_HAAR_TILTED_NAME, feature->tilted ) );
2153 CV_CALL( cvEndWriteStruct( fs ) ); /* feature */
2155 CV_CALL( cvWriteReal( fs, ICV_HAAR_THRESHOLD_NAME, tree->threshold[k]) );
2157 if( tree->left[k] > 0 )
2159 CV_CALL( cvWriteInt( fs, ICV_HAAR_LEFT_NODE_NAME, tree->left[k] ) );
2163 CV_CALL( cvWriteReal( fs, ICV_HAAR_LEFT_VAL_NAME,
2164 tree->alpha[-tree->left[k]] ) );
2167 if( tree->right[k] > 0 )
2169 CV_CALL( cvWriteInt( fs, ICV_HAAR_RIGHT_NODE_NAME, tree->right[k] ) );
2173 CV_CALL( cvWriteReal( fs, ICV_HAAR_RIGHT_VAL_NAME,
2174 tree->alpha[-tree->right[k]] ) );
2177 CV_CALL( cvEndWriteStruct( fs ) ); /* split */
2180 CV_CALL( cvEndWriteStruct( fs ) ); /* tree */
2183 CV_CALL( cvEndWriteStruct( fs ) ); /* trees */
2185 CV_CALL( cvWriteReal( fs, ICV_HAAR_STAGE_THRESHOLD_NAME,
2186 cascade->stage_classifier[i].threshold) );
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 ) );
2193 CV_CALL( cvEndWriteStruct( fs ) ); /* stage */
2194 } /* for each stage */
2196 CV_CALL( cvEndWriteStruct( fs ) ); /* stages */
2197 CV_CALL( cvEndWriteStruct( fs ) ); /* root */
2203 icvCloneHaarClassifier( const void* struct_ptr )
2205 CvHaarClassifierCascade* cascade = NULL;
2207 CV_FUNCNAME( "cvCloneHaarClassifier" );
2212 const CvHaarClassifierCascade* cascade_src =
2213 (const CvHaarClassifierCascade*) struct_ptr;
2215 n = cascade_src->count;
2216 CV_CALL( cascade = icvCreateHaarClassifierCascade(n) );
2217 cascade->orig_window_size = cascade_src->orig_window_size;
2219 for( i = 0; i < n; ++i )
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;
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] ) ) );
2231 cascade->stage_classifier[i].count = cascade_src->stage_classifier[i].count;
2233 for( j = 0; j < cascade->stage_classifier[i].count; ++j )
2235 cascade->stage_classifier[i].classifier[j].haar_feature = NULL;
2238 for( j = 0; j < cascade->stage_classifier[i].count; ++j )
2240 const CvHaarClassifier* classifier_src =
2241 &cascade_src->stage_classifier[i].classifier[j];
2242 CvHaarClassifier* classifier =
2243 &cascade->stage_classifier[i].classifier[j];
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 )
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];
2264 classifier->alpha[classifier->count] =
2265 classifier_src->alpha[classifier->count];
2275 CvType haar_type( CV_TYPE_NAME_HAAR, icvIsHaarClassifier,
2276 (CvReleaseFunc)cvReleaseHaarClassifierCascade,
2277 icvReadHaarClassifier, icvWriteHaarClassifier,
2278 icvCloneHaarClassifier );