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.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
18 // Niko Li, newlife20080214@gmail.com
19 // Wang Weiyan, wangweiyanster@gmail.com
20 // Jia Haipeng, jiahaipeng95@gmail.com
21 // Wu Xinglong, wxl370@126.com
22 // Wang Yao, bitwangyaoyao@gmail.com
24 // Redistribution and use in source and binary forms, with or without modification,
25 // are permitted provided that the following conditions are met:
27 // * Redistribution's of source code must retain the above copyright notice,
28 // this list of conditions and the following disclaimer.
30 // * Redistribution's in binary form must reproduce the above copyright notice,
31 // this list of conditions and the following disclaimer in the documentation
32 // and/or other oclMaterials provided with the distribution.
34 // * The name of the copyright holders may not be used to endorse or promote products
35 // derived from this software without specific prior written permission.
37 // This software is provided by the copyright holders and contributors "as is" and
38 // any express or implied warranties, including, but not limited to, the implied
39 // warranties of merchantability and fitness for a particular purpose are disclaimed.
40 // In no event shall the Intel Corporation or contributors be liable for any direct,
41 // indirect, incidental, special, exemplary, or consequential damages
42 // (including, but not limited to, procurement of substitute goods or services;
43 // loss of use, data, or profits; or business interruption) however caused
44 // and on any theory of liability, whether in contract, strict liability,
45 // or tort (including negligence or otherwise) arising in any way out of
46 // the use of this software, even if advised of the possibility of such damage.
50 #include "precomp.hpp"
51 #include "opencl_kernels.hpp"
54 using namespace cv::ocl;
56 /* these settings affect the quality of detection: change with care */
57 #define CV_ADJUST_FEATURES 1
58 #define CV_ADJUST_WEIGHTS 0
59 #define CV_HAAR_FEATURE_MAX 3
61 typedef double sqsumtype;
63 typedef struct CvHidHaarFeature
67 sumtype *p0, *p1, *p2, *p3;
70 rect[CV_HAAR_FEATURE_MAX];
75 typedef struct CvHidHaarTreeNode
77 CvHidHaarFeature feature;
85 typedef struct CvHidHaarClassifier
88 //CvHaarFeature* orig_feature;
89 CvHidHaarTreeNode *node;
95 typedef struct CvHidHaarStageClassifier
99 CvHidHaarClassifier *classifier;
102 struct CvHidHaarStageClassifier *next;
103 struct CvHidHaarStageClassifier *child;
104 struct CvHidHaarStageClassifier *parent;
106 CvHidHaarStageClassifier;
109 struct CvHidHaarClassifierCascade
113 int has_tilted_features;
115 double inv_window_area;
116 CvMat sum, sqsum, tilted;
117 CvHidHaarStageClassifier *stage_classifier;
118 sqsumtype *pq0, *pq1, *pq2, *pq3;
119 sumtype *p0, *p1, *p2, *p3;
126 int grpnumperline_totalgrp;
129 } detect_piramid_info;
131 #define _ALIGNED_ON(_ALIGNMENT) __declspec(align(_ALIGNMENT))
133 typedef _ALIGNED_ON(128) struct GpuHidHaarTreeNode
135 _ALIGNED_ON(64) int p[CV_HAAR_FEATURE_MAX][4];
136 float weight[CV_HAAR_FEATURE_MAX] ;
138 _ALIGNED_ON(16) float alpha[3] ;
139 _ALIGNED_ON(4) int left ;
140 _ALIGNED_ON(4) int right ;
145 typedef _ALIGNED_ON(32) struct GpuHidHaarClassifier
147 _ALIGNED_ON(4) int count;
148 _ALIGNED_ON(8) GpuHidHaarTreeNode *node ;
149 _ALIGNED_ON(8) float *alpha ;
151 GpuHidHaarClassifier;
154 typedef _ALIGNED_ON(64) struct GpuHidHaarStageClassifier
156 _ALIGNED_ON(4) int count ;
157 _ALIGNED_ON(4) float threshold ;
158 _ALIGNED_ON(4) int two_rects ;
159 _ALIGNED_ON(8) GpuHidHaarClassifier *classifier ;
160 _ALIGNED_ON(8) struct GpuHidHaarStageClassifier *next;
161 _ALIGNED_ON(8) struct GpuHidHaarStageClassifier *child ;
162 _ALIGNED_ON(8) struct GpuHidHaarStageClassifier *parent ;
164 GpuHidHaarStageClassifier;
167 typedef _ALIGNED_ON(64) struct GpuHidHaarClassifierCascade
169 _ALIGNED_ON(4) int count ;
170 _ALIGNED_ON(4) int is_stump_based ;
171 _ALIGNED_ON(4) int has_tilted_features ;
172 _ALIGNED_ON(4) int is_tree ;
173 _ALIGNED_ON(4) int pq0 ;
174 _ALIGNED_ON(4) int pq1 ;
175 _ALIGNED_ON(4) int pq2 ;
176 _ALIGNED_ON(4) int pq3 ;
177 _ALIGNED_ON(4) int p0 ;
178 _ALIGNED_ON(4) int p1 ;
179 _ALIGNED_ON(4) int p2 ;
180 _ALIGNED_ON(4) int p3 ;
181 _ALIGNED_ON(4) float inv_window_area ;
182 } GpuHidHaarClassifierCascade;
184 #define _ALIGNED_ON(_ALIGNMENT) __attribute__((aligned(_ALIGNMENT) ))
186 typedef struct _ALIGNED_ON(128) GpuHidHaarTreeNode
188 int p[CV_HAAR_FEATURE_MAX][4] _ALIGNED_ON(64);
189 float weight[CV_HAAR_FEATURE_MAX];// _ALIGNED_ON(16);
190 float threshold;// _ALIGNED_ON(4);
191 float alpha[3] _ALIGNED_ON(16);
192 int left _ALIGNED_ON(4);
193 int right _ALIGNED_ON(4);
197 typedef struct _ALIGNED_ON(32) GpuHidHaarClassifier
199 int count _ALIGNED_ON(4);
200 GpuHidHaarTreeNode *node _ALIGNED_ON(8);
201 float *alpha _ALIGNED_ON(8);
203 GpuHidHaarClassifier;
206 typedef struct _ALIGNED_ON(64) GpuHidHaarStageClassifier
208 int count _ALIGNED_ON(4);
209 float threshold _ALIGNED_ON(4);
210 int two_rects _ALIGNED_ON(4);
211 GpuHidHaarClassifier *classifier _ALIGNED_ON(8);
212 struct GpuHidHaarStageClassifier *next _ALIGNED_ON(8);
213 struct GpuHidHaarStageClassifier *child _ALIGNED_ON(8);
214 struct GpuHidHaarStageClassifier *parent _ALIGNED_ON(8);
216 GpuHidHaarStageClassifier;
219 typedef struct _ALIGNED_ON(64) GpuHidHaarClassifierCascade
221 int count _ALIGNED_ON(4);
222 int is_stump_based _ALIGNED_ON(4);
223 int has_tilted_features _ALIGNED_ON(4);
224 int is_tree _ALIGNED_ON(4);
225 int pq0 _ALIGNED_ON(4);
226 int pq1 _ALIGNED_ON(4);
227 int pq2 _ALIGNED_ON(4);
228 int pq3 _ALIGNED_ON(4);
229 int p0 _ALIGNED_ON(4);
230 int p1 _ALIGNED_ON(4);
231 int p2 _ALIGNED_ON(4);
232 int p3 _ALIGNED_ON(4);
233 float inv_window_area _ALIGNED_ON(4);
234 } GpuHidHaarClassifierCascade;
237 const int icv_object_win_border = 1;
238 const float icv_stage_threshold_bias = 0.0001f;
239 double globaltime = 0;
241 /* create more efficient internal representation of haar classifier cascade */
242 static GpuHidHaarClassifierCascade * gpuCreateHidHaarClassifierCascade( CvHaarClassifierCascade *cascade, int *size, int *totalclassifier)
244 GpuHidHaarClassifierCascade *out = 0;
248 int total_classifiers = 0;
252 GpuHidHaarStageClassifier *stage_classifier_ptr;
253 GpuHidHaarClassifier *haar_classifier_ptr;
254 GpuHidHaarTreeNode *haar_node_ptr;
256 CvSize orig_window_size;
257 int has_tilted_features = 0;
259 if( !CV_IS_HAAR_CLASSIFIER(cascade) )
260 CV_Error( !cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier pointer" );
262 if( cascade->hid_cascade )
263 CV_Error( CV_StsError, "hid_cascade has been already created" );
265 if( !cascade->stage_classifier )
266 CV_Error( CV_StsNullPtr, "" );
268 if( cascade->count <= 0 )
269 CV_Error( CV_StsOutOfRange, "Negative number of cascade stages" );
271 orig_window_size = cascade->orig_window_size;
273 /* check input structure correctness and calculate total memory size needed for
274 internal representation of the classifier cascade */
275 for( i = 0; i < cascade->count; i++ )
277 CvHaarStageClassifier *stage_classifier = cascade->stage_classifier + i;
279 if( !stage_classifier->classifier ||
280 stage_classifier->count <= 0 )
282 sprintf( errorstr, "header of the stage classifier #%d is invalid "
283 "(has null pointers or non-positive classfier count)", i );
284 CV_Error( CV_StsError, errorstr );
287 total_classifiers += stage_classifier->count;
289 for( j = 0; j < stage_classifier->count; j++ )
291 CvHaarClassifier *classifier = stage_classifier->classifier + j;
293 total_nodes += classifier->count;
294 for( l = 0; l < classifier->count; l++ )
296 for( k = 0; k < CV_HAAR_FEATURE_MAX; k++ )
298 if( classifier->haar_feature[l].rect[k].r.width )
300 CvRect r = classifier->haar_feature[l].rect[k].r;
301 int tilted = classifier->haar_feature[l].tilted;
302 has_tilted_features |= tilted != 0;
303 if( r.width < 0 || r.height < 0 || r.y < 0 ||
304 r.x + r.width > orig_window_size.width
307 (r.x < 0 || r.y + r.height > orig_window_size.height))
309 (tilted && (r.x - r.height < 0 ||
310 r.y + r.width + r.height > orig_window_size.height)))
312 sprintf( errorstr, "rectangle #%d of the classifier #%d of "
313 "the stage classifier #%d is not inside "
314 "the reference (original) cascade window", k, j, i );
315 CV_Error( CV_StsNullPtr, errorstr );
323 // this is an upper boundary for the whole hidden cascade size
324 datasize = sizeof(GpuHidHaarClassifierCascade) +
325 sizeof(GpuHidHaarStageClassifier) * cascade->count +
326 sizeof(GpuHidHaarClassifier) * total_classifiers +
327 sizeof(GpuHidHaarTreeNode) * total_nodes;
329 *totalclassifier = total_classifiers;
331 out = (GpuHidHaarClassifierCascade *)cvAlloc( datasize );
332 memset( out, 0, sizeof(*out) );
335 out->count = cascade->count;
336 stage_classifier_ptr = (GpuHidHaarStageClassifier *)(out + 1);
337 haar_classifier_ptr = (GpuHidHaarClassifier *)(stage_classifier_ptr + cascade->count);
338 haar_node_ptr = (GpuHidHaarTreeNode *)(haar_classifier_ptr + total_classifiers);
340 out->is_stump_based = 1;
341 out->has_tilted_features = has_tilted_features;
344 /* initialize internal representation */
345 for( i = 0; i < cascade->count; i++ )
347 CvHaarStageClassifier *stage_classifier = cascade->stage_classifier + i;
348 GpuHidHaarStageClassifier *hid_stage_classifier = stage_classifier_ptr + i;
350 hid_stage_classifier->count = stage_classifier->count;
351 hid_stage_classifier->threshold = stage_classifier->threshold - icv_stage_threshold_bias;
352 hid_stage_classifier->classifier = haar_classifier_ptr;
353 hid_stage_classifier->two_rects = 1;
354 haar_classifier_ptr += stage_classifier->count;
356 for( j = 0; j < stage_classifier->count; j++ )
358 CvHaarClassifier *classifier = stage_classifier->classifier + j;
359 GpuHidHaarClassifier *hid_classifier = hid_stage_classifier->classifier + j;
360 int node_count = classifier->count;
362 float *alpha_ptr = &haar_node_ptr->alpha[0];
364 hid_classifier->count = node_count;
365 hid_classifier->node = haar_node_ptr;
366 hid_classifier->alpha = alpha_ptr;
368 for( l = 0; l < node_count; l++ )
370 GpuHidHaarTreeNode *node = hid_classifier->node + l;
371 CvHaarFeature *feature = classifier->haar_feature + l;
373 memset( node, -1, sizeof(*node) );
374 node->threshold = classifier->threshold[l];
375 node->left = classifier->left[l];
376 node->right = classifier->right[l];
378 if( fabs(feature->rect[2].weight) < DBL_EPSILON ||
379 feature->rect[2].r.width == 0 ||
380 feature->rect[2].r.height == 0 )
389 hid_stage_classifier->two_rects = 0;
391 memcpy( node->alpha, classifier->alpha, (node_count + 1)*sizeof(alpha_ptr[0]));
392 haar_node_ptr = haar_node_ptr + 1;
394 out->is_stump_based &= node_count == 1;
398 cascade->hid_cascade = (CvHidHaarClassifierCascade *)out;
399 assert( (char *)haar_node_ptr - (char *)out <= datasize );
405 #define sum_elem_ptr(sum,row,col) \
406 ((sumtype*)CV_MAT_ELEM_PTR_FAST((sum),(row),(col),sizeof(sumtype)))
408 #define sqsum_elem_ptr(sqsum,row,col) \
409 ((sqsumtype*)CV_MAT_ELEM_PTR_FAST((sqsum),(row),(col),sizeof(sqsumtype)))
411 #define calc_sum(rect,offset) \
412 ((rect).p0[offset] - (rect).p1[offset] - (rect).p2[offset] + (rect).p3[offset])
415 static void gpuSetImagesForHaarClassifierCascade( CvHaarClassifierCascade *_cascade,
419 GpuHidHaarClassifierCascade *cascade;
420 int coi0 = 0, coi1 = 0;
426 GpuHidHaarStageClassifier *stage_classifier;
428 if( !CV_IS_HAAR_CLASSIFIER(_cascade) )
429 CV_Error( !_cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier pointer" );
432 CV_Error( CV_StsOutOfRange, "Scale must be positive" );
435 CV_Error( CV_BadCOI, "COI is not supported" );
437 if( !_cascade->hid_cascade )
438 gpuCreateHidHaarClassifierCascade(_cascade, &datasize, &total);
440 cascade = (GpuHidHaarClassifierCascade *) _cascade->hid_cascade;
441 stage_classifier = (GpuHidHaarStageClassifier *) (cascade + 1);
443 _cascade->scale = scale;
444 _cascade->real_window_size.width = cvRound( _cascade->orig_window_size.width * scale );
445 _cascade->real_window_size.height = cvRound( _cascade->orig_window_size.height * scale );
447 equRect.x = equRect.y = cvRound(scale);
448 equRect.width = cvRound((_cascade->orig_window_size.width - 2) * scale);
449 equRect.height = cvRound((_cascade->orig_window_size.height - 2) * scale);
450 weight_scale = 1. / (equRect.width * equRect.height);
451 cascade->inv_window_area = weight_scale;
453 cascade->pq0 = equRect.x;
454 cascade->pq1 = equRect.y;
455 cascade->pq2 = equRect.x + equRect.width;
456 cascade->pq3 = equRect.y + equRect.height;
458 cascade->p0 = equRect.x;
459 cascade->p1 = equRect.y;
460 cascade->p2 = equRect.x + equRect.width;
461 cascade->p3 = equRect.y + equRect.height;
464 /* init pointers in haar features according to real window size and
465 given image pointers */
466 for( i = 0; i < _cascade->count; i++ )
469 for( j = 0; j < stage_classifier[i].count; j++ )
471 for( l = 0; l < stage_classifier[i].classifier[j].count; l++ )
473 CvHaarFeature *feature =
474 &_cascade->stage_classifier[i].classifier[j].haar_feature[l];
475 GpuHidHaarTreeNode *hidnode = &stage_classifier[i].classifier[j].node[l];
476 double sum0 = 0, area0 = 0;
479 int base_w = -1, base_h = -1;
480 int new_base_w = 0, new_base_h = 0;
482 int flagx = 0, flagy = 0;
487 for( k = 0; k < CV_HAAR_FEATURE_MAX; k++ )
489 if(!hidnode->p[k][0])
491 r[k] = feature->rect[k].r;
492 base_w = (int)CV_IMIN( (unsigned)base_w, (unsigned)(r[k].width - 1) );
493 base_w = (int)CV_IMIN( (unsigned)base_w, (unsigned)(r[k].x - r[0].x - 1) );
494 base_h = (int)CV_IMIN( (unsigned)base_h, (unsigned)(r[k].height - 1) );
495 base_h = (int)CV_IMIN( (unsigned)base_h, (unsigned)(r[k].y - r[0].y - 1) );
503 kx = r[0].width / base_w;
506 ky = r[0].height / base_h;
511 new_base_w = cvRound( r[0].width * scale ) / kx;
512 x0 = cvRound( r[0].x * scale );
518 new_base_h = cvRound( r[0].height * scale ) / ky;
519 y0 = cvRound( r[0].y * scale );
522 for( k = 0; k < nr; k++ )
525 double correction_ratio;
529 tr.x = (r[k].x - r[0].x) * new_base_w / base_w + x0;
530 tr.width = r[k].width * new_base_w / base_w;
534 tr.x = cvRound( r[k].x * scale );
535 tr.width = cvRound( r[k].width * scale );
540 tr.y = (r[k].y - r[0].y) * new_base_h / base_h + y0;
541 tr.height = r[k].height * new_base_h / base_h;
545 tr.y = cvRound( r[k].y * scale );
546 tr.height = cvRound( r[k].height * scale );
549 #if CV_ADJUST_WEIGHTS
552 const float orig_feature_size = (float)(feature->rect[k].r.width) * feature->rect[k].r.height;
553 const float orig_norm_size = (float)(_cascade->orig_window_size.width) * (_cascade->orig_window_size.height);
554 const float feature_size = float(tr.width * tr.height);
555 //const float normSize = float(equRect.width*equRect.height);
556 float target_ratio = orig_feature_size / orig_norm_size;
557 //float isRatio = featureSize / normSize;
558 //correctionRatio = targetRatio / isRatio / normSize;
559 correction_ratio = target_ratio / feature_size;
563 correction_ratio = weight_scale * (!feature->tilted ? 1 : 0.5);
566 if( !feature->tilted )
568 hidnode->p[k][0] = tr.x;
569 hidnode->p[k][1] = tr.y;
570 hidnode->p[k][2] = tr.x + tr.width;
571 hidnode->p[k][3] = tr.y + tr.height;
575 hidnode->p[k][2] = (tr.y + tr.width) * step + tr.x + tr.width;
576 hidnode->p[k][3] = (tr.y + tr.width + tr.height) * step + tr.x + tr.width - tr.height;
577 hidnode->p[k][0] = tr.y * step + tr.x;
578 hidnode->p[k][1] = (tr.y + tr.height) * step + tr.x - tr.height;
580 hidnode->weight[k] = (float)(feature->rect[k].weight * correction_ratio);
582 area0 = tr.width * tr.height;
584 sum0 += hidnode->weight[k] * tr.width * tr.height;
586 hidnode->weight[0] = (float)(-sum0 / area0);
592 static void gpuSetHaarClassifierCascade( CvHaarClassifierCascade *_cascade)
594 GpuHidHaarClassifierCascade *cascade;
600 GpuHidHaarStageClassifier *stage_classifier;
602 if( !CV_IS_HAAR_CLASSIFIER(_cascade) )
603 CV_Error( !_cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier pointer" );
605 if( !_cascade->hid_cascade )
606 gpuCreateHidHaarClassifierCascade(_cascade, &datasize, &total);
608 cascade = (GpuHidHaarClassifierCascade *) _cascade->hid_cascade;
609 stage_classifier = (GpuHidHaarStageClassifier *) cascade + 1;
611 _cascade->scale = 1.0;
612 _cascade->real_window_size.width = _cascade->orig_window_size.width ;
613 _cascade->real_window_size.height = _cascade->orig_window_size.height;
615 equRect.x = equRect.y = 1;
616 equRect.width = _cascade->orig_window_size.width - 2;
617 equRect.height = _cascade->orig_window_size.height - 2;
619 cascade->inv_window_area = weight_scale;
621 cascade->p0 = equRect.x;
622 cascade->p1 = equRect.y;
623 cascade->p2 = equRect.height;
624 cascade->p3 = equRect.width ;
625 for( i = 0; i < _cascade->count; i++ )
628 for( j = 0; j < stage_classifier[i].count; j++ )
630 for( l = 0; l < stage_classifier[i].classifier[j].count; l++ )
632 CvHaarFeature *feature =
633 &_cascade->stage_classifier[i].classifier[j].haar_feature[l];
634 GpuHidHaarTreeNode *hidnode = &stage_classifier[i].classifier[j].node[l];
641 for( k = 0; k < CV_HAAR_FEATURE_MAX; k++ )
643 if(!hidnode->p[k][0])
645 r[k] = feature->rect[k].r;
649 for( k = 0; k < nr; k++ )
652 double correction_ratio;
654 tr.width = r[k].width;
656 tr.height = r[k].height;
657 correction_ratio = weight_scale * (!feature->tilted ? 1 : 0.5);
658 hidnode->p[k][0] = tr.x;
659 hidnode->p[k][1] = tr.y;
660 hidnode->p[k][2] = tr.width;
661 hidnode->p[k][3] = tr.height;
662 hidnode->weight[k] = (float)(feature->rect[k].weight * correction_ratio);
668 void OclCascadeClassifier::detectMultiScale(oclMat &gimg, CV_OUT std::vector<cv::Rect>& faces,
669 double scaleFactor, int minNeighbors, int flags,
670 Size minSize, Size maxSize)
671 //CvSeq *cv::ocl::OclCascadeClassifier::oclHaarDetectObjects( oclMat &gimg, CvMemStorage *storage, double scaleFactor,
672 // int minNeighbors, int flags, CvSize minSize, CvSize maxSize)
674 CvHaarClassifierCascade *cascade = oldCascade;
676 const double GROUP_EPS = 0.2;
678 cv::ConcurrentRectVector allCandidates;
679 std::vector<cv::Rect> rectList;
680 std::vector<int> rweights;
683 int totalclassifier=0;
685 GpuHidHaarClassifierCascade *gcascade;
686 GpuHidHaarStageClassifier *stage;
687 GpuHidHaarClassifier *classifier;
688 GpuHidHaarTreeNode *node;
693 bool findBiggestObject = (flags & CV_HAAR_FIND_BIGGEST_OBJECT) != 0;
695 if( maxSize.height == 0 || maxSize.width == 0 )
697 maxSize.height = gimg.rows;
698 maxSize.width = gimg.cols;
701 if( !CV_IS_HAAR_CLASSIFIER(cascade) )
702 CV_Error( !cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier cascade" );
705 // CV_Error( CV_StsNullPtr, "Null storage pointer" );
707 if( CV_MAT_DEPTH(gimg.type()) != CV_8U )
708 CV_Error( CV_StsUnsupportedFormat, "Only 8-bit images are supported" );
710 if( scaleFactor <= 1 )
711 CV_Error( CV_StsOutOfRange, "scale factor must be > 1" );
713 if( findBiggestObject )
714 flags &= ~CV_HAAR_SCALE_IMAGE;
716 if( !cascade->hid_cascade )
717 gpuCreateHidHaarClassifierCascade(cascade, &datasize, &totalclassifier);
719 //result_seq = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvAvgComp), storage );
721 if( CV_MAT_CN(gimg.type()) > 1 )
724 cvtColor( gimg, gtemp, COLOR_BGR2GRAY );
728 if( findBiggestObject )
729 flags &= ~(CV_HAAR_SCALE_IMAGE | CV_HAAR_DO_CANNY_PRUNING);
731 if( gimg.cols < minSize.width || gimg.rows < minSize.height )
732 CV_Error(CV_StsError, "Image too small");
734 cl_command_queue qu = getClCommandQueue(Context::getContext());
735 if( (flags & CV_HAAR_SCALE_IMAGE) )
737 CvSize winSize0 = cascade->orig_window_size;
741 std::vector<CvSize> sizev;
742 std::vector<float> scalev;
743 for(factor = 1.f;; factor *= scaleFactor)
745 CvSize winSize( cvRound(winSize0.width * factor), cvRound(winSize0.height * factor) );
746 sz.width = cvRound( gimg.cols / factor ) + 1;
747 sz.height = cvRound( gimg.rows / factor ) + 1;
748 CvSize sz1( sz.width - winSize0.width - 1, sz.height - winSize0.height - 1 );
750 if( sz1.width <= 0 || sz1.height <= 0 )
752 if( winSize.width > maxSize.width || winSize.height > maxSize.height )
754 if( winSize.width < minSize.width || winSize.height < minSize.height )
757 totalheight += sz.height;
759 scalev.push_back(factor);
762 oclMat gimg1(gimg.rows, gimg.cols, CV_8UC1);
763 oclMat gsum(totalheight + 4, gimg.cols + 1, CV_32SC1);
764 oclMat gsqsum(totalheight + 4, gimg.cols + 1, CV_32FC1);
768 cl_mem candidatebuffer;
769 cl_mem scaleinfobuffer;
771 cv::Mat imgroi, imgroisq;
772 cv::ocl::oclMat resizeroi, gimgroi, gimgroisq;
775 size_t blocksize = 8;
776 size_t localThreads[3] = { blocksize, blocksize , 1 };
777 size_t globalThreads[3] = { grp_per_CU *(gsum.clCxt->getDeviceInfo().maxComputeUnits) *localThreads[0],
780 int outputsz = 256 * globalThreads[0] / localThreads[0];
781 int loopcount = sizev.size();
782 detect_piramid_info *scaleinfo = (detect_piramid_info *)malloc(sizeof(detect_piramid_info) * loopcount);
784 for( int i = 0; i < loopcount; i++ )
788 roi = Rect(0, indexy, sz.width, sz.height);
789 roi2 = Rect(0, 0, sz.width - 1, sz.height - 1);
790 resizeroi = gimg1(roi2);
792 gimgroisq = gsqsum(roi);
793 int width = gimgroi.cols - 1 - cascade->orig_window_size.width;
794 int height = gimgroi.rows - 1 - cascade->orig_window_size.height;
795 scaleinfo[i].width_height = (width << 16) | height;
798 int grpnumperline = (width + localThreads[0] - 1) / localThreads[0];
799 int totalgrp = ((height + localThreads[1] - 1) / localThreads[1]) * grpnumperline;
801 scaleinfo[i].grpnumperline_totalgrp = (grpnumperline << 16) | totalgrp;
802 scaleinfo[i].imgoff = gimgroi.offset >> 2;
803 scaleinfo[i].factor = factor;
804 cv::ocl::resize(gimg, resizeroi, Size(sz.width - 1, sz.height - 1), 0, 0, INTER_LINEAR);
805 cv::ocl::integral(resizeroi, gimgroi, gimgroisq);
809 gcascade = (GpuHidHaarClassifierCascade *)cascade->hid_cascade;
810 stage = (GpuHidHaarStageClassifier *)(gcascade + 1);
811 classifier = (GpuHidHaarClassifier *)(stage + gcascade->count);
812 node = (GpuHidHaarTreeNode *)(classifier->node);
814 int nodenum = (datasize - sizeof(GpuHidHaarClassifierCascade) -
815 sizeof(GpuHidHaarStageClassifier) * gcascade->count - sizeof(GpuHidHaarClassifier) * totalclassifier) / sizeof(GpuHidHaarTreeNode);
817 candidate = (int *)malloc(4 * sizeof(int) * outputsz);
819 gpuSetImagesForHaarClassifierCascade( cascade, 1., gsum.step / 4 );
821 stagebuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_READ_ONLY, sizeof(GpuHidHaarStageClassifier) * gcascade->count);
822 openCLSafeCall(clEnqueueWriteBuffer(qu, stagebuffer, 1, 0, sizeof(GpuHidHaarStageClassifier)*gcascade->count, stage, 0, NULL, NULL));
824 nodebuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_READ_ONLY, nodenum * sizeof(GpuHidHaarTreeNode));
826 openCLSafeCall(clEnqueueWriteBuffer(qu, nodebuffer, 1, 0, nodenum * sizeof(GpuHidHaarTreeNode),
827 node, 0, NULL, NULL));
828 candidatebuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_WRITE_ONLY, 4 * sizeof(int) * outputsz);
830 scaleinfobuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_READ_ONLY, sizeof(detect_piramid_info) * loopcount);
831 openCLSafeCall(clEnqueueWriteBuffer(qu, scaleinfobuffer, 1, 0, sizeof(detect_piramid_info)*loopcount, scaleinfo, 0, NULL, NULL));
834 int endstage = gcascade->count;
836 int pixelstep = gsum.step / 4;
838 int splitnode = stage[0].count + stage[1].count + stage[2].count;
840 p.s[0] = gcascade->p0;
841 p.s[1] = gcascade->p1;
842 p.s[2] = gcascade->p2;
843 p.s[3] = gcascade->p3;
844 pq.s[0] = gcascade->pq0;
845 pq.s[1] = gcascade->pq1;
846 pq.s[2] = gcascade->pq2;
847 pq.s[3] = gcascade->pq3;
848 float correction = gcascade->inv_window_area;
850 std::vector<std::pair<size_t, const void *> > args;
851 args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&stagebuffer ));
852 args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&scaleinfobuffer ));
853 args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&nodebuffer ));
854 args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&gsum.data ));
855 args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&gsqsum.data ));
856 args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&candidatebuffer ));
857 args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&pixelstep ));
858 args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&loopcount ));
859 args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&startstage ));
860 args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&splitstage ));
861 args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&endstage ));
862 args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&startnode ));
863 args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&splitnode ));
864 args.push_back ( std::make_pair(sizeof(cl_int4) , (void *)&p ));
865 args.push_back ( std::make_pair(sizeof(cl_int4) , (void *)&pq ));
866 args.push_back ( std::make_pair(sizeof(cl_float) , (void *)&correction ));
868 const char * build_options = gcascade->is_stump_based ? "-D STUMP_BASED=1" : "-D STUMP_BASED=0";
870 openCLExecuteKernel(gsum.clCxt, &haarobjectdetect, "gpuRunHaarClassifierCascade", globalThreads, localThreads, args, -1, -1, build_options);
872 openCLReadBuffer( gsum.clCxt, candidatebuffer, candidate, 4 * sizeof(int)*outputsz );
874 for(int i = 0; i < outputsz; i++)
875 if(candidate[4 * i + 2] != 0)
876 allCandidates.push_back(Rect(candidate[4 * i], candidate[4 * i + 1],
877 candidate[4 * i + 2], candidate[4 * i + 3]));
881 openCLSafeCall(clReleaseMemObject(stagebuffer));
882 openCLSafeCall(clReleaseMemObject(scaleinfobuffer));
883 openCLSafeCall(clReleaseMemObject(nodebuffer));
884 openCLSafeCall(clReleaseMemObject(candidatebuffer));
889 CvSize winsize0 = cascade->orig_window_size;
893 cv::ocl::integral(gimg, gsum, gsqsum);
895 std::vector<CvSize> sizev;
896 std::vector<float> scalev;
897 gpuSetHaarClassifierCascade(cascade);
898 gcascade = (GpuHidHaarClassifierCascade *)cascade->hid_cascade;
899 stage = (GpuHidHaarStageClassifier *)(gcascade + 1);
900 classifier = (GpuHidHaarClassifier *)(stage + gcascade->count);
901 node = (GpuHidHaarTreeNode *)(classifier->node);
904 cl_mem candidatebuffer;
905 cl_mem scaleinfobuffer;
907 cl_mem correctionbuffer;
908 for( n_factors = 0, factor = 1;
909 cvRound(factor * winsize0.width) < gimg.cols - 10 &&
910 cvRound(factor * winsize0.height) < gimg.rows - 10;
911 n_factors++, factor *= scaleFactor )
913 CvSize winSize( cvRound( winsize0.width * factor ), cvRound( winsize0.height * factor ) );
914 if( winSize.width < minSize.width || winSize.height < minSize.height )
918 sizev.push_back(winSize);
919 scalev.push_back(factor);
921 int loopcount = scalev.size();
926 sizev.push_back(minSize);
927 scalev.push_back( std::min(cvRound(minSize.width / winsize0.width), cvRound(minSize.height / winsize0.height)) );
930 detect_piramid_info *scaleinfo = (detect_piramid_info *)malloc(sizeof(detect_piramid_info) * loopcount);
931 cl_int4 *p = (cl_int4 *)malloc(sizeof(cl_int4) * loopcount);
932 float *correction = (float *)malloc(sizeof(float) * loopcount);
934 size_t blocksize = 8;
935 size_t localThreads[3] = { blocksize, blocksize , 1 };
936 size_t globalThreads[3] = { grp_per_CU *gsum.clCxt->getDeviceInfo().maxComputeUnits *localThreads[0],
937 localThreads[1], 1 };
938 int outputsz = 256 * globalThreads[0] / localThreads[0];
939 int nodenum = (datasize - sizeof(GpuHidHaarClassifierCascade) -
940 sizeof(GpuHidHaarStageClassifier) * gcascade->count - sizeof(GpuHidHaarClassifier) * totalclassifier) / sizeof(GpuHidHaarTreeNode);
941 nodebuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_READ_ONLY,
942 nodenum * sizeof(GpuHidHaarTreeNode));
943 openCLSafeCall(clEnqueueWriteBuffer(qu, nodebuffer, 1, 0,
944 nodenum * sizeof(GpuHidHaarTreeNode),
945 node, 0, NULL, NULL));
946 cl_mem newnodebuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_READ_WRITE,
947 loopcount * nodenum * sizeof(GpuHidHaarTreeNode));
949 int endstage = gcascade->count;
950 for(int i = 0; i < loopcount; i++)
954 int ystep = cvRound(std::max(2., factor));
955 int equRect_x = (int)(factor * gcascade->p0 + 0.5);
956 int equRect_y = (int)(factor * gcascade->p1 + 0.5);
957 int equRect_w = (int)(factor * gcascade->p3 + 0.5);
958 int equRect_h = (int)(factor * gcascade->p2 + 0.5);
959 p[i].s[0] = equRect_x;
960 p[i].s[1] = equRect_y;
961 p[i].s[2] = equRect_x + equRect_w;
962 p[i].s[3] = equRect_y + equRect_h;
963 correction[i] = 1. / (equRect_w * equRect_h);
964 int width = (gsum.cols - 1 - sz.width + ystep - 1) / ystep;
965 int height = (gsum.rows - 1 - sz.height + ystep - 1) / ystep;
966 int grpnumperline = (width + localThreads[0] - 1) / localThreads[0];
967 int totalgrp = ((height + localThreads[1] - 1) / localThreads[1]) * grpnumperline;
969 scaleinfo[i].width_height = (width << 16) | height;
970 scaleinfo[i].grpnumperline_totalgrp = (grpnumperline << 16) | totalgrp;
971 scaleinfo[i].imgoff = 0;
972 scaleinfo[i].factor = factor;
973 int startnodenum = nodenum * i;
974 float factor2 = (float)factor;
976 std::vector<std::pair<size_t, const void *> > args1;
977 args1.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&nodebuffer ));
978 args1.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&newnodebuffer ));
979 args1.push_back ( std::make_pair(sizeof(cl_float) , (void *)&factor2 ));
980 args1.push_back ( std::make_pair(sizeof(cl_float) , (void *)&correction[i] ));
981 args1.push_back ( std::make_pair(sizeof(cl_int) , (void *)&startnodenum ));
983 size_t globalThreads2[3] = {nodenum, 1, 1};
984 openCLExecuteKernel(gsum.clCxt, &haarobjectdetect_scaled2, "gpuscaleclassifier", globalThreads2, NULL/*localThreads2*/, args1, -1, -1);
987 int step = gsum.step / 4;
990 stagebuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_READ_ONLY, sizeof(GpuHidHaarStageClassifier) * gcascade->count);
991 openCLSafeCall(clEnqueueWriteBuffer(qu, stagebuffer, 1, 0, sizeof(GpuHidHaarStageClassifier)*gcascade->count, stage, 0, NULL, NULL));
992 candidatebuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_WRITE_ONLY | CL_MEM_ALLOC_HOST_PTR, 4 * sizeof(int) * outputsz);
993 scaleinfobuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_READ_ONLY, sizeof(detect_piramid_info) * loopcount);
994 openCLSafeCall(clEnqueueWriteBuffer(qu, scaleinfobuffer, 1, 0, sizeof(detect_piramid_info)*loopcount, scaleinfo, 0, NULL, NULL));
995 pbuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_READ_ONLY, sizeof(cl_int4) * loopcount);
996 openCLSafeCall(clEnqueueWriteBuffer(qu, pbuffer, 1, 0, sizeof(cl_int4)*loopcount, p, 0, NULL, NULL));
997 correctionbuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_READ_ONLY, sizeof(cl_float) * loopcount);
998 openCLSafeCall(clEnqueueWriteBuffer(qu, correctionbuffer, 1, 0, sizeof(cl_float)*loopcount, correction, 0, NULL, NULL));
1000 std::vector<std::pair<size_t, const void *> > args;
1001 args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&stagebuffer ));
1002 args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&scaleinfobuffer ));
1003 args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&newnodebuffer ));
1004 args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&gsum.data ));
1005 args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&gsqsum.data ));
1006 args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&candidatebuffer ));
1007 args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&gsum.rows ));
1008 args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&gsum.cols ));
1009 args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&step ));
1010 args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&loopcount ));
1011 args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&startstage ));
1012 args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&splitstage ));
1013 args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&endstage ));
1014 args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&startnode ));
1015 args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&pbuffer ));
1016 args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&correctionbuffer ));
1017 args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&nodenum ));
1018 const char * build_options = gcascade->is_stump_based ? "-D STUMP_BASED=1" : "-D STUMP_BASED=0";
1019 openCLExecuteKernel(gsum.clCxt, &haarobjectdetect_scaled2, "gpuRunHaarClassifierCascade_scaled2", globalThreads, localThreads, args, -1, -1, build_options);
1021 candidate = (int *)clEnqueueMapBuffer(qu, candidatebuffer, 1, CL_MAP_READ, 0, 4 * sizeof(int) * outputsz, 0, 0, 0, &status);
1023 for(int i = 0; i < outputsz; i++)
1025 if(candidate[4 * i + 2] != 0)
1026 allCandidates.push_back(Rect(candidate[4 * i], candidate[4 * i + 1], candidate[4 * i + 2], candidate[4 * i + 3]));
1032 clEnqueueUnmapMemObject(qu, candidatebuffer, candidate, 0, 0, 0);
1033 openCLSafeCall(clReleaseMemObject(stagebuffer));
1034 openCLSafeCall(clReleaseMemObject(scaleinfobuffer));
1035 openCLSafeCall(clReleaseMemObject(nodebuffer));
1036 openCLSafeCall(clReleaseMemObject(newnodebuffer));
1037 openCLSafeCall(clReleaseMemObject(candidatebuffer));
1038 openCLSafeCall(clReleaseMemObject(pbuffer));
1039 openCLSafeCall(clReleaseMemObject(correctionbuffer));
1042 cvFree(&cascade->hid_cascade);
1043 rectList.resize(allCandidates.size());
1044 if(!allCandidates.empty())
1045 std::copy(allCandidates.begin(), allCandidates.end(), rectList.begin());
1047 if( minNeighbors != 0 || findBiggestObject )
1048 groupRectangles(rectList, rweights, std::max(minNeighbors, 1), GROUP_EPS);
1050 rweights.resize(rectList.size(), 0);
1053 if( findBiggestObject && rectList.size() )
1055 Rect result_comp(0, 0, 0, 0);
1056 for( size_t i = 0; i < rectList.size(); i++ )
1058 cv::Rect r = rectList[i];
1059 if( r.area() > result_comp.area() )
1064 faces.push_back(result_comp);