Merge remote-tracking branch 'origin/2.4' into merge-2.4
[profile/ivi/opencv.git] / modules / ocl / src / haar.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
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.
16 //
17 // @Authors
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
23 //
24 // Redistribution and use in source and binary forms, with or without modification,
25 // are permitted provided that the following conditions are met:
26 //
27 //   * Redistribution's of source code must retain the above copyright notice,
28 //     this list of conditions and the following disclaimer.
29 //
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.
33 //
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.
36 //
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.
47 //
48 //M*/
49
50 #include "precomp.hpp"
51 #include "opencl_kernels.hpp"
52
53 using namespace cv;
54 using namespace cv::ocl;
55
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
60 typedef int sumtype;
61 typedef double sqsumtype;
62
63 typedef struct CvHidHaarFeature
64 {
65     struct
66     {
67         sumtype *p0, *p1, *p2, *p3;
68         float weight;
69     }
70     rect[CV_HAAR_FEATURE_MAX];
71 }
72 CvHidHaarFeature;
73
74
75 typedef struct CvHidHaarTreeNode
76 {
77     CvHidHaarFeature feature;
78     float threshold;
79     int left;
80     int right;
81 }
82 CvHidHaarTreeNode;
83
84
85 typedef struct CvHidHaarClassifier
86 {
87     int count;
88     //CvHaarFeature* orig_feature;
89     CvHidHaarTreeNode *node;
90     float *alpha;
91 }
92 CvHidHaarClassifier;
93
94
95 typedef struct CvHidHaarStageClassifier
96 {
97     int  count;
98     float threshold;
99     CvHidHaarClassifier *classifier;
100     int two_rects;
101
102     struct CvHidHaarStageClassifier *next;
103     struct CvHidHaarStageClassifier *child;
104     struct CvHidHaarStageClassifier *parent;
105 }
106 CvHidHaarStageClassifier;
107
108
109 struct CvHidHaarClassifierCascade
110 {
111     int  count;
112     int  is_stump_based;
113     int  has_tilted_features;
114     int  is_tree;
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;
120
121     void **ipp_stages;
122 };
123 typedef struct
124 {
125     int width_height;
126     int grpnumperline_totalgrp;
127     int imgoff;
128     float factor;
129 } detect_piramid_info;
130 #ifdef _MSC_VER
131 #define _ALIGNED_ON(_ALIGNMENT) __declspec(align(_ALIGNMENT))
132
133 typedef _ALIGNED_ON(128) struct  GpuHidHaarTreeNode
134 {
135     _ALIGNED_ON(64) int p[CV_HAAR_FEATURE_MAX][4];
136     float weight[CV_HAAR_FEATURE_MAX] ;
137     float threshold ;
138     _ALIGNED_ON(16) float alpha[3] ;
139     _ALIGNED_ON(4) int left ;
140     _ALIGNED_ON(4) int right ;
141 }
142 GpuHidHaarTreeNode;
143
144
145 typedef  _ALIGNED_ON(32) struct  GpuHidHaarClassifier
146 {
147     _ALIGNED_ON(4) int count;
148     _ALIGNED_ON(8) GpuHidHaarTreeNode *node ;
149     _ALIGNED_ON(8) float *alpha ;
150 }
151 GpuHidHaarClassifier;
152
153
154 typedef _ALIGNED_ON(64) struct   GpuHidHaarStageClassifier
155 {
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 ;
163 }
164 GpuHidHaarStageClassifier;
165
166
167 typedef _ALIGNED_ON(64) struct  GpuHidHaarClassifierCascade
168 {
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;
183 #else
184 #define _ALIGNED_ON(_ALIGNMENT) __attribute__((aligned(_ALIGNMENT) ))
185
186 typedef struct _ALIGNED_ON(128) GpuHidHaarTreeNode
187 {
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);
194 }
195 GpuHidHaarTreeNode;
196
197 typedef struct _ALIGNED_ON(32) GpuHidHaarClassifier
198 {
199     int count _ALIGNED_ON(4);
200     GpuHidHaarTreeNode *node _ALIGNED_ON(8);
201     float *alpha _ALIGNED_ON(8);
202 }
203 GpuHidHaarClassifier;
204
205
206 typedef struct _ALIGNED_ON(64) GpuHidHaarStageClassifier
207 {
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);
215 }
216 GpuHidHaarStageClassifier;
217
218
219 typedef struct _ALIGNED_ON(64) GpuHidHaarClassifierCascade
220 {
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;
235 #endif
236
237 const int icv_object_win_border = 1;
238 const float icv_stage_threshold_bias = 0.0001f;
239 double globaltime = 0;
240
241 /* create more efficient internal representation of haar classifier cascade */
242 static GpuHidHaarClassifierCascade * gpuCreateHidHaarClassifierCascade( CvHaarClassifierCascade *cascade, int *size, int *totalclassifier)
243 {
244     GpuHidHaarClassifierCascade *out = 0;
245
246     int i, j, k, l;
247     int datasize;
248     int total_classifiers = 0;
249     int total_nodes = 0;
250     char errorstr[256];
251
252     GpuHidHaarStageClassifier *stage_classifier_ptr;
253     GpuHidHaarClassifier *haar_classifier_ptr;
254     GpuHidHaarTreeNode *haar_node_ptr;
255
256     CvSize orig_window_size;
257     int has_tilted_features = 0;
258
259     if( !CV_IS_HAAR_CLASSIFIER(cascade) )
260         CV_Error( !cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier pointer" );
261
262     if( cascade->hid_cascade )
263         CV_Error( CV_StsError, "hid_cascade has been already created" );
264
265     if( !cascade->stage_classifier )
266         CV_Error( CV_StsNullPtr, "" );
267
268     if( cascade->count <= 0 )
269         CV_Error( CV_StsOutOfRange, "Negative number of cascade stages" );
270
271     orig_window_size = cascade->orig_window_size;
272
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++ )
276     {
277         CvHaarStageClassifier *stage_classifier = cascade->stage_classifier + i;
278
279         if( !stage_classifier->classifier ||
280                 stage_classifier->count <= 0 )
281         {
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 );
285         }
286
287         total_classifiers += stage_classifier->count;
288
289         for( j = 0; j < stage_classifier->count; j++ )
290         {
291             CvHaarClassifier *classifier = stage_classifier->classifier + j;
292
293             total_nodes += classifier->count;
294             for( l = 0; l < classifier->count; l++ )
295             {
296                 for( k = 0; k < CV_HAAR_FEATURE_MAX; k++ )
297                 {
298                     if( classifier->haar_feature[l].rect[k].r.width )
299                     {
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
305                                 ||
306                                 (!tilted &&
307                                  (r.x < 0 || r.y + r.height > orig_window_size.height))
308                                 ||
309                                 (tilted && (r.x - r.height < 0 ||
310                                             r.y + r.width + r.height > orig_window_size.height)))
311                         {
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 );
316                         }
317                     }
318                 }
319             }
320         }
321     }
322
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;
328
329     *totalclassifier = total_classifiers;
330     *size = datasize;
331     out = (GpuHidHaarClassifierCascade *)cvAlloc( datasize );
332     memset( out, 0, sizeof(*out) );
333
334     /* init header */
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);
339
340     out->is_stump_based = 1;
341     out->has_tilted_features = has_tilted_features;
342     out->is_tree = 0;
343
344     /* initialize internal representation */
345     for( i = 0; i < cascade->count; i++ )
346     {
347         CvHaarStageClassifier *stage_classifier = cascade->stage_classifier + i;
348         GpuHidHaarStageClassifier *hid_stage_classifier = stage_classifier_ptr + i;
349
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;
355
356         for( j = 0; j < stage_classifier->count; j++ )
357         {
358             CvHaarClassifier *classifier         = stage_classifier->classifier + j;
359             GpuHidHaarClassifier *hid_classifier = hid_stage_classifier->classifier + j;
360             int node_count = classifier->count;
361
362             float *alpha_ptr = &haar_node_ptr->alpha[0];
363
364             hid_classifier->count = node_count;
365             hid_classifier->node = haar_node_ptr;
366             hid_classifier->alpha = alpha_ptr;
367
368             for( l = 0; l < node_count; l++ )
369             {
370                 GpuHidHaarTreeNode *node     = hid_classifier->node + l;
371                 CvHaarFeature      *feature = classifier->haar_feature + l;
372
373                 memset( node, -1, sizeof(*node) );
374                 node->threshold = classifier->threshold[l];
375                 node->left      = classifier->left[l];
376                 node->right     = classifier->right[l];
377
378                 if( fabs(feature->rect[2].weight) < DBL_EPSILON ||
379                         feature->rect[2].r.width == 0 ||
380                         feature->rect[2].r.height == 0 )
381                 {
382                     node->p[2][0] = 0;
383                     node->p[2][1] = 0;
384                     node->p[2][2] = 0;
385                     node->p[2][3] = 0;
386                     node->weight[2] = 0;
387                 }
388                 else
389                     hid_stage_classifier->two_rects = 0;
390
391                 memcpy( node->alpha, classifier->alpha, (node_count + 1)*sizeof(alpha_ptr[0]));
392                 haar_node_ptr = haar_node_ptr + 1;
393             }
394             out->is_stump_based &= node_count == 1;
395         }
396     }
397
398     cascade->hid_cascade = (CvHidHaarClassifierCascade *)out;
399     assert( (char *)haar_node_ptr - (char *)out <= datasize );
400
401     return out;
402 }
403
404
405 #define sum_elem_ptr(sum,row,col)  \
406     ((sumtype*)CV_MAT_ELEM_PTR_FAST((sum),(row),(col),sizeof(sumtype)))
407
408 #define sqsum_elem_ptr(sqsum,row,col)  \
409     ((sqsumtype*)CV_MAT_ELEM_PTR_FAST((sqsum),(row),(col),sizeof(sqsumtype)))
410
411 #define calc_sum(rect,offset) \
412     ((rect).p0[offset] - (rect).p1[offset] - (rect).p2[offset] + (rect).p3[offset])
413
414
415 static void gpuSetImagesForHaarClassifierCascade( CvHaarClassifierCascade *_cascade,
416                                       double scale,
417                                       int step)
418 {
419     GpuHidHaarClassifierCascade *cascade;
420     int coi0 = 0, coi1 = 0;
421     int i;
422     int datasize;
423     int total;
424     CvRect equRect;
425     double weight_scale;
426     GpuHidHaarStageClassifier *stage_classifier;
427
428     if( !CV_IS_HAAR_CLASSIFIER(_cascade) )
429         CV_Error( !_cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier pointer" );
430
431     if( scale <= 0 )
432         CV_Error( CV_StsOutOfRange, "Scale must be positive" );
433
434     if( coi0 || coi1 )
435         CV_Error( CV_BadCOI, "COI is not supported" );
436
437     if( !_cascade->hid_cascade )
438         gpuCreateHidHaarClassifierCascade(_cascade, &datasize, &total);
439
440     cascade = (GpuHidHaarClassifierCascade *) _cascade->hid_cascade;
441     stage_classifier = (GpuHidHaarStageClassifier *) (cascade + 1);
442
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 );
446
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;
452
453     cascade->pq0 = equRect.x;
454     cascade->pq1 = equRect.y;
455     cascade->pq2 = equRect.x + equRect.width;
456     cascade->pq3 = equRect.y + equRect.height;
457
458     cascade->p0 = equRect.x;
459     cascade->p1 = equRect.y;
460     cascade->p2 = equRect.x + equRect.width;
461     cascade->p3 = equRect.y + equRect.height;
462
463
464     /* init pointers in haar features according to real window size and
465     given image pointers */
466     for( i = 0; i < _cascade->count; i++ )
467     {
468         int j, k, l;
469         for( j = 0; j < stage_classifier[i].count; j++ )
470         {
471             for( l = 0; l < stage_classifier[i].classifier[j].count; l++ )
472             {
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;
477                 CvRect r[3];
478
479                 int base_w = -1, base_h = -1;
480                 int new_base_w = 0, new_base_h = 0;
481                 int kx, ky;
482                 int flagx = 0, flagy = 0;
483                 int x0 = 0, y0 = 0;
484                 int nr;
485
486                 /* align blocks */
487                 for( k = 0; k < CV_HAAR_FEATURE_MAX; k++ )
488                 {
489                     if(!hidnode->p[k][0])
490                         break;
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) );
496                 }
497
498                 nr = k;
499                 base_w += 1;
500                 base_h += 1;
501                 if(base_w == 0)
502                     base_w = 1;
503                 kx = r[0].width / base_w;
504                 if(base_h == 0)
505                     base_h = 1;
506                 ky = r[0].height / base_h;
507
508                 if( kx <= 0 )
509                 {
510                     flagx = 1;
511                     new_base_w = cvRound( r[0].width * scale ) / kx;
512                     x0 = cvRound( r[0].x * scale );
513                 }
514
515                 if( ky <= 0 )
516                 {
517                     flagy = 1;
518                     new_base_h = cvRound( r[0].height * scale ) / ky;
519                     y0 = cvRound( r[0].y * scale );
520                 }
521
522                 for( k = 0; k < nr; k++ )
523                 {
524                     CvRect tr;
525                     double correction_ratio;
526
527                     if( flagx )
528                     {
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;
531                     }
532                     else
533                     {
534                         tr.x = cvRound( r[k].x * scale );
535                         tr.width = cvRound( r[k].width * scale );
536                     }
537
538                     if( flagy )
539                     {
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;
542                     }
543                     else
544                     {
545                         tr.y = cvRound( r[k].y * scale );
546                         tr.height = cvRound( r[k].height * scale );
547                     }
548
549 #if CV_ADJUST_WEIGHTS
550                     {
551                         // RAINER START
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;
560                         // RAINER END
561                     }
562 #else
563                     correction_ratio = weight_scale * (!feature->tilted ? 1 : 0.5);
564 #endif
565
566                     if( !feature->tilted )
567                     {
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;
572                     }
573                     else
574                     {
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;
579                     }
580                     hidnode->weight[k] = (float)(feature->rect[k].weight * correction_ratio);
581                     if( k == 0 )
582                         area0 = tr.width * tr.height;
583                     else
584                         sum0 += hidnode->weight[k] * tr.width * tr.height;
585                 }
586                 hidnode->weight[0] = (float)(-sum0 / area0);
587             } /* l */
588         } /* j */
589     }
590 }
591
592 static void gpuSetHaarClassifierCascade( CvHaarClassifierCascade *_cascade)
593 {
594     GpuHidHaarClassifierCascade *cascade;
595     int i;
596     int datasize;
597     int total;
598     CvRect equRect;
599     double weight_scale;
600     GpuHidHaarStageClassifier *stage_classifier;
601
602     if( !CV_IS_HAAR_CLASSIFIER(_cascade) )
603         CV_Error( !_cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier pointer" );
604
605     if( !_cascade->hid_cascade )
606         gpuCreateHidHaarClassifierCascade(_cascade, &datasize, &total);
607
608     cascade = (GpuHidHaarClassifierCascade *) _cascade->hid_cascade;
609     stage_classifier = (GpuHidHaarStageClassifier *) cascade + 1;
610
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;
614
615     equRect.x = equRect.y = 1;
616     equRect.width = _cascade->orig_window_size.width - 2;
617     equRect.height = _cascade->orig_window_size.height - 2;
618     weight_scale = 1;
619     cascade->inv_window_area = weight_scale;
620
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++ )
626     {
627         int j, k, l;
628         for( j = 0; j < stage_classifier[i].count; j++ )
629         {
630             for( l = 0; l < stage_classifier[i].classifier[j].count; l++ )
631             {
632                 CvHaarFeature *feature =
633                     &_cascade->stage_classifier[i].classifier[j].haar_feature[l];
634                 GpuHidHaarTreeNode *hidnode = &stage_classifier[i].classifier[j].node[l];
635                 CvRect r[3];
636
637
638                 int nr;
639
640                 /* align blocks */
641                 for( k = 0; k < CV_HAAR_FEATURE_MAX; k++ )
642                 {
643                     if(!hidnode->p[k][0])
644                         break;
645                     r[k] = feature->rect[k].r;
646                }
647
648                 nr = k;
649                 for( k = 0; k < nr; k++ )
650                 {
651                     CvRect tr;
652                     double correction_ratio;
653                     tr.x = r[k].x;
654                     tr.width = r[k].width;
655                     tr.y = r[k].y ;
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);
663                 }
664             } /* l */
665         } /* j */
666     }
667 }
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)
673 {
674     CvHaarClassifierCascade *cascade = oldCascade;
675
676     const double GROUP_EPS = 0.2;
677
678     cv::ConcurrentRectVector allCandidates;
679     std::vector<cv::Rect> rectList;
680     std::vector<int> rweights;
681     double factor;
682     int datasize=0;
683     int totalclassifier=0;
684
685     GpuHidHaarClassifierCascade *gcascade;
686     GpuHidHaarStageClassifier    *stage;
687     GpuHidHaarClassifier         *classifier;
688     GpuHidHaarTreeNode           *node;
689
690     int *candidate;
691     cl_int status;
692
693     bool findBiggestObject = (flags & CV_HAAR_FIND_BIGGEST_OBJECT) != 0;
694
695     if( maxSize.height == 0 || maxSize.width == 0 )
696     {
697         maxSize.height = gimg.rows;
698         maxSize.width = gimg.cols;
699     }
700
701     if( !CV_IS_HAAR_CLASSIFIER(cascade) )
702         CV_Error( !cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier cascade" );
703
704     //if( !storage )
705     //    CV_Error( CV_StsNullPtr, "Null storage pointer" );
706
707     if( CV_MAT_DEPTH(gimg.type()) != CV_8U )
708         CV_Error( CV_StsUnsupportedFormat, "Only 8-bit images are supported" );
709
710     if( scaleFactor <= 1 )
711         CV_Error( CV_StsOutOfRange, "scale factor must be > 1" );
712
713     if( findBiggestObject )
714         flags &= ~CV_HAAR_SCALE_IMAGE;
715
716     if( !cascade->hid_cascade )
717         gpuCreateHidHaarClassifierCascade(cascade, &datasize, &totalclassifier);
718
719     //result_seq = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvAvgComp), storage );
720
721     if( CV_MAT_CN(gimg.type()) > 1 )
722     {
723         oclMat gtemp;
724         cvtColor( gimg, gtemp, COLOR_BGR2GRAY );
725         gimg = gtemp;
726     }
727
728     if( findBiggestObject )
729         flags &= ~(CV_HAAR_SCALE_IMAGE | CV_HAAR_DO_CANNY_PRUNING);
730
731     if( gimg.cols < minSize.width || gimg.rows < minSize.height )
732         CV_Error(CV_StsError, "Image too small");
733
734     cl_command_queue qu = getClCommandQueue(Context::getContext());
735     if( (flags & CV_HAAR_SCALE_IMAGE) )
736     {
737         CvSize winSize0 = cascade->orig_window_size;
738         int totalheight = 0;
739         int indexy = 0;
740         CvSize sz;
741         std::vector<CvSize> sizev;
742         std::vector<float> scalev;
743         for(factor = 1.f;; factor *= scaleFactor)
744         {
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 );
749
750             if( sz1.width <= 0 || sz1.height <= 0 )
751                 break;
752             if( winSize.width > maxSize.width || winSize.height > maxSize.height )
753                 break;
754             if( winSize.width < minSize.width || winSize.height < minSize.height )
755                 continue;
756
757             totalheight += sz.height;
758             sizev.push_back(sz);
759             scalev.push_back(factor);
760         }
761
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);
765
766         cl_mem stagebuffer;
767         cl_mem nodebuffer;
768         cl_mem candidatebuffer;
769         cl_mem scaleinfobuffer;
770         cv::Rect roi, roi2;
771         cv::Mat imgroi, imgroisq;
772         cv::ocl::oclMat resizeroi, gimgroi, gimgroisq;
773         int grp_per_CU = 12;
774
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],
778                                     localThreads[1], 1
779                                   };
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);
783
784         for( int i = 0; i < loopcount; i++ )
785         {
786             sz = sizev[i];
787             factor = scalev[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);
791             gimgroi = gsum(roi);
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;
796
797
798             int grpnumperline = (width + localThreads[0] - 1) / localThreads[0];
799             int totalgrp = ((height + localThreads[1] - 1) / localThreads[1]) * grpnumperline;
800
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);
806             indexy += sz.height;
807         }
808
809         gcascade   = (GpuHidHaarClassifierCascade *)cascade->hid_cascade;
810         stage      = (GpuHidHaarStageClassifier *)(gcascade + 1);
811         classifier = (GpuHidHaarClassifier *)(stage + gcascade->count);
812         node       = (GpuHidHaarTreeNode *)(classifier->node);
813
814         int nodenum = (datasize - sizeof(GpuHidHaarClassifierCascade) -
815                        sizeof(GpuHidHaarStageClassifier) * gcascade->count - sizeof(GpuHidHaarClassifier) * totalclassifier) / sizeof(GpuHidHaarTreeNode);
816
817         candidate = (int *)malloc(4 * sizeof(int) * outputsz);
818
819         gpuSetImagesForHaarClassifierCascade( cascade, 1., gsum.step / 4 );
820
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));
823
824         nodebuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_READ_ONLY, nodenum * sizeof(GpuHidHaarTreeNode));
825
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);
829
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));
832
833         int startstage = 0;
834         int endstage = gcascade->count;
835         int startnode = 0;
836         int pixelstep = gsum.step / 4;
837         int splitstage = 3;
838         int splitnode = stage[0].count + stage[1].count + stage[2].count;
839         cl_int4 p, pq;
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;
849
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 ));
867
868         const char * build_options = gcascade->is_stump_based ? "-D STUMP_BASED=1" : "-D STUMP_BASED=0";
869
870         openCLExecuteKernel(gsum.clCxt, &haarobjectdetect, "gpuRunHaarClassifierCascade", globalThreads, localThreads, args, -1, -1, build_options);
871
872         openCLReadBuffer( gsum.clCxt, candidatebuffer, candidate, 4 * sizeof(int)*outputsz );
873
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]));
878
879         free(scaleinfo);
880         free(candidate);
881         openCLSafeCall(clReleaseMemObject(stagebuffer));
882         openCLSafeCall(clReleaseMemObject(scaleinfobuffer));
883         openCLSafeCall(clReleaseMemObject(nodebuffer));
884         openCLSafeCall(clReleaseMemObject(candidatebuffer));
885
886     }
887     else
888     {
889         CvSize winsize0 = cascade->orig_window_size;
890         int n_factors = 0;
891         oclMat gsum;
892         oclMat gsqsum;
893         cv::ocl::integral(gimg, gsum, gsqsum);
894         CvSize sz;
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);
902         cl_mem stagebuffer;
903         cl_mem nodebuffer;
904         cl_mem candidatebuffer;
905         cl_mem scaleinfobuffer;
906         cl_mem pbuffer;
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 )
912         {
913             CvSize winSize( cvRound( winsize0.width * factor ), cvRound( winsize0.height * factor ) );
914             if( winSize.width < minSize.width || winSize.height < minSize.height )
915             {
916                 continue;
917             }
918             sizev.push_back(winSize);
919             scalev.push_back(factor);
920         }
921         int loopcount = scalev.size();
922         if(loopcount == 0)
923         {
924             loopcount = 1;
925             n_factors = 1;
926             sizev.push_back(minSize);
927             scalev.push_back( std::min(cvRound(minSize.width / winsize0.width), cvRound(minSize.height / winsize0.height)) );
928
929         }
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);
933         int grp_per_CU = 12;
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));
948         int startstage = 0;
949         int endstage = gcascade->count;
950         for(int i = 0; i < loopcount; i++)
951         {
952             sz = sizev[i];
953             factor = scalev[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;
968
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;
975
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 ));
982
983             size_t globalThreads2[3] = {nodenum, 1, 1};
984             openCLExecuteKernel(gsum.clCxt, &haarobjectdetect_scaled2, "gpuscaleclassifier", globalThreads2, NULL/*localThreads2*/, args1, -1, -1);
985         }
986
987         int step = gsum.step / 4;
988         int startnode = 0;
989         int splitstage = 3;
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));
999
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);
1020
1021         candidate = (int *)clEnqueueMapBuffer(qu, candidatebuffer, 1, CL_MAP_READ, 0, 4 * sizeof(int) * outputsz, 0, 0, 0, &status);
1022
1023         for(int i = 0; i < outputsz; i++)
1024         {
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]));
1027         }
1028
1029         free(scaleinfo);
1030         free(p);
1031         free(correction);
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));
1040     }
1041
1042     cvFree(&cascade->hid_cascade);
1043     rectList.resize(allCandidates.size());
1044     if(!allCandidates.empty())
1045         std::copy(allCandidates.begin(), allCandidates.end(), rectList.begin());
1046
1047     if( minNeighbors != 0 || findBiggestObject )
1048         groupRectangles(rectList, rweights, std::max(minNeighbors, 1), GROUP_EPS);
1049     else
1050         rweights.resize(rectList.size(), 0);
1051
1052     faces.clear();
1053     if( findBiggestObject && rectList.size() )
1054     {
1055         Rect result_comp(0, 0, 0, 0);
1056         for( size_t i = 0; i < rectList.size(); i++ )
1057         {
1058             cv::Rect r = rectList[i];
1059             if( r.area() > result_comp.area() )
1060             {
1061                 result_comp = r;
1062             }
1063         }
1064         faces.push_back(result_comp);
1065     }
1066     else
1067     {
1068         faces = rectList;
1069     }
1070 }