Merge pull request #1722 from StevenPuttemans:feature_1631_second
[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 //    Sen Liu, swjtuls1987@126.com
24 //
25 // Redistribution and use in source and binary forms, with or without modification,
26 // are permitted provided that the following conditions are met:
27 //
28 //   * Redistribution's of source code must retain the above copyright notice,
29 //     this list of conditions and the following disclaimer.
30 //
31 //   * Redistribution's in binary form must reproduce the above copyright notice,
32 //     this list of conditions and the following disclaimer in the documentation
33 //     and/or other materials provided with the distribution.
34 //
35 //   * The name of the copyright holders may not be used to endorse or promote products
36 //     derived from this software without specific prior written permission.
37 //
38 // This software is provided by the copyright holders and contributors "as is" and
39 // any express or implied warranties, including, but not limited to, the implied
40 // warranties of merchantability and fitness for a particular purpose are disclaimed.
41 // In no event shall the Intel Corporation or contributors be liable for any direct,
42 // indirect, incidental, special, exemplary, or consequential damages
43 // (including, but not limited to, procurement of substitute goods or services;
44 // loss of use, data, or profits; or business interruption) however caused
45 // and on any theory of liability, whether in contract, strict liability,
46 // or tort (including negligence or otherwise) arising in any way out of
47 // the use of this software, even if advised of the possibility of such damage.
48 //
49 //M*/
50
51 #include "precomp.hpp"
52 #include "opencl_kernels.hpp"
53
54 using namespace cv;
55 using namespace cv::ocl;
56
57 /* these settings affect the quality of detection: change with care */
58 #define CV_ADJUST_FEATURES 1
59 #define CV_ADJUST_WEIGHTS  0
60
61 typedef int sumtype;
62 typedef double sqsumtype;
63
64 typedef struct CvHidHaarFeature
65 {
66     struct
67     {
68         sumtype *p0, *p1, *p2, *p3;
69         float weight;
70     }
71     rect[CV_HAAR_FEATURE_MAX];
72 }
73 CvHidHaarFeature;
74
75
76 typedef struct CvHidHaarTreeNode
77 {
78     CvHidHaarFeature feature;
79     float threshold;
80     int left;
81     int right;
82 }
83 CvHidHaarTreeNode;
84
85
86 typedef struct CvHidHaarClassifier
87 {
88     int count;
89     //CvHaarFeature* orig_feature;
90     CvHidHaarTreeNode *node;
91     float *alpha;
92 }
93 CvHidHaarClassifier;
94
95
96 typedef struct CvHidHaarStageClassifier
97 {
98     int  count;
99     float threshold;
100     CvHidHaarClassifier *classifier;
101     int two_rects;
102
103     struct CvHidHaarStageClassifier *next;
104     struct CvHidHaarStageClassifier *child;
105     struct CvHidHaarStageClassifier *parent;
106 }
107 CvHidHaarStageClassifier;
108
109
110 struct CvHidHaarClassifierCascade
111 {
112     int  count;
113     int  is_stump_based;
114     int  has_tilted_features;
115     int  is_tree;
116     double inv_window_area;
117     CvMat sum, sqsum, tilted;
118     CvHidHaarStageClassifier *stage_classifier;
119     sqsumtype *pq0, *pq1, *pq2, *pq3;
120     sumtype *p0, *p1, *p2, *p3;
121
122     void **ipp_stages;
123 };
124 typedef struct
125 {
126     int width_height;
127     int grpnumperline_totalgrp;
128     int imgoff;
129     float factor;
130 } detect_piramid_info;
131 #ifdef _MSC_VER
132 #define _ALIGNED_ON(_ALIGNMENT) __declspec(align(_ALIGNMENT))
133
134 typedef _ALIGNED_ON(128) struct  GpuHidHaarTreeNode
135 {
136     _ALIGNED_ON(64) int p[CV_HAAR_FEATURE_MAX][4];
137     float weight[CV_HAAR_FEATURE_MAX] ;
138     float threshold ;
139     _ALIGNED_ON(16) float alpha[3] ;
140     _ALIGNED_ON(4) int left ;
141     _ALIGNED_ON(4) int right ;
142 }
143 GpuHidHaarTreeNode;
144
145
146 typedef  _ALIGNED_ON(32) struct  GpuHidHaarClassifier
147 {
148     _ALIGNED_ON(4) int count;
149     _ALIGNED_ON(8) GpuHidHaarTreeNode *node ;
150     _ALIGNED_ON(8) float *alpha ;
151 }
152 GpuHidHaarClassifier;
153
154
155 typedef _ALIGNED_ON(64) struct   GpuHidHaarStageClassifier
156 {
157     _ALIGNED_ON(4) int  count ;
158     _ALIGNED_ON(4) float threshold ;
159     _ALIGNED_ON(4) int two_rects ;
160     _ALIGNED_ON(8) GpuHidHaarClassifier *classifier ;
161     _ALIGNED_ON(8) struct GpuHidHaarStageClassifier *next;
162     _ALIGNED_ON(8) struct GpuHidHaarStageClassifier *child ;
163     _ALIGNED_ON(8) struct GpuHidHaarStageClassifier *parent ;
164 }
165 GpuHidHaarStageClassifier;
166
167
168 typedef _ALIGNED_ON(64) struct  GpuHidHaarClassifierCascade
169 {
170     _ALIGNED_ON(4) int  count ;
171     _ALIGNED_ON(4) int  is_stump_based ;
172     _ALIGNED_ON(4) int  has_tilted_features ;
173     _ALIGNED_ON(4) int  is_tree ;
174     _ALIGNED_ON(4) int pq0 ;
175     _ALIGNED_ON(4) int pq1 ;
176     _ALIGNED_ON(4) int pq2 ;
177     _ALIGNED_ON(4) int pq3 ;
178     _ALIGNED_ON(4) int p0 ;
179     _ALIGNED_ON(4) int p1 ;
180     _ALIGNED_ON(4) int p2 ;
181     _ALIGNED_ON(4) int p3 ;
182     _ALIGNED_ON(4) float inv_window_area ;
183 } GpuHidHaarClassifierCascade;
184 #else
185 #define _ALIGNED_ON(_ALIGNMENT) __attribute__((aligned(_ALIGNMENT) ))
186
187 typedef struct _ALIGNED_ON(128) GpuHidHaarTreeNode
188 {
189     int p[CV_HAAR_FEATURE_MAX][4] _ALIGNED_ON(64);
190     float weight[CV_HAAR_FEATURE_MAX];// _ALIGNED_ON(16);
191     float threshold;// _ALIGNED_ON(4);
192     float alpha[3] _ALIGNED_ON(16);
193     int left _ALIGNED_ON(4);
194     int right _ALIGNED_ON(4);
195 }
196 GpuHidHaarTreeNode;
197
198 typedef struct _ALIGNED_ON(32) GpuHidHaarClassifier
199 {
200     int count _ALIGNED_ON(4);
201     GpuHidHaarTreeNode *node _ALIGNED_ON(8);
202     float *alpha _ALIGNED_ON(8);
203 }
204 GpuHidHaarClassifier;
205
206
207 typedef struct _ALIGNED_ON(64) GpuHidHaarStageClassifier
208 {
209     int  count _ALIGNED_ON(4);
210     float threshold _ALIGNED_ON(4);
211     int two_rects _ALIGNED_ON(4);
212     GpuHidHaarClassifier *classifier _ALIGNED_ON(8);
213     struct GpuHidHaarStageClassifier *next _ALIGNED_ON(8);
214     struct GpuHidHaarStageClassifier *child _ALIGNED_ON(8);
215     struct GpuHidHaarStageClassifier *parent _ALIGNED_ON(8);
216 }
217 GpuHidHaarStageClassifier;
218
219
220 typedef struct _ALIGNED_ON(64) GpuHidHaarClassifierCascade
221 {
222     int  count _ALIGNED_ON(4);
223     int  is_stump_based _ALIGNED_ON(4);
224     int  has_tilted_features _ALIGNED_ON(4);
225     int  is_tree _ALIGNED_ON(4);
226     int pq0 _ALIGNED_ON(4);
227     int pq1 _ALIGNED_ON(4);
228     int pq2 _ALIGNED_ON(4);
229     int pq3 _ALIGNED_ON(4);
230     int p0 _ALIGNED_ON(4);
231     int p1 _ALIGNED_ON(4);
232     int p2 _ALIGNED_ON(4);
233     int p3 _ALIGNED_ON(4);
234     float inv_window_area _ALIGNED_ON(4);
235 } GpuHidHaarClassifierCascade;
236 #endif
237
238 const int icv_object_win_border = 1;
239 const float icv_stage_threshold_bias = 0.0001f;
240 double globaltime = 0;
241
242 /* create more efficient internal representation of haar classifier cascade */
243 static GpuHidHaarClassifierCascade * gpuCreateHidHaarClassifierCascade( CvHaarClassifierCascade *cascade, int *size, int *totalclassifier)
244 {
245     GpuHidHaarClassifierCascade *out = 0;
246
247     int i, j, k, l;
248     int datasize;
249     int total_classifiers = 0;
250     int total_nodes = 0;
251     char errorstr[256];
252
253     GpuHidHaarStageClassifier *stage_classifier_ptr;
254     GpuHidHaarClassifier *haar_classifier_ptr;
255     GpuHidHaarTreeNode *haar_node_ptr;
256
257     CvSize orig_window_size;
258     int has_tilted_features = 0;
259
260     if( !CV_IS_HAAR_CLASSIFIER(cascade) )
261         CV_Error( !cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier pointer" );
262
263     if( cascade->hid_cascade )
264         CV_Error( CV_StsError, "hid_cascade has been already created" );
265
266     if( !cascade->stage_classifier )
267         CV_Error( CV_StsNullPtr, "" );
268
269     if( cascade->count <= 0 )
270         CV_Error( CV_StsOutOfRange, "Negative number of cascade stages" );
271
272     orig_window_size = cascade->orig_window_size;
273
274     /* check input structure correctness and calculate total memory size needed for
275     internal representation of the classifier cascade */
276     for( i = 0; i < cascade->count; i++ )
277     {
278         CvHaarStageClassifier *stage_classifier = cascade->stage_classifier + i;
279
280         if( !stage_classifier->classifier ||
281                 stage_classifier->count <= 0 )
282         {
283             sprintf( errorstr, "header of the stage classifier #%d is invalid "
284                      "(has null pointers or non-positive classfier count)", i );
285             CV_Error( CV_StsError, errorstr );
286         }
287
288         total_classifiers += stage_classifier->count;
289
290         for( j = 0; j < stage_classifier->count; j++ )
291         {
292             CvHaarClassifier *classifier = stage_classifier->classifier + j;
293
294             total_nodes += classifier->count;
295             for( l = 0; l < classifier->count; l++ )
296             {
297                 for( k = 0; k < CV_HAAR_FEATURE_MAX; k++ )
298                 {
299                     if( classifier->haar_feature[l].rect[k].r.width )
300                     {
301                         CvRect r = classifier->haar_feature[l].rect[k].r;
302                         int tilted = classifier->haar_feature[l].tilted;
303                         has_tilted_features |= tilted != 0;
304                         if( r.width < 0 || r.height < 0 || r.y < 0 ||
305                                 r.x + r.width > orig_window_size.width
306                                 ||
307                                 (!tilted &&
308                                  (r.x < 0 || r.y + r.height > orig_window_size.height))
309                                 ||
310                                 (tilted && (r.x - r.height < 0 ||
311                                             r.y + r.width + r.height > orig_window_size.height)))
312                         {
313                             sprintf( errorstr, "rectangle #%d of the classifier #%d of "
314                                      "the stage classifier #%d is not inside "
315                                      "the reference (original) cascade window", k, j, i );
316                             CV_Error( CV_StsNullPtr, errorstr );
317                         }
318                     }
319                 }
320             }
321         }
322     }
323
324     // this is an upper boundary for the whole hidden cascade size
325     datasize = sizeof(GpuHidHaarClassifierCascade)                   +
326                sizeof(GpuHidHaarStageClassifier) * cascade->count    +
327                sizeof(GpuHidHaarClassifier)      * total_classifiers +
328                sizeof(GpuHidHaarTreeNode)        * total_nodes;
329
330     *totalclassifier = total_classifiers;
331     *size = datasize;
332     out = (GpuHidHaarClassifierCascade *)cvAlloc( datasize );
333     memset( out, 0, sizeof(*out) );
334
335     /* init header */
336     out->count = cascade->count;
337     stage_classifier_ptr = (GpuHidHaarStageClassifier *)(out + 1);
338     haar_classifier_ptr = (GpuHidHaarClassifier *)(stage_classifier_ptr + cascade->count);
339     haar_node_ptr = (GpuHidHaarTreeNode *)(haar_classifier_ptr + total_classifiers);
340
341     out->is_stump_based = 1;
342     out->has_tilted_features = has_tilted_features;
343     out->is_tree = 0;
344
345     /* initialize internal representation */
346     for( i = 0; i < cascade->count; i++ )
347     {
348         CvHaarStageClassifier *stage_classifier = cascade->stage_classifier + i;
349         GpuHidHaarStageClassifier *hid_stage_classifier = stage_classifier_ptr + i;
350
351         hid_stage_classifier->count = stage_classifier->count;
352         hid_stage_classifier->threshold = stage_classifier->threshold - icv_stage_threshold_bias;
353         hid_stage_classifier->classifier = haar_classifier_ptr;
354         hid_stage_classifier->two_rects = 1;
355         haar_classifier_ptr += stage_classifier->count;
356
357         for( j = 0; j < stage_classifier->count; j++ )
358         {
359             CvHaarClassifier *classifier         = stage_classifier->classifier + j;
360             GpuHidHaarClassifier *hid_classifier = hid_stage_classifier->classifier + j;
361             int node_count = classifier->count;
362
363             float *alpha_ptr = &haar_node_ptr->alpha[0];
364
365             hid_classifier->count = node_count;
366             hid_classifier->node = haar_node_ptr;
367             hid_classifier->alpha = alpha_ptr;
368
369             for( l = 0; l < node_count; l++ )
370             {
371                 GpuHidHaarTreeNode *node     = hid_classifier->node + l;
372                 CvHaarFeature      *feature = classifier->haar_feature + l;
373
374                 memset( node, -1, sizeof(*node) );
375                 node->threshold = classifier->threshold[l];
376                 node->left      = classifier->left[l];
377                 node->right     = classifier->right[l];
378
379                 if( fabs(feature->rect[2].weight) < DBL_EPSILON ||
380                         feature->rect[2].r.width == 0 ||
381                         feature->rect[2].r.height == 0 )
382                 {
383                     node->p[2][0] = 0;
384                     node->p[2][1] = 0;
385                     node->p[2][2] = 0;
386                     node->p[2][3] = 0;
387                     node->weight[2] = 0;
388                 }
389                 else
390                     hid_stage_classifier->two_rects = 0;
391
392                 memcpy( node->alpha, classifier->alpha, (node_count + 1)*sizeof(alpha_ptr[0]));
393                 haar_node_ptr = haar_node_ptr + 1;
394             }
395             out->is_stump_based &= node_count == 1;
396         }
397     }
398
399     cascade->hid_cascade = (CvHidHaarClassifierCascade *)out;
400     assert( (char *)haar_node_ptr - (char *)out <= datasize );
401
402     return out;
403 }
404
405
406 #define sum_elem_ptr(sum,row,col)  \
407     ((sumtype*)CV_MAT_ELEM_PTR_FAST((sum),(row),(col),sizeof(sumtype)))
408
409 #define sqsum_elem_ptr(sqsum,row,col)  \
410     ((sqsumtype*)CV_MAT_ELEM_PTR_FAST((sqsum),(row),(col),sizeof(sqsumtype)))
411
412 #define calc_sum(rect,offset) \
413     ((rect).p0[offset] - (rect).p1[offset] - (rect).p2[offset] + (rect).p3[offset])
414
415
416 static void gpuSetImagesForHaarClassifierCascade( CvHaarClassifierCascade *_cascade,
417                                       double scale,
418                                       int step)
419 {
420     GpuHidHaarClassifierCascade *cascade;
421     int coi0 = 0, coi1 = 0;
422     int i;
423     int datasize;
424     int total;
425     CvRect equRect;
426     double weight_scale;
427     GpuHidHaarStageClassifier *stage_classifier;
428
429     if( !CV_IS_HAAR_CLASSIFIER(_cascade) )
430         CV_Error( !_cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier pointer" );
431
432     if( scale <= 0 )
433         CV_Error( CV_StsOutOfRange, "Scale must be positive" );
434
435     if( coi0 || coi1 )
436         CV_Error( CV_BadCOI, "COI is not supported" );
437
438     if( !_cascade->hid_cascade )
439         gpuCreateHidHaarClassifierCascade(_cascade, &datasize, &total);
440
441     cascade = (GpuHidHaarClassifierCascade *) _cascade->hid_cascade;
442     stage_classifier = (GpuHidHaarStageClassifier *) (cascade + 1);
443
444     _cascade->scale = scale;
445     _cascade->real_window_size.width = cvRound( _cascade->orig_window_size.width * scale );
446     _cascade->real_window_size.height = cvRound( _cascade->orig_window_size.height * scale );
447
448     equRect.x = equRect.y = cvRound(scale);
449     equRect.width = cvRound((_cascade->orig_window_size.width - 2) * scale);
450     equRect.height = cvRound((_cascade->orig_window_size.height - 2) * scale);
451     weight_scale = 1. / (equRect.width * equRect.height);
452     cascade->inv_window_area = weight_scale;
453
454     cascade->pq0 = equRect.x;
455     cascade->pq1 = equRect.y;
456     cascade->pq2 = equRect.x + equRect.width;
457     cascade->pq3 = equRect.y + equRect.height;
458
459     cascade->p0 = equRect.x;
460     cascade->p1 = equRect.y;
461     cascade->p2 = equRect.x + equRect.width;
462     cascade->p3 = equRect.y + equRect.height;
463
464
465     /* init pointers in haar features according to real window size and
466     given image pointers */
467     for( i = 0; i < _cascade->count; i++ )
468     {
469         int j, k, l;
470         for( j = 0; j < stage_classifier[i].count; j++ )
471         {
472             for( l = 0; l < stage_classifier[i].classifier[j].count; l++ )
473             {
474                 CvHaarFeature *feature =
475                     &_cascade->stage_classifier[i].classifier[j].haar_feature[l];
476                 GpuHidHaarTreeNode *hidnode = &stage_classifier[i].classifier[j].node[l];
477                 double sum0 = 0, area0 = 0;
478                 CvRect r[3];
479
480                 int base_w = -1, base_h = -1;
481                 int new_base_w = 0, new_base_h = 0;
482                 int kx, ky;
483                 int flagx = 0, flagy = 0;
484                 int x0 = 0, y0 = 0;
485                 int nr;
486
487                 /* align blocks */
488                 for( k = 0; k < CV_HAAR_FEATURE_MAX; k++ )
489                 {
490                     if(!hidnode->p[k][0])
491                         break;
492                     r[k] = feature->rect[k].r;
493                     base_w = (int)CV_IMIN( (unsigned)base_w, (unsigned)(r[k].width - 1) );
494                     base_w = (int)CV_IMIN( (unsigned)base_w, (unsigned)(r[k].x - r[0].x - 1) );
495                     base_h = (int)CV_IMIN( (unsigned)base_h, (unsigned)(r[k].height - 1) );
496                     base_h = (int)CV_IMIN( (unsigned)base_h, (unsigned)(r[k].y - r[0].y - 1) );
497                 }
498
499                 nr = k;
500                 base_w += 1;
501                 base_h += 1;
502                 if(base_w == 0)
503                     base_w = 1;
504                 kx = r[0].width / base_w;
505                 if(base_h == 0)
506                     base_h = 1;
507                 ky = r[0].height / base_h;
508
509                 if( kx <= 0 )
510                 {
511                     flagx = 1;
512                     new_base_w = cvRound( r[0].width * scale ) / kx;
513                     x0 = cvRound( r[0].x * scale );
514                 }
515
516                 if( ky <= 0 )
517                 {
518                     flagy = 1;
519                     new_base_h = cvRound( r[0].height * scale ) / ky;
520                     y0 = cvRound( r[0].y * scale );
521                 }
522
523                 for( k = 0; k < nr; k++ )
524                 {
525                     CvRect tr;
526                     double correction_ratio;
527
528                     if( flagx )
529                     {
530                         tr.x = (r[k].x - r[0].x) * new_base_w / base_w + x0;
531                         tr.width = r[k].width * new_base_w / base_w;
532                     }
533                     else
534                     {
535                         tr.x = cvRound( r[k].x * scale );
536                         tr.width = cvRound( r[k].width * scale );
537                     }
538
539                     if( flagy )
540                     {
541                         tr.y = (r[k].y - r[0].y) * new_base_h / base_h + y0;
542                         tr.height = r[k].height * new_base_h / base_h;
543                     }
544                     else
545                     {
546                         tr.y = cvRound( r[k].y * scale );
547                         tr.height = cvRound( r[k].height * scale );
548                     }
549
550 #if CV_ADJUST_WEIGHTS
551                     {
552                         // RAINER START
553                         const float orig_feature_size =  (float)(feature->rect[k].r.width) * feature->rect[k].r.height;
554                         const float orig_norm_size = (float)(_cascade->orig_window_size.width) * (_cascade->orig_window_size.height);
555                         const float feature_size = float(tr.width * tr.height);
556                         //const float normSize    = float(equRect.width*equRect.height);
557                         float target_ratio = orig_feature_size / orig_norm_size;
558                         //float isRatio = featureSize / normSize;
559                         //correctionRatio = targetRatio / isRatio / normSize;
560                         correction_ratio = target_ratio / feature_size;
561                         // RAINER END
562                     }
563 #else
564                     correction_ratio = weight_scale * (!feature->tilted ? 1 : 0.5);
565 #endif
566
567                     if( !feature->tilted )
568                     {
569                         hidnode->p[k][0] = tr.x;
570                         hidnode->p[k][1] = tr.y;
571                         hidnode->p[k][2] = tr.x + tr.width;
572                         hidnode->p[k][3] = tr.y + tr.height;
573                     }
574                     else
575                     {
576                         hidnode->p[k][2] = (tr.y + tr.width) * step + tr.x + tr.width;
577                         hidnode->p[k][3] = (tr.y + tr.width + tr.height) * step + tr.x + tr.width - tr.height;
578                         hidnode->p[k][0] = tr.y * step + tr.x;
579                         hidnode->p[k][1] = (tr.y + tr.height) * step + tr.x - tr.height;
580                     }
581                     hidnode->weight[k] = (float)(feature->rect[k].weight * correction_ratio);
582                     if( k == 0 )
583                         area0 = tr.width * tr.height;
584                     else
585                         sum0 += hidnode->weight[k] * tr.width * tr.height;
586                 }
587                 hidnode->weight[0] = (float)(-sum0 / area0);
588             } /* l */
589         } /* j */
590     }
591 }
592
593 static void gpuSetHaarClassifierCascade( CvHaarClassifierCascade *_cascade)
594 {
595     GpuHidHaarClassifierCascade *cascade;
596     int i;
597     int datasize;
598     int total;
599     CvRect equRect;
600     double weight_scale;
601     GpuHidHaarStageClassifier *stage_classifier;
602
603     if( !CV_IS_HAAR_CLASSIFIER(_cascade) )
604         CV_Error( !_cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier pointer" );
605
606     if( !_cascade->hid_cascade )
607         gpuCreateHidHaarClassifierCascade(_cascade, &datasize, &total);
608
609     cascade = (GpuHidHaarClassifierCascade *) _cascade->hid_cascade;
610     stage_classifier = (GpuHidHaarStageClassifier *) cascade + 1;
611
612     _cascade->scale = 1.0;
613     _cascade->real_window_size.width =  _cascade->orig_window_size.width ;
614     _cascade->real_window_size.height = _cascade->orig_window_size.height;
615
616     equRect.x = equRect.y = 1;
617     equRect.width = _cascade->orig_window_size.width - 2;
618     equRect.height = _cascade->orig_window_size.height - 2;
619     weight_scale = 1;
620     cascade->inv_window_area = weight_scale;
621
622     cascade->p0 = equRect.x;
623     cascade->p1 = equRect.y;
624     cascade->p2 = equRect.height;
625     cascade->p3 = equRect.width ;
626     for( i = 0; i < _cascade->count; i++ )
627     {
628         int j, l;
629         for( j = 0; j < stage_classifier[i].count; j++ )
630         {
631             for( l = 0; l < stage_classifier[i].classifier[j].count; l++ )
632             {
633                 const CvHaarFeature *feature =
634                     &_cascade->stage_classifier[i].classifier[j].haar_feature[l];
635                 GpuHidHaarTreeNode *hidnode = &stage_classifier[i].classifier[j].node[l];
636
637                 for( int k = 0; k < CV_HAAR_FEATURE_MAX; k++ )
638                 {
639                     const CvRect tr = feature->rect[k].r;
640                     if (tr.width == 0)
641                         break;
642                     double correction_ratio = weight_scale * (!feature->tilted ? 1 : 0.5);
643                     hidnode->p[k][0] = tr.x;
644                     hidnode->p[k][1] = tr.y;
645                     hidnode->p[k][2] = tr.width;
646                     hidnode->p[k][3] = tr.height;
647                     hidnode->weight[k] = (float)(feature->rect[k].weight * correction_ratio);
648                 }
649             } /* l */
650         } /* j */
651     }
652 }
653
654 CvSeq *cv::ocl::OclCascadeClassifier::oclHaarDetectObjects( oclMat &gimg, CvMemStorage *storage, double scaleFactor,
655         int minNeighbors, int flags, CvSize minSize, CvSize maxSize)
656 {
657     CvHaarClassifierCascade *cascade = oldCascade;
658
659     const double GROUP_EPS = 0.2;
660     CvSeq *result_seq = 0;
661
662     cv::ConcurrentRectVector allCandidates;
663     std::vector<cv::Rect> rectList;
664     std::vector<int> rweights;
665     double factor;
666     int datasize=0;
667     int totalclassifier=0;
668
669     GpuHidHaarClassifierCascade *gcascade;
670     GpuHidHaarStageClassifier    *stage;
671     GpuHidHaarClassifier         *classifier;
672     GpuHidHaarTreeNode           *node;
673
674     int *candidate;
675     cl_int status;
676
677     bool findBiggestObject = (flags & CV_HAAR_FIND_BIGGEST_OBJECT) != 0;
678
679     if( maxSize.height == 0 || maxSize.width == 0 )
680     {
681         maxSize.height = gimg.rows;
682         maxSize.width = gimg.cols;
683     }
684
685     if( !CV_IS_HAAR_CLASSIFIER(cascade) )
686         CV_Error( !cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier cascade" );
687
688     if( !storage )
689         CV_Error( CV_StsNullPtr, "Null storage pointer" );
690
691     if( CV_MAT_DEPTH(gimg.type()) != CV_8U )
692         CV_Error( CV_StsUnsupportedFormat, "Only 8-bit images are supported" );
693
694     if( scaleFactor <= 1 )
695         CV_Error( CV_StsOutOfRange, "scale factor must be > 1" );
696
697     if( findBiggestObject )
698         flags &= ~CV_HAAR_SCALE_IMAGE;
699
700     if( !cascade->hid_cascade )
701         gpuCreateHidHaarClassifierCascade(cascade, &datasize, &totalclassifier);
702
703     result_seq = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvAvgComp), storage );
704
705     if( CV_MAT_CN(gimg.type()) > 1 )
706     {
707         oclMat gtemp;
708         cvtColor( gimg, gtemp, CV_BGR2GRAY );
709         gimg = gtemp;
710     }
711
712     if( findBiggestObject )
713         flags &= ~(CV_HAAR_SCALE_IMAGE | CV_HAAR_DO_CANNY_PRUNING);
714
715     if( gimg.cols < minSize.width || gimg.rows < minSize.height )
716         CV_Error(CV_StsError, "Image too small");
717
718     cl_command_queue qu = getClCommandQueue(Context::getContext());
719     if( (flags & CV_HAAR_SCALE_IMAGE) )
720     {
721         CvSize winSize0 = cascade->orig_window_size;
722         int totalheight = 0;
723         int indexy = 0;
724         CvSize sz;
725         vector<CvSize> sizev;
726         vector<float> scalev;
727         for(factor = 1.f;; factor *= scaleFactor)
728         {
729             CvSize winSize = { cvRound(winSize0.width * factor), cvRound(winSize0.height * factor) };
730             sz.width     = cvRound( gimg.cols / factor ) + 1;
731             sz.height    = cvRound( gimg.rows / factor ) + 1;
732             CvSize sz1     = { sz.width - winSize0.width - 1,      sz.height - winSize0.height - 1 };
733
734             if( sz1.width <= 0 || sz1.height <= 0 )
735                 break;
736             if( winSize.width > maxSize.width || winSize.height > maxSize.height )
737                 break;
738             if( winSize.width < minSize.width || winSize.height < minSize.height )
739                 continue;
740
741             totalheight += sz.height;
742             sizev.push_back(sz);
743             scalev.push_back(factor);
744         }
745
746         oclMat gimg1(gimg.rows, gimg.cols, CV_8UC1);
747         oclMat gsum(totalheight + 4, gimg.cols + 1, CV_32SC1);
748         oclMat gsqsum(totalheight + 4, gimg.cols + 1, CV_32FC1);
749
750         int sdepth = 0;
751         if(Context::getContext()->supportsFeature(FEATURE_CL_DOUBLE))
752             sdepth = CV_64FC1;
753         else
754             sdepth = CV_32FC1;
755         sdepth = CV_MAT_DEPTH(sdepth);
756         int type = CV_MAKE_TYPE(sdepth, 1);
757         oclMat gsqsum_t(totalheight + 4, gimg.cols + 1, type);
758
759         cl_mem stagebuffer;
760         cl_mem nodebuffer;
761         cl_mem candidatebuffer;
762         cl_mem scaleinfobuffer;
763         cv::Rect roi, roi2;
764         cv::Mat imgroi, imgroisq;
765         cv::ocl::oclMat resizeroi, gimgroi, gimgroisq;
766
767         int grp_per_CU = 12;
768
769         size_t blocksize = 8;
770         size_t localThreads[3] = { blocksize, blocksize , 1 };
771         size_t globalThreads[3] = { grp_per_CU *(gsum.clCxt->getDeviceInfo().maxComputeUnits) *localThreads[0],
772                                     localThreads[1], 1
773                                   };
774         int outputsz = 256 * globalThreads[0] / localThreads[0];
775         int loopcount = sizev.size();
776         detect_piramid_info *scaleinfo = (detect_piramid_info *)malloc(sizeof(detect_piramid_info) * loopcount);
777
778         for( int i = 0; i < loopcount; i++ )
779         {
780             sz = sizev[i];
781             factor = scalev[i];
782             roi = Rect(0, indexy, sz.width, sz.height);
783             roi2 = Rect(0, 0, sz.width - 1, sz.height - 1);
784             resizeroi = gimg1(roi2);
785             gimgroi = gsum(roi);
786             gimgroisq = gsqsum_t(roi);
787             int width = gimgroi.cols - 1 - cascade->orig_window_size.width;
788             int height = gimgroi.rows - 1 - cascade->orig_window_size.height;
789             scaleinfo[i].width_height = (width << 16) | height;
790
791
792             int grpnumperline = (width + localThreads[0] - 1) / localThreads[0];
793             int totalgrp = ((height + localThreads[1] - 1) / localThreads[1]) * grpnumperline;
794
795             scaleinfo[i].grpnumperline_totalgrp = (grpnumperline << 16) | totalgrp;
796             scaleinfo[i].imgoff = gimgroi.offset >> 2;
797             scaleinfo[i].factor = factor;
798             cv::ocl::resize(gimg, resizeroi, Size(sz.width - 1, sz.height - 1), 0, 0, INTER_LINEAR);
799             cv::ocl::integral(resizeroi, gimgroi, gimgroisq);
800
801             indexy += sz.height;
802         }
803         if(gsqsum_t.depth() == CV_64F)
804             gsqsum_t.convertTo(gsqsum, CV_32FC1);
805         else
806             gsqsum = gsqsum_t;
807
808         gcascade   = (GpuHidHaarClassifierCascade *)cascade->hid_cascade;
809         stage      = (GpuHidHaarStageClassifier *)(gcascade + 1);
810         classifier = (GpuHidHaarClassifier *)(stage + gcascade->count);
811         node       = (GpuHidHaarTreeNode *)(classifier->node);
812
813         int nodenum = (datasize - sizeof(GpuHidHaarClassifierCascade) -
814                        sizeof(GpuHidHaarStageClassifier) * gcascade->count - sizeof(GpuHidHaarClassifier) * totalclassifier) / sizeof(GpuHidHaarTreeNode);
815
816         candidate = (int *)malloc(4 * sizeof(int) * outputsz);
817
818         gpuSetImagesForHaarClassifierCascade( cascade, 1., gsum.step / 4 );
819
820         stagebuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_READ_ONLY, sizeof(GpuHidHaarStageClassifier) * gcascade->count);
821         openCLSafeCall(clEnqueueWriteBuffer(qu, stagebuffer, 1, 0, sizeof(GpuHidHaarStageClassifier)*gcascade->count, stage, 0, NULL, NULL));
822
823         nodebuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_READ_ONLY, nodenum * sizeof(GpuHidHaarTreeNode));
824
825         openCLSafeCall(clEnqueueWriteBuffer(qu, nodebuffer, 1, 0, nodenum * sizeof(GpuHidHaarTreeNode),
826                                             node, 0, NULL, NULL));
827         candidatebuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_WRITE_ONLY, 4 * sizeof(int) * outputsz);
828
829         scaleinfobuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_READ_ONLY, sizeof(detect_piramid_info) * loopcount);
830         openCLSafeCall(clEnqueueWriteBuffer(qu, scaleinfobuffer, 1, 0, sizeof(detect_piramid_info)*loopcount, scaleinfo, 0, NULL, NULL));
831
832         int startstage = 0;
833         int endstage = gcascade->count;
834         int startnode = 0;
835         int pixelstep = gsum.step / 4;
836         int splitstage = 3;
837         int splitnode = stage[0].count + stage[1].count + stage[2].count;
838         cl_int4 p, pq;
839         p.s[0] = gcascade->p0;
840         p.s[1] = gcascade->p1;
841         p.s[2] = gcascade->p2;
842         p.s[3] = gcascade->p3;
843         pq.s[0] = gcascade->pq0;
844         pq.s[1] = gcascade->pq1;
845         pq.s[2] = gcascade->pq2;
846         pq.s[3] = gcascade->pq3;
847         float correction = gcascade->inv_window_area;
848
849         vector<pair<size_t, const void *> > args;
850         args.push_back ( make_pair(sizeof(cl_mem) , (void *)&stagebuffer ));
851         args.push_back ( make_pair(sizeof(cl_mem) , (void *)&scaleinfobuffer ));
852         args.push_back ( make_pair(sizeof(cl_mem) , (void *)&nodebuffer ));
853         args.push_back ( make_pair(sizeof(cl_mem) , (void *)&gsum.data ));
854         args.push_back ( make_pair(sizeof(cl_mem) , (void *)&gsqsum.data ));
855         args.push_back ( make_pair(sizeof(cl_mem) , (void *)&candidatebuffer ));
856         args.push_back ( make_pair(sizeof(cl_int) , (void *)&pixelstep ));
857         args.push_back ( make_pair(sizeof(cl_int) , (void *)&loopcount ));
858         args.push_back ( make_pair(sizeof(cl_int) , (void *)&startstage ));
859         args.push_back ( make_pair(sizeof(cl_int) , (void *)&splitstage ));
860         args.push_back ( make_pair(sizeof(cl_int) , (void *)&endstage ));
861         args.push_back ( make_pair(sizeof(cl_int) , (void *)&startnode ));
862         args.push_back ( make_pair(sizeof(cl_int) , (void *)&splitnode ));
863         args.push_back ( make_pair(sizeof(cl_int4) , (void *)&p ));
864         args.push_back ( make_pair(sizeof(cl_int4) , (void *)&pq ));
865         args.push_back ( make_pair(sizeof(cl_float) , (void *)&correction ));
866
867         if(gcascade->is_stump_based && gsum.clCxt->supportsFeature(FEATURE_CL_INTEL_DEVICE))
868         {
869             //setup local group size
870             localThreads[0] = 8;
871             localThreads[1] = 16;
872             localThreads[2] = 1;
873
874             //init maximal number of workgroups
875             int WGNumX = 1+(sizev[0].width /(localThreads[0]));
876             int WGNumY = 1+(sizev[0].height/(localThreads[1]));
877             int WGNumZ = loopcount;
878             int WGNum = 0; //accurate number of non -empty workgroups
879             oclMat      oclWGInfo(1,sizeof(cl_int4) * WGNumX*WGNumY*WGNumZ,CV_8U);
880             {
881                 cl_int4*    pWGInfo = (cl_int4*)clEnqueueMapBuffer(getClCommandQueue(oclWGInfo.clCxt),(cl_mem)oclWGInfo.datastart,true,CL_MAP_WRITE, 0, oclWGInfo.step, 0,0,0,&status);
882                 openCLVerifyCall(status);
883                 for(int z=0;z<WGNumZ;++z)
884                 {
885                     int     Width  = (scaleinfo[z].width_height >> 16)&0xFFFF;
886                     int     Height = (scaleinfo[z].width_height >> 0 )& 0xFFFF;
887                     for(int y=0;y<WGNumY;++y)
888                     {
889                         int     gy = y*localThreads[1];
890                         if(gy>=(Height-cascade->orig_window_size.height))
891                             continue; // no data to process
892                         for(int x=0;x<WGNumX;++x)
893                         {
894                             int     gx = x*localThreads[0];
895                             if(gx>=(Width-cascade->orig_window_size.width))
896                                 continue; // no data to process
897
898                             // save no-empty workgroup info into array
899                             pWGInfo[WGNum].s[0] = scaleinfo[z].width_height;
900                             pWGInfo[WGNum].s[1] = (gx << 16) | gy;
901                             pWGInfo[WGNum].s[2] = scaleinfo[z].imgoff;
902                             memcpy(&(pWGInfo[WGNum].s[3]),&(scaleinfo[z].factor),sizeof(float));
903                             WGNum++;
904                         }
905                     }
906                 }
907                 openCLSafeCall(clEnqueueUnmapMemObject(getClCommandQueue(oclWGInfo.clCxt),(cl_mem)oclWGInfo.datastart,pWGInfo,0,0,0));
908                 pWGInfo = NULL;
909             }
910
911             // setup global sizes to have linear array of workgroups with WGNum size
912             globalThreads[0] = localThreads[0]*WGNum;
913             globalThreads[1] = localThreads[1];
914             globalThreads[2] = 1;
915
916 #define NODE_SIZE 12
917             // pack node info to have less memory loads
918             oclMat  oclNodesPK(1,sizeof(cl_int) * NODE_SIZE * nodenum,CV_8U);
919             {
920                 cl_int  status;
921                 cl_int* pNodesPK = (cl_int*)clEnqueueMapBuffer(getClCommandQueue(oclNodesPK.clCxt),(cl_mem)oclNodesPK.datastart,true,CL_MAP_WRITE, 0, oclNodesPK.step, 0,0,0,&status);
922                 openCLVerifyCall(status);
923                 //use known local data stride to precalulate indexes
924                 int DATA_SIZE_X = (localThreads[0]+cascade->orig_window_size.width);
925                 // check that maximal value is less than maximal unsigned short
926                 assert(DATA_SIZE_X*cascade->orig_window_size.height+cascade->orig_window_size.width < USHRT_MAX);
927                 for(int i = 0;i<nodenum;++i)
928                 {//process each node from classifier
929                     struct NodePK
930                     {
931                         unsigned short  slm_index[3][4];
932                         float           weight[3];
933                         float           threshold;
934                         float           alpha[2];
935                     };
936                     struct NodePK * pOut = (struct NodePK *)(pNodesPK + NODE_SIZE*i);
937                     for(int k=0;k<3;++k)
938                     {// calc 4 short indexes in shared local mem for each rectangle instead of 2 (x,y) pair.
939                         int* p = &(node[i].p[k][0]);
940                         pOut->slm_index[k][0] = (unsigned short)(p[1]*DATA_SIZE_X+p[0]);
941                         pOut->slm_index[k][1] = (unsigned short)(p[1]*DATA_SIZE_X+p[2]);
942                         pOut->slm_index[k][2] = (unsigned short)(p[3]*DATA_SIZE_X+p[0]);
943                         pOut->slm_index[k][3] = (unsigned short)(p[3]*DATA_SIZE_X+p[2]);
944                     }
945                     //store used float point values for each node
946                     pOut->weight[0] = node[i].weight[0];
947                     pOut->weight[1] = node[i].weight[1];
948                     pOut->weight[2] = node[i].weight[2];
949                     pOut->threshold = node[i].threshold;
950                     pOut->alpha[0] = node[i].alpha[0];
951                     pOut->alpha[1] = node[i].alpha[1];
952                 }
953                 openCLSafeCall(clEnqueueUnmapMemObject(getClCommandQueue(oclNodesPK.clCxt),(cl_mem)oclNodesPK.datastart,pNodesPK,0,0,0));
954                 pNodesPK = NULL;
955             }
956             // add 2 additional buffers (WGinfo and packed nodes) as 2 last args
957             args.push_back ( make_pair(sizeof(cl_mem) , (void *)&oclNodesPK.datastart ));
958             args.push_back ( make_pair(sizeof(cl_mem) , (void *)&oclWGInfo.datastart ));
959
960             //form build options for kernel
961             string  options = "-D PACKED_CLASSIFIER";
962             options += format(" -D NODE_SIZE=%d",NODE_SIZE);
963             options += format(" -D WND_SIZE_X=%d",cascade->orig_window_size.width);
964             options += format(" -D WND_SIZE_Y=%d",cascade->orig_window_size.height);
965             options += format(" -D STUMP_BASED=%d",gcascade->is_stump_based);
966             options += format(" -D LSx=%d",localThreads[0]);
967             options += format(" -D LSy=%d",localThreads[1]);
968             options += format(" -D SPLITNODE=%d",splitnode);
969             options += format(" -D SPLITSTAGE=%d",splitstage);
970             options += format(" -D OUTPUTSZ=%d",outputsz);
971
972             // init candiate global count by 0
973             int pattern = 0;
974             openCLSafeCall(clEnqueueWriteBuffer(qu, candidatebuffer, 1, 0, 1 * sizeof(pattern),&pattern, 0, NULL, NULL));
975             // execute face detector
976             openCLExecuteKernel(gsum.clCxt, &haarobjectdetect, "gpuRunHaarClassifierCascadePacked", globalThreads, localThreads, args, -1, -1, options.c_str());
977             //read candidate buffer back and put it into host list
978             openCLReadBuffer( gsum.clCxt, candidatebuffer, candidate, 4 * sizeof(int)*outputsz );
979             assert(candidate[0]<outputsz);
980             //printf("candidate[0]=%d\n",candidate[0]);
981             for(int i = 1; i <= candidate[0]; i++)
982             {
983                 allCandidates.push_back(Rect(candidate[4 * i], candidate[4 * i + 1],candidate[4 * i + 2], candidate[4 * i + 3]));
984             }
985         }
986         else
987         {
988             const char * build_options = gcascade->is_stump_based ? "-D STUMP_BASED=1" : "-D STUMP_BASED=0";
989
990             openCLExecuteKernel(gsum.clCxt, &haarobjectdetect, "gpuRunHaarClassifierCascade", globalThreads, localThreads, args, -1, -1, build_options);
991
992             openCLReadBuffer( gsum.clCxt, candidatebuffer, candidate, 4 * sizeof(int)*outputsz );
993
994             for(int i = 0; i < outputsz; i++)
995                 if(candidate[4 * i + 2] != 0)
996                     allCandidates.push_back(Rect(candidate[4 * i], candidate[4 * i + 1],
997                     candidate[4 * i + 2], candidate[4 * i + 3]));
998         }
999
1000         free(scaleinfo);
1001         free(candidate);
1002         openCLSafeCall(clReleaseMemObject(stagebuffer));
1003         openCLSafeCall(clReleaseMemObject(scaleinfobuffer));
1004         openCLSafeCall(clReleaseMemObject(nodebuffer));
1005         openCLSafeCall(clReleaseMemObject(candidatebuffer));
1006
1007     }
1008     else
1009     {
1010         CvSize winsize0 = cascade->orig_window_size;
1011         int n_factors = 0;
1012         oclMat gsum;
1013         oclMat gsqsum;
1014         oclMat gsqsum_t;
1015         cv::ocl::integral(gimg, gsum, gsqsum_t);
1016         if(gsqsum_t.depth() == CV_64F)
1017             gsqsum_t.convertTo(gsqsum, CV_32FC1);
1018         else
1019             gsqsum = gsqsum_t;
1020         CvSize sz;
1021         vector<CvSize> sizev;
1022         vector<float> scalev;
1023         gpuSetHaarClassifierCascade(cascade);
1024         gcascade   = (GpuHidHaarClassifierCascade *)cascade->hid_cascade;
1025         stage      = (GpuHidHaarStageClassifier *)(gcascade + 1);
1026         classifier = (GpuHidHaarClassifier *)(stage + gcascade->count);
1027         node       = (GpuHidHaarTreeNode *)(classifier->node);
1028         cl_mem stagebuffer;
1029         cl_mem nodebuffer;
1030         cl_mem candidatebuffer;
1031         cl_mem scaleinfobuffer;
1032         cl_mem pbuffer;
1033         cl_mem correctionbuffer;
1034         for( n_factors = 0, factor = 1;
1035                 cvRound(factor * winsize0.width) < gimg.cols - 10 &&
1036                 cvRound(factor * winsize0.height) < gimg.rows - 10;
1037                 n_factors++, factor *= scaleFactor )
1038         {
1039             CvSize winSize = { cvRound( winsize0.width * factor ),
1040                                cvRound( winsize0.height * factor )
1041                              };
1042             if( winSize.width < minSize.width || winSize.height < minSize.height )
1043             {
1044                 continue;
1045             }
1046             sizev.push_back(winSize);
1047             scalev.push_back(factor);
1048         }
1049         int loopcount = scalev.size();
1050         if(loopcount == 0)
1051         {
1052             loopcount = 1;
1053             n_factors = 1;
1054             sizev.push_back(minSize);
1055             scalev.push_back( std::min(cvRound(minSize.width / winsize0.width), cvRound(minSize.height / winsize0.height)) );
1056
1057         }
1058         detect_piramid_info *scaleinfo = (detect_piramid_info *)malloc(sizeof(detect_piramid_info) * loopcount);
1059         cl_int4 *p = (cl_int4 *)malloc(sizeof(cl_int4) * loopcount);
1060         float *correction = (float *)malloc(sizeof(float) * loopcount);
1061         int grp_per_CU = 12;
1062         size_t blocksize = 8;
1063         size_t localThreads[3] = { blocksize, blocksize , 1 };
1064         size_t globalThreads[3] = { grp_per_CU *gsum.clCxt->getDeviceInfo().maxComputeUnits *localThreads[0],
1065                                     localThreads[1], 1 };
1066         int outputsz = 256 * globalThreads[0] / localThreads[0];
1067         int nodenum = (datasize - sizeof(GpuHidHaarClassifierCascade) -
1068                        sizeof(GpuHidHaarStageClassifier) * gcascade->count - sizeof(GpuHidHaarClassifier) * totalclassifier) / sizeof(GpuHidHaarTreeNode);
1069         nodebuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_READ_ONLY,
1070                                         nodenum * sizeof(GpuHidHaarTreeNode));
1071         openCLSafeCall(clEnqueueWriteBuffer(qu, nodebuffer, 1, 0,
1072                                             nodenum * sizeof(GpuHidHaarTreeNode),
1073                                             node, 0, NULL, NULL));
1074         cl_mem newnodebuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_READ_WRITE,
1075                                loopcount * nodenum * sizeof(GpuHidHaarTreeNode));
1076         int startstage = 0;
1077         int endstage = gcascade->count;
1078         for(int i = 0; i < loopcount; i++)
1079         {
1080             sz = sizev[i];
1081             factor = scalev[i];
1082             double ystep = std::max(2., factor);
1083             int equRect_x = cvRound(factor * gcascade->p0);
1084             int equRect_y = cvRound(factor * gcascade->p1);
1085             int equRect_w = cvRound(factor * gcascade->p3);
1086             int equRect_h = cvRound(factor * gcascade->p2);
1087             p[i].s[0] = equRect_x;
1088             p[i].s[1] = equRect_y;
1089             p[i].s[2] = equRect_x + equRect_w;
1090             p[i].s[3] = equRect_y + equRect_h;
1091             correction[i] = 1. / (equRect_w * equRect_h);
1092             int width = (gsum.cols - 1 - sz.width  + ystep - 1) / ystep;
1093             int height = (gsum.rows - 1 - sz.height + ystep - 1) / ystep;
1094             int grpnumperline = (width + localThreads[0] - 1) / localThreads[0];
1095             int totalgrp = ((height + localThreads[1] - 1) / localThreads[1]) * grpnumperline;
1096
1097             scaleinfo[i].width_height = (width << 16) | height;
1098             scaleinfo[i].grpnumperline_totalgrp = (grpnumperline << 16) | totalgrp;
1099             scaleinfo[i].imgoff = 0;
1100             scaleinfo[i].factor = factor;
1101             int startnodenum = nodenum * i;
1102             float factor2 = (float)factor;
1103
1104             vector<pair<size_t, const void *> > args1;
1105             args1.push_back ( make_pair(sizeof(cl_mem) , (void *)&nodebuffer ));
1106             args1.push_back ( make_pair(sizeof(cl_mem) , (void *)&newnodebuffer ));
1107             args1.push_back ( make_pair(sizeof(cl_float) , (void *)&factor2 ));
1108             args1.push_back ( make_pair(sizeof(cl_float) , (void *)&correction[i] ));
1109             args1.push_back ( make_pair(sizeof(cl_int) , (void *)&startnodenum ));
1110
1111             size_t globalThreads2[3] = {nodenum, 1, 1};
1112             openCLExecuteKernel(gsum.clCxt, &haarobjectdetect_scaled2, "gpuscaleclassifier", globalThreads2, NULL/*localThreads2*/, args1, -1, -1);
1113         }
1114
1115         int step = gsum.step / 4;
1116         int startnode = 0;
1117         int splitstage = 3;
1118         stagebuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_READ_ONLY, sizeof(GpuHidHaarStageClassifier) * gcascade->count);
1119         openCLSafeCall(clEnqueueWriteBuffer(qu, stagebuffer, 1, 0, sizeof(GpuHidHaarStageClassifier)*gcascade->count, stage, 0, NULL, NULL));
1120         candidatebuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_WRITE_ONLY | CL_MEM_ALLOC_HOST_PTR, 4 * sizeof(int) * outputsz);
1121         scaleinfobuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_READ_ONLY, sizeof(detect_piramid_info) * loopcount);
1122         openCLSafeCall(clEnqueueWriteBuffer(qu, scaleinfobuffer, 1, 0, sizeof(detect_piramid_info)*loopcount, scaleinfo, 0, NULL, NULL));
1123         pbuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_READ_ONLY, sizeof(cl_int4) * loopcount);
1124         openCLSafeCall(clEnqueueWriteBuffer(qu, pbuffer, 1, 0, sizeof(cl_int4)*loopcount, p, 0, NULL, NULL));
1125         correctionbuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_READ_ONLY, sizeof(cl_float) * loopcount);
1126         openCLSafeCall(clEnqueueWriteBuffer(qu, correctionbuffer, 1, 0, sizeof(cl_float)*loopcount, correction, 0, NULL, NULL));
1127
1128         vector<pair<size_t, const void *> > args;
1129         args.push_back ( make_pair(sizeof(cl_mem) , (void *)&stagebuffer ));
1130         args.push_back ( make_pair(sizeof(cl_mem) , (void *)&scaleinfobuffer ));
1131         args.push_back ( make_pair(sizeof(cl_mem) , (void *)&newnodebuffer ));
1132         args.push_back ( make_pair(sizeof(cl_mem) , (void *)&gsum.data ));
1133         args.push_back ( make_pair(sizeof(cl_mem) , (void *)&gsqsum.data ));
1134         args.push_back ( make_pair(sizeof(cl_mem) , (void *)&candidatebuffer ));
1135         args.push_back ( make_pair(sizeof(cl_int) , (void *)&gsum.rows ));
1136         args.push_back ( make_pair(sizeof(cl_int) , (void *)&gsum.cols ));
1137         args.push_back ( make_pair(sizeof(cl_int) , (void *)&step ));
1138         args.push_back ( make_pair(sizeof(cl_int) , (void *)&loopcount ));
1139         args.push_back ( make_pair(sizeof(cl_int) , (void *)&startstage ));
1140         args.push_back ( make_pair(sizeof(cl_int) , (void *)&splitstage ));
1141         args.push_back ( make_pair(sizeof(cl_int) , (void *)&endstage ));
1142         args.push_back ( make_pair(sizeof(cl_int) , (void *)&startnode ));
1143         args.push_back ( make_pair(sizeof(cl_mem) , (void *)&pbuffer ));
1144         args.push_back ( make_pair(sizeof(cl_mem) , (void *)&correctionbuffer ));
1145         args.push_back ( make_pair(sizeof(cl_int) , (void *)&nodenum ));
1146         const char * build_options = gcascade->is_stump_based ? "-D STUMP_BASED=1" : "-D STUMP_BASED=0";
1147         openCLExecuteKernel(gsum.clCxt, &haarobjectdetect_scaled2, "gpuRunHaarClassifierCascade_scaled2", globalThreads, localThreads, args, -1, -1, build_options);
1148
1149         candidate = (int *)clEnqueueMapBuffer(qu, candidatebuffer, 1, CL_MAP_READ, 0, 4 * sizeof(int) * outputsz, 0, 0, 0, &status);
1150
1151         for(int i = 0; i < outputsz; i++)
1152         {
1153             if(candidate[4 * i + 2] != 0)
1154                 allCandidates.push_back(Rect(candidate[4 * i], candidate[4 * i + 1], candidate[4 * i + 2], candidate[4 * i + 3]));
1155         }
1156
1157         free(scaleinfo);
1158         free(p);
1159         free(correction);
1160         clEnqueueUnmapMemObject(qu, candidatebuffer, candidate, 0, 0, 0);
1161         openCLSafeCall(clReleaseMemObject(stagebuffer));
1162         openCLSafeCall(clReleaseMemObject(scaleinfobuffer));
1163         openCLSafeCall(clReleaseMemObject(nodebuffer));
1164         openCLSafeCall(clReleaseMemObject(newnodebuffer));
1165         openCLSafeCall(clReleaseMemObject(candidatebuffer));
1166         openCLSafeCall(clReleaseMemObject(pbuffer));
1167         openCLSafeCall(clReleaseMemObject(correctionbuffer));
1168     }
1169
1170     cvFree(&cascade->hid_cascade);
1171     rectList.resize(allCandidates.size());
1172     if(!allCandidates.empty())
1173         std::copy(allCandidates.begin(), allCandidates.end(), rectList.begin());
1174
1175     if( minNeighbors != 0 || findBiggestObject )
1176         groupRectangles(rectList, rweights, std::max(minNeighbors, 1), GROUP_EPS);
1177     else
1178         rweights.resize(rectList.size(), 0);
1179
1180     if( findBiggestObject && rectList.size() )
1181     {
1182         CvAvgComp result_comp = {{0, 0, 0, 0}, 0};
1183
1184         for( size_t i = 0; i < rectList.size(); i++ )
1185         {
1186             cv::Rect r = rectList[i];
1187             if( r.area() > cv::Rect(result_comp.rect).area() )
1188             {
1189                 result_comp.rect = r;
1190                 result_comp.neighbors = rweights[i];
1191             }
1192         }
1193         cvSeqPush( result_seq, &result_comp );
1194     }
1195     else
1196     {
1197         for( size_t i = 0; i < rectList.size(); i++ )
1198         {
1199             CvAvgComp c;
1200             c.rect = rectList[i];
1201             c.neighbors = rweights[i];
1202             cvSeqPush( result_seq, &c );
1203         }
1204     }
1205
1206     return result_seq;
1207 }
1208
1209
1210 struct getRect
1211 {
1212     Rect operator()(const CvAvgComp &e) const
1213     {
1214         return e.rect;
1215     }
1216 };
1217
1218 void cv::ocl::OclCascadeClassifier::detectMultiScale(oclMat &gimg, CV_OUT std::vector<cv::Rect>& faces,
1219                                                         double scaleFactor, int minNeighbors, int flags,
1220                                                         Size minSize, Size maxSize)
1221 {
1222     CvSeq* _objects;
1223     MemStorage storage(cvCreateMemStorage(0));
1224     _objects = oclHaarDetectObjects(gimg, storage, scaleFactor, minNeighbors, flags, minSize, maxSize);
1225     vector<CvAvgComp> vecAvgComp;
1226     Seq<CvAvgComp>(_objects).copyTo(vecAvgComp);
1227     faces.resize(vecAvgComp.size());
1228     std::transform(vecAvgComp.begin(), vecAvgComp.end(), faces.begin(), getRect());
1229 }
1230
1231 struct OclBuffers
1232 {
1233     cl_mem stagebuffer;
1234     cl_mem nodebuffer;
1235     cl_mem candidatebuffer;
1236     cl_mem scaleinfobuffer;
1237     cl_mem pbuffer;
1238     cl_mem correctionbuffer;
1239     cl_mem newnodebuffer;
1240 };
1241
1242
1243 void cv::ocl::OclCascadeClassifierBuf::detectMultiScale(oclMat &gimg, CV_OUT std::vector<cv::Rect>& faces,
1244                                                         double scaleFactor, int minNeighbors, int flags,
1245                                                         Size minSize, Size maxSize)
1246 {
1247     int blocksize = 8;
1248     int grp_per_CU = 12;
1249     size_t localThreads[3] = { blocksize, blocksize, 1 };
1250     size_t globalThreads[3] = { grp_per_CU * cv::ocl::Context::getContext()->getDeviceInfo().maxComputeUnits *localThreads[0],
1251         localThreads[1],
1252         1 };
1253     int outputsz = 256 * globalThreads[0] / localThreads[0];
1254
1255     Init(gimg.rows, gimg.cols, scaleFactor, flags, outputsz, localThreads, minSize, maxSize);
1256
1257     const double GROUP_EPS = 0.2;
1258
1259     cv::ConcurrentRectVector allCandidates;
1260     std::vector<cv::Rect> rectList;
1261     std::vector<int> rweights;
1262
1263     CvHaarClassifierCascade      *cascade = oldCascade;
1264     GpuHidHaarClassifierCascade  *gcascade;
1265     GpuHidHaarStageClassifier    *stage;
1266
1267     if( CV_MAT_DEPTH(gimg.type()) != CV_8U )
1268         CV_Error( CV_StsUnsupportedFormat, "Only 8-bit images are supported" );
1269
1270     if( CV_MAT_CN(gimg.type()) > 1 )
1271     {
1272         oclMat gtemp;
1273         cvtColor( gimg, gtemp, CV_BGR2GRAY );
1274         gimg = gtemp;
1275     }
1276
1277     int *candidate;
1278     cl_command_queue qu = getClCommandQueue(Context::getContext());
1279     if( (flags & CV_HAAR_SCALE_IMAGE) )
1280     {
1281         int indexy = 0;
1282         CvSize sz;
1283
1284         cv::Rect roi, roi2;
1285         cv::ocl::oclMat resizeroi, gimgroi, gimgroisq;
1286
1287         for( int i = 0; i < m_loopcount; i++ )
1288         {
1289             sz = sizev[i];
1290             roi = Rect(0, indexy, sz.width, sz.height);
1291             roi2 = Rect(0, 0, sz.width - 1, sz.height - 1);
1292             resizeroi = gimg1(roi2);
1293             gimgroi = gsum(roi);
1294             gimgroisq = gsqsum_t(roi);
1295
1296             cv::ocl::resize(gimg, resizeroi, Size(sz.width - 1, sz.height - 1), 0, 0, INTER_LINEAR);
1297             cv::ocl::integral(resizeroi, gimgroi, gimgroisq);
1298             indexy += sz.height;
1299         }
1300         if(gsqsum_t.depth() == CV_64F)
1301             gsqsum_t.convertTo(gsqsum, CV_32FC1);
1302         else
1303             gsqsum = gsqsum_t;
1304
1305         gcascade   = (GpuHidHaarClassifierCascade *)(cascade->hid_cascade);
1306         stage      = (GpuHidHaarStageClassifier *)(gcascade + 1);
1307
1308         int startstage = 0;
1309         int endstage = gcascade->count;
1310         int startnode = 0;
1311         int pixelstep = gsum.step / 4;
1312         int splitstage = 3;
1313         int splitnode = stage[0].count + stage[1].count + stage[2].count;
1314         cl_int4 p, pq;
1315         p.s[0] = gcascade->p0;
1316         p.s[1] = gcascade->p1;
1317         p.s[2] = gcascade->p2;
1318         p.s[3] = gcascade->p3;
1319         pq.s[0] = gcascade->pq0;
1320         pq.s[1] = gcascade->pq1;
1321         pq.s[2] = gcascade->pq2;
1322         pq.s[3] = gcascade->pq3;
1323         float correction = gcascade->inv_window_area;
1324
1325         vector<pair<size_t, const void *> > args;
1326         args.push_back ( make_pair(sizeof(cl_mem) , (void *)&((OclBuffers *)buffers)->stagebuffer ));
1327         args.push_back ( make_pair(sizeof(cl_mem) , (void *)&((OclBuffers *)buffers)->scaleinfobuffer ));
1328         args.push_back ( make_pair(sizeof(cl_mem) , (void *)&((OclBuffers *)buffers)->nodebuffer ));
1329         args.push_back ( make_pair(sizeof(cl_mem) , (void *)&gsum.data ));
1330         args.push_back ( make_pair(sizeof(cl_mem) , (void *)&gsqsum.data ));
1331         args.push_back ( make_pair(sizeof(cl_mem) , (void *)&((OclBuffers *)buffers)->candidatebuffer ));
1332         args.push_back ( make_pair(sizeof(cl_int) , (void *)&pixelstep ));
1333         args.push_back ( make_pair(sizeof(cl_int) , (void *)&m_loopcount ));
1334         args.push_back ( make_pair(sizeof(cl_int) , (void *)&startstage ));
1335         args.push_back ( make_pair(sizeof(cl_int) , (void *)&splitstage ));
1336         args.push_back ( make_pair(sizeof(cl_int) , (void *)&endstage ));
1337         args.push_back ( make_pair(sizeof(cl_int) , (void *)&startnode ));
1338         args.push_back ( make_pair(sizeof(cl_int) , (void *)&splitnode ));
1339         args.push_back ( make_pair(sizeof(cl_int4) , (void *)&p ));
1340         args.push_back ( make_pair(sizeof(cl_int4) , (void *)&pq ));
1341         args.push_back ( make_pair(sizeof(cl_float) , (void *)&correction ));
1342
1343         const char * build_options = gcascade->is_stump_based ? "-D STUMP_BASED=1" : "-D STUMP_BASED=0";
1344
1345         openCLExecuteKernel(gsum.clCxt, &haarobjectdetect, "gpuRunHaarClassifierCascade", globalThreads, localThreads, args, -1, -1, build_options);
1346
1347         candidate = (int *)malloc(4 * sizeof(int) * outputsz);
1348         memset(candidate, 0, 4 * sizeof(int) * outputsz);
1349
1350         openCLReadBuffer( gsum.clCxt, ((OclBuffers *)buffers)->candidatebuffer, candidate, 4 * sizeof(int)*outputsz );
1351
1352         for(int i = 0; i < outputsz; i++)
1353         {
1354             if(candidate[4 * i + 2] != 0)
1355             {
1356                 allCandidates.push_back(Rect(candidate[4 * i], candidate[4 * i + 1],
1357                 candidate[4 * i + 2], candidate[4 * i + 3]));
1358             }
1359         }
1360         free((void *)candidate);
1361         candidate = NULL;
1362     }
1363     else
1364     {
1365         cv::ocl::integral(gimg, gsum, gsqsum_t);
1366         if(gsqsum_t.depth() == CV_64F)
1367             gsqsum_t.convertTo(gsqsum, CV_32FC1);
1368         else
1369             gsqsum = gsqsum_t;
1370
1371         gcascade   = (GpuHidHaarClassifierCascade *)cascade->hid_cascade;
1372
1373         int step = gsum.step / 4;
1374         int startnode = 0;
1375         int splitstage = 3;
1376
1377         int startstage = 0;
1378         int endstage = gcascade->count;
1379
1380         vector<pair<size_t, const void *> > args;
1381         args.push_back ( make_pair(sizeof(cl_mem) , (void *)&((OclBuffers *)buffers)->stagebuffer ));
1382         args.push_back ( make_pair(sizeof(cl_mem) , (void *)&((OclBuffers *)buffers)->scaleinfobuffer ));
1383         args.push_back ( make_pair(sizeof(cl_mem) , (void *)&((OclBuffers *)buffers)->newnodebuffer ));
1384         args.push_back ( make_pair(sizeof(cl_mem) , (void *)&gsum.data ));
1385         args.push_back ( make_pair(sizeof(cl_mem) , (void *)&gsqsum.data ));
1386         args.push_back ( make_pair(sizeof(cl_mem) , (void *)&((OclBuffers *)buffers)->candidatebuffer ));
1387         args.push_back ( make_pair(sizeof(cl_int) , (void *)&gsum.rows ));
1388         args.push_back ( make_pair(sizeof(cl_int) , (void *)&gsum.cols ));
1389         args.push_back ( make_pair(sizeof(cl_int) , (void *)&step ));
1390         args.push_back ( make_pair(sizeof(cl_int) , (void *)&m_loopcount ));
1391         args.push_back ( make_pair(sizeof(cl_int) , (void *)&startstage ));
1392         args.push_back ( make_pair(sizeof(cl_int) , (void *)&splitstage ));
1393         args.push_back ( make_pair(sizeof(cl_int) , (void *)&endstage ));
1394         args.push_back ( make_pair(sizeof(cl_int) , (void *)&startnode ));
1395         args.push_back ( make_pair(sizeof(cl_mem) , (void *)&((OclBuffers *)buffers)->pbuffer ));
1396         args.push_back ( make_pair(sizeof(cl_mem) , (void *)&((OclBuffers *)buffers)->correctionbuffer ));
1397         args.push_back ( make_pair(sizeof(cl_int) , (void *)&m_nodenum ));
1398
1399         const char * build_options = gcascade->is_stump_based ? "-D STUMP_BASED=1" : "-D STUMP_BASED=0";
1400         openCLExecuteKernel(gsum.clCxt, &haarobjectdetect_scaled2, "gpuRunHaarClassifierCascade_scaled2", globalThreads, localThreads, args, -1, -1, build_options);
1401
1402         candidate = (int *)clEnqueueMapBuffer(qu, ((OclBuffers *)buffers)->candidatebuffer, 1, CL_MAP_READ, 0, 4 * sizeof(int) * outputsz, 0, 0, 0, NULL);
1403
1404         for(int i = 0; i < outputsz; i++)
1405         {
1406             if(candidate[4 * i + 2] != 0)
1407                 allCandidates.push_back(Rect(candidate[4 * i], candidate[4 * i + 1],
1408                 candidate[4 * i + 2], candidate[4 * i + 3]));
1409         }
1410         clEnqueueUnmapMemObject(qu, ((OclBuffers *)buffers)->candidatebuffer, candidate, 0, 0, 0);
1411     }
1412     rectList.resize(allCandidates.size());
1413     if(!allCandidates.empty())
1414         std::copy(allCandidates.begin(), allCandidates.end(), rectList.begin());
1415
1416     if( minNeighbors != 0 || findBiggestObject )
1417         groupRectangles(rectList, rweights, std::max(minNeighbors, 1), GROUP_EPS);
1418     else
1419         rweights.resize(rectList.size(), 0);
1420
1421     GenResult(faces, rectList, rweights);
1422 }
1423
1424 void cv::ocl::OclCascadeClassifierBuf::Init(const int rows, const int cols,
1425     double scaleFactor, int flags,
1426     const int outputsz, const size_t localThreads[],
1427     CvSize minSize, CvSize maxSize)
1428 {
1429     if(initialized)
1430     {
1431         return; // we only allow one time initialization
1432     }
1433     CvHaarClassifierCascade      *cascade = oldCascade;
1434
1435     if( !CV_IS_HAAR_CLASSIFIER(cascade) )
1436         CV_Error( !cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier cascade" );
1437
1438     if( scaleFactor <= 1 )
1439         CV_Error( CV_StsOutOfRange, "scale factor must be > 1" );
1440
1441     if( cols < minSize.width || rows < minSize.height )
1442         CV_Error(CV_StsError, "Image too small");
1443
1444     int datasize=0;
1445     int totalclassifier=0;
1446
1447     if( !cascade->hid_cascade )
1448     {
1449         gpuCreateHidHaarClassifierCascade(cascade, &datasize, &totalclassifier);
1450     }
1451
1452     if( maxSize.height == 0 || maxSize.width == 0 )
1453     {
1454         maxSize.height = rows;
1455         maxSize.width = cols;
1456     }
1457
1458     findBiggestObject = (flags & CV_HAAR_FIND_BIGGEST_OBJECT) != 0;
1459     if( findBiggestObject )
1460         flags &= ~(CV_HAAR_SCALE_IMAGE | CV_HAAR_DO_CANNY_PRUNING);
1461
1462     CreateBaseBufs(datasize, totalclassifier, flags, outputsz);
1463     CreateFactorRelatedBufs(rows, cols, flags, scaleFactor, localThreads, minSize, maxSize);
1464
1465     m_scaleFactor = scaleFactor;
1466     m_rows = rows;
1467     m_cols = cols;
1468     m_flags = flags;
1469     m_minSize = minSize;
1470     m_maxSize = maxSize;
1471
1472     // initialize nodes
1473     GpuHidHaarClassifierCascade  *gcascade;
1474     GpuHidHaarStageClassifier    *stage;
1475     GpuHidHaarClassifier         *classifier;
1476     GpuHidHaarTreeNode           *node;
1477     cl_command_queue qu = getClCommandQueue(Context::getContext());
1478     if( (flags & CV_HAAR_SCALE_IMAGE) )
1479     {
1480         gcascade   = (GpuHidHaarClassifierCascade *)(cascade->hid_cascade);
1481         stage      = (GpuHidHaarStageClassifier *)(gcascade + 1);
1482         classifier = (GpuHidHaarClassifier *)(stage + gcascade->count);
1483         node       = (GpuHidHaarTreeNode *)(classifier->node);
1484
1485         gpuSetImagesForHaarClassifierCascade( cascade, 1., gsum.step / 4 );
1486
1487         openCLSafeCall(clEnqueueWriteBuffer(qu, ((OclBuffers *)buffers)->stagebuffer, 1, 0,
1488             sizeof(GpuHidHaarStageClassifier) * gcascade->count,
1489             stage, 0, NULL, NULL));
1490
1491         openCLSafeCall(clEnqueueWriteBuffer(qu, ((OclBuffers *)buffers)->nodebuffer, 1, 0,
1492                                             m_nodenum * sizeof(GpuHidHaarTreeNode),
1493                                             node, 0, NULL, NULL));
1494     }
1495     else
1496     {
1497         gpuSetHaarClassifierCascade(cascade);
1498
1499         gcascade   = (GpuHidHaarClassifierCascade *)cascade->hid_cascade;
1500         stage      = (GpuHidHaarStageClassifier *)(gcascade + 1);
1501         classifier = (GpuHidHaarClassifier *)(stage + gcascade->count);
1502         node       = (GpuHidHaarTreeNode *)(classifier->node);
1503
1504         openCLSafeCall(clEnqueueWriteBuffer(qu, ((OclBuffers *)buffers)->nodebuffer, 1, 0,
1505             m_nodenum * sizeof(GpuHidHaarTreeNode),
1506             node, 0, NULL, NULL));
1507
1508         cl_int4 *p = (cl_int4 *)malloc(sizeof(cl_int4) * m_loopcount);
1509         float *correction = (float *)malloc(sizeof(float) * m_loopcount);
1510         double factor;
1511         for(int i = 0; i < m_loopcount; i++)
1512         {
1513             factor = scalev[i];
1514             int equRect_x = (int)(factor * gcascade->p0 + 0.5);
1515             int equRect_y = (int)(factor * gcascade->p1 + 0.5);
1516             int equRect_w = (int)(factor * gcascade->p3 + 0.5);
1517             int equRect_h = (int)(factor * gcascade->p2 + 0.5);
1518             p[i].s[0] = equRect_x;
1519             p[i].s[1] = equRect_y;
1520             p[i].s[2] = equRect_x + equRect_w;
1521             p[i].s[3] = equRect_y + equRect_h;
1522             correction[i] = 1. / (equRect_w * equRect_h);
1523             int startnodenum = m_nodenum * i;
1524             float factor2 = (float)factor;
1525
1526             vector<pair<size_t, const void *> > args1;
1527             args1.push_back ( make_pair(sizeof(cl_mem) , (void *)&((OclBuffers *)buffers)->nodebuffer ));
1528             args1.push_back ( make_pair(sizeof(cl_mem) , (void *)&((OclBuffers *)buffers)->newnodebuffer ));
1529             args1.push_back ( make_pair(sizeof(cl_float) , (void *)&factor2 ));
1530             args1.push_back ( make_pair(sizeof(cl_float) , (void *)&correction[i] ));
1531             args1.push_back ( make_pair(sizeof(cl_int) , (void *)&startnodenum ));
1532
1533             size_t globalThreads2[3] = {m_nodenum, 1, 1};
1534
1535             openCLExecuteKernel(Context::getContext(), &haarobjectdetect_scaled2, "gpuscaleclassifier", globalThreads2, NULL/*localThreads2*/, args1, -1, -1);
1536         }
1537         openCLSafeCall(clEnqueueWriteBuffer(qu, ((OclBuffers *)buffers)->stagebuffer, 1, 0, sizeof(GpuHidHaarStageClassifier)*gcascade->count, stage, 0, NULL, NULL));
1538         openCLSafeCall(clEnqueueWriteBuffer(qu, ((OclBuffers *)buffers)->pbuffer, 1, 0, sizeof(cl_int4)*m_loopcount, p, 0, NULL, NULL));
1539         openCLSafeCall(clEnqueueWriteBuffer(qu, ((OclBuffers *)buffers)->correctionbuffer, 1, 0, sizeof(cl_float)*m_loopcount, correction, 0, NULL, NULL));
1540
1541         free(p);
1542         free(correction);
1543     }
1544     initialized = true;
1545 }
1546
1547 void cv::ocl::OclCascadeClassifierBuf::CreateBaseBufs(const int datasize, const int totalclassifier,
1548                                                       const int flags, const int outputsz)
1549 {
1550     if (!initialized)
1551     {
1552         buffers = malloc(sizeof(OclBuffers));
1553
1554         size_t tempSize =
1555             sizeof(GpuHidHaarStageClassifier) * ((GpuHidHaarClassifierCascade *)oldCascade->hid_cascade)->count;
1556         m_nodenum = (datasize - sizeof(GpuHidHaarClassifierCascade) - tempSize - sizeof(GpuHidHaarClassifier) * totalclassifier)
1557             / sizeof(GpuHidHaarTreeNode);
1558
1559         ((OclBuffers *)buffers)->stagebuffer     = openCLCreateBuffer(cv::ocl::Context::getContext(), CL_MEM_READ_ONLY,  tempSize);
1560         ((OclBuffers *)buffers)->nodebuffer      = openCLCreateBuffer(cv::ocl::Context::getContext(), CL_MEM_READ_ONLY,  m_nodenum * sizeof(GpuHidHaarTreeNode));
1561     }
1562
1563     if (initialized
1564         && ((m_flags & CV_HAAR_SCALE_IMAGE) ^ (flags & CV_HAAR_SCALE_IMAGE)))
1565     {
1566         openCLSafeCall(clReleaseMemObject(((OclBuffers *)buffers)->candidatebuffer));
1567     }
1568
1569     if (flags & CV_HAAR_SCALE_IMAGE)
1570     {
1571         ((OclBuffers *)buffers)->candidatebuffer = openCLCreateBuffer(cv::ocl::Context::getContext(),
1572                                                         CL_MEM_WRITE_ONLY,
1573                                                         4 * sizeof(int) * outputsz);
1574     }
1575     else
1576     {
1577         ((OclBuffers *)buffers)->candidatebuffer = openCLCreateBuffer(cv::ocl::Context::getContext(),
1578                                                         CL_MEM_WRITE_ONLY | CL_MEM_ALLOC_HOST_PTR,
1579                                                         4 * sizeof(int) * outputsz);
1580     }
1581 }
1582
1583 void cv::ocl::OclCascadeClassifierBuf::CreateFactorRelatedBufs(
1584     const int rows, const int cols, const int flags,
1585     const double scaleFactor, const size_t localThreads[],
1586     CvSize minSize, CvSize maxSize)
1587 {
1588     if (initialized)
1589     {
1590         if ((m_flags & CV_HAAR_SCALE_IMAGE) && !(flags & CV_HAAR_SCALE_IMAGE))
1591         {
1592             gimg1.release();
1593             gsum.release();
1594             gsqsum.release();
1595             gsqsum_t.release();
1596         }
1597         else if (!(m_flags & CV_HAAR_SCALE_IMAGE) && (flags & CV_HAAR_SCALE_IMAGE))
1598         {
1599             openCLSafeCall(clReleaseMemObject(((OclBuffers *)buffers)->newnodebuffer));
1600             openCLSafeCall(clReleaseMemObject(((OclBuffers *)buffers)->correctionbuffer));
1601             openCLSafeCall(clReleaseMemObject(((OclBuffers *)buffers)->pbuffer));
1602         }
1603         else if ((m_flags & CV_HAAR_SCALE_IMAGE) && (flags & CV_HAAR_SCALE_IMAGE))
1604         {
1605             if (fabs(m_scaleFactor - scaleFactor) < 1e-6
1606                 && (rows == m_rows && cols == m_cols)
1607                 && (minSize.width == m_minSize.width)
1608                 && (minSize.height == m_minSize.height)
1609                 && (maxSize.width == m_maxSize.width)
1610                 && (maxSize.height == m_maxSize.height))
1611             {
1612                 return;
1613             }
1614         }
1615         else
1616         {
1617             if (fabs(m_scaleFactor - scaleFactor) < 1e-6
1618                 && (rows == m_rows && cols == m_cols)
1619                 && (minSize.width == m_minSize.width)
1620                 && (minSize.height == m_minSize.height)
1621                 && (maxSize.width == m_maxSize.width)
1622                 && (maxSize.height == m_maxSize.height))
1623             {
1624                 return;
1625             }
1626             else
1627             {
1628                 openCLSafeCall(clReleaseMemObject(((OclBuffers *)buffers)->newnodebuffer));
1629                 openCLSafeCall(clReleaseMemObject(((OclBuffers *)buffers)->correctionbuffer));
1630                 openCLSafeCall(clReleaseMemObject(((OclBuffers *)buffers)->pbuffer));
1631             }
1632         }
1633     }
1634
1635     int loopcount;
1636     int indexy = 0;
1637     int totalheight = 0;
1638     double factor;
1639     Rect roi;
1640     CvSize sz;
1641     CvSize winSize0 = oldCascade->orig_window_size;
1642     detect_piramid_info *scaleinfo;
1643     cl_command_queue qu = getClCommandQueue(Context::getContext());
1644     if (flags & CV_HAAR_SCALE_IMAGE)
1645     {
1646         for(factor = 1.f;; factor *= scaleFactor)
1647         {
1648             CvSize winSize = { cvRound(winSize0.width * factor), cvRound(winSize0.height * factor) };
1649             sz.width     = cvRound( cols / factor ) + 1;
1650             sz.height    = cvRound( rows / factor ) + 1;
1651             CvSize sz1     = { sz.width - winSize0.width - 1,      sz.height - winSize0.height - 1 };
1652
1653             if( sz1.width <= 0 || sz1.height <= 0 )
1654                 break;
1655             if( winSize.width > maxSize.width || winSize.height > maxSize.height )
1656                 break;
1657             if( winSize.width < minSize.width || winSize.height < minSize.height )
1658                 continue;
1659
1660             totalheight += sz.height;
1661             sizev.push_back(sz);
1662             scalev.push_back(static_cast<float>(factor));
1663         }
1664
1665         loopcount = sizev.size();
1666         gimg1.create(rows, cols, CV_8UC1);
1667         gsum.create(totalheight + 4, cols + 1, CV_32SC1);
1668         gsqsum.create(totalheight + 4, cols + 1, CV_32FC1);
1669
1670         int sdepth = 0;
1671         if(Context::getContext()->supportsFeature(FEATURE_CL_DOUBLE))
1672             sdepth = CV_64FC1;
1673         else
1674             sdepth = CV_32FC1;
1675         sdepth = CV_MAT_DEPTH(sdepth);
1676         int type = CV_MAKE_TYPE(sdepth, 1);
1677
1678         gsqsum_t.create(totalheight + 4, cols + 1, type);
1679
1680         scaleinfo = (detect_piramid_info *)malloc(sizeof(detect_piramid_info) * loopcount);
1681         for( int i = 0; i < loopcount; i++ )
1682         {
1683             sz = sizev[i];
1684             roi = Rect(0, indexy, sz.width, sz.height);
1685             int width = sz.width - 1 - oldCascade->orig_window_size.width;
1686             int height = sz.height - 1 - oldCascade->orig_window_size.height;
1687             int grpnumperline = (width + localThreads[0] - 1) / localThreads[0];
1688             int totalgrp = ((height + localThreads[1] - 1) / localThreads[1]) * grpnumperline;
1689
1690             ((detect_piramid_info *)scaleinfo)[i].width_height = (width << 16) | height;
1691             ((detect_piramid_info *)scaleinfo)[i].grpnumperline_totalgrp = (grpnumperline << 16) | totalgrp;
1692             ((detect_piramid_info *)scaleinfo)[i].imgoff = gsum(roi).offset >> 2;
1693             ((detect_piramid_info *)scaleinfo)[i].factor = scalev[i];
1694
1695             indexy += sz.height;
1696         }
1697     }
1698     else
1699     {
1700         for(factor = 1;
1701             cvRound(factor * winSize0.width) < cols - 10 && cvRound(factor * winSize0.height) < rows - 10;
1702             factor *= scaleFactor)
1703         {
1704             CvSize winSize = { cvRound( winSize0.width * factor ), cvRound( winSize0.height * factor ) };
1705             if( winSize.width < minSize.width || winSize.height < minSize.height )
1706             {
1707                 continue;
1708             }
1709             sizev.push_back(winSize);
1710             scalev.push_back(factor);
1711         }
1712
1713         loopcount = scalev.size();
1714         if(loopcount == 0)
1715         {
1716             loopcount = 1;
1717             sizev.push_back(minSize);
1718             scalev.push_back( std::min(cvRound(minSize.width / winSize0.width), cvRound(minSize.height / winSize0.height)) );
1719         }
1720
1721         ((OclBuffers *)buffers)->pbuffer = openCLCreateBuffer(cv::ocl::Context::getContext(), CL_MEM_READ_ONLY,
1722             sizeof(cl_int4) * loopcount);
1723         ((OclBuffers *)buffers)->correctionbuffer = openCLCreateBuffer(cv::ocl::Context::getContext(), CL_MEM_READ_ONLY,
1724             sizeof(cl_float) * loopcount);
1725         ((OclBuffers *)buffers)->newnodebuffer = openCLCreateBuffer(cv::ocl::Context::getContext(), CL_MEM_READ_WRITE,
1726             loopcount * m_nodenum * sizeof(GpuHidHaarTreeNode));
1727
1728         scaleinfo = (detect_piramid_info *)malloc(sizeof(detect_piramid_info) * loopcount);
1729         for( int i = 0; i < loopcount; i++ )
1730         {
1731             sz = sizev[i];
1732             factor = scalev[i];
1733             double ystep = cv::max(2.,factor);
1734             int width = cvRound((cols - 1 - sz.width  + ystep - 1) / ystep);
1735             int height = cvRound((rows - 1 - sz.height + ystep - 1) / ystep);
1736             int grpnumperline = (width + localThreads[0] - 1) / localThreads[0];
1737             int totalgrp = ((height + localThreads[1] - 1) / localThreads[1]) * grpnumperline;
1738
1739             ((detect_piramid_info *)scaleinfo)[i].width_height = (width << 16) | height;
1740             ((detect_piramid_info *)scaleinfo)[i].grpnumperline_totalgrp = (grpnumperline << 16) | totalgrp;
1741             ((detect_piramid_info *)scaleinfo)[i].imgoff = 0;
1742             ((detect_piramid_info *)scaleinfo)[i].factor = factor;
1743         }
1744     }
1745
1746     if (loopcount != m_loopcount)
1747     {
1748         if (initialized)
1749         {
1750             openCLSafeCall(clReleaseMemObject(((OclBuffers *)buffers)->scaleinfobuffer));
1751         }
1752         ((OclBuffers *)buffers)->scaleinfobuffer = openCLCreateBuffer(cv::ocl::Context::getContext(), CL_MEM_READ_ONLY, sizeof(detect_piramid_info) * loopcount);
1753     }
1754
1755     openCLSafeCall(clEnqueueWriteBuffer(qu, ((OclBuffers *)buffers)->scaleinfobuffer, 1, 0,
1756         sizeof(detect_piramid_info)*loopcount,
1757         scaleinfo, 0, NULL, NULL));
1758     free(scaleinfo);
1759
1760     m_loopcount = loopcount;
1761 }
1762
1763 void cv::ocl::OclCascadeClassifierBuf::GenResult(CV_OUT std::vector<cv::Rect>& faces,
1764                                                  const std::vector<cv::Rect> &rectList,
1765                                                  const std::vector<int> &rweights)
1766 {
1767     MemStorage tempStorage(cvCreateMemStorage(0));
1768     CvSeq *result_seq = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvAvgComp), tempStorage );
1769
1770     if( findBiggestObject && rectList.size() )
1771     {
1772         CvAvgComp result_comp = {{0, 0, 0, 0}, 0};
1773
1774         for( size_t i = 0; i < rectList.size(); i++ )
1775         {
1776             cv::Rect r = rectList[i];
1777             if( r.area() > cv::Rect(result_comp.rect).area() )
1778             {
1779                 result_comp.rect = r;
1780                 result_comp.neighbors = rweights[i];
1781             }
1782         }
1783         cvSeqPush( result_seq, &result_comp );
1784     }
1785     else
1786     {
1787         for( size_t i = 0; i < rectList.size(); i++ )
1788         {
1789             CvAvgComp c;
1790             c.rect = rectList[i];
1791             c.neighbors = rweights[i];
1792             cvSeqPush( result_seq, &c );
1793         }
1794     }
1795
1796     vector<CvAvgComp> vecAvgComp;
1797     Seq<CvAvgComp>(result_seq).copyTo(vecAvgComp);
1798     faces.resize(vecAvgComp.size());
1799     std::transform(vecAvgComp.begin(), vecAvgComp.end(), faces.begin(), getRect());
1800 }
1801
1802 void cv::ocl::OclCascadeClassifierBuf::release()
1803 {
1804     if(initialized)
1805     {
1806         openCLSafeCall(clReleaseMemObject(((OclBuffers *)buffers)->stagebuffer));
1807         openCLSafeCall(clReleaseMemObject(((OclBuffers *)buffers)->scaleinfobuffer));
1808         openCLSafeCall(clReleaseMemObject(((OclBuffers *)buffers)->nodebuffer));
1809         openCLSafeCall(clReleaseMemObject(((OclBuffers *)buffers)->candidatebuffer));
1810
1811         if( (m_flags & CV_HAAR_SCALE_IMAGE) )
1812         {
1813             cvFree(&oldCascade->hid_cascade);
1814         }
1815         else
1816         {
1817             openCLSafeCall(clReleaseMemObject(((OclBuffers *)buffers)->newnodebuffer));
1818             openCLSafeCall(clReleaseMemObject(((OclBuffers *)buffers)->correctionbuffer));
1819             openCLSafeCall(clReleaseMemObject(((OclBuffers *)buffers)->pbuffer));
1820         }
1821
1822         free(buffers);
1823         buffers = NULL;
1824         initialized = false;
1825     }
1826 }
1827
1828 #ifndef _MAX_PATH
1829 #define _MAX_PATH 1024
1830 #endif