haar.cpp 50.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// @Authors
//    Niko Li, newlife20080214@gmail.com
//    Wang Weiyan, wangweiyanster@gmail.com
//    Jia Haipeng, jiahaipeng95@gmail.com
//    Wu Xinglong, wxl370@126.com
//    Wang Yao, bitwangyaoyao@gmail.com
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
Andrey Pavlenko's avatar
Andrey Pavlenko committed
32
//     and/or other materials provided with the distribution.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#include "precomp.hpp"
51
#include "opencl_kernels.hpp"
yao's avatar
yao committed
52

53 54 55 56
using namespace cv;
using namespace cv::ocl;

/* these settings affect the quality of detection: change with care */
57 58 59
#define CV_ADJUST_FEATURES  1
#define CV_ADJUST_WEIGHTS   0
#define CV_HAAR_FEATURE_MAX 3
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
typedef int sumtype;
typedef double sqsumtype;

typedef struct CvHidHaarFeature
{
    struct
    {
        sumtype *p0, *p1, *p2, *p3;
        float weight;
    }
    rect[CV_HAAR_FEATURE_MAX];
}
CvHidHaarFeature;


typedef struct CvHidHaarTreeNode
{
    CvHidHaarFeature feature;
    float threshold;
    int left;
    int right;
}
CvHidHaarTreeNode;


typedef struct CvHidHaarClassifier
{
    int count;
    //CvHaarFeature* orig_feature;
    CvHidHaarTreeNode *node;
    float *alpha;
}
CvHidHaarClassifier;


typedef struct CvHidHaarStageClassifier
{
    int  count;
    float threshold;
    CvHidHaarClassifier *classifier;
    int two_rects;

    struct CvHidHaarStageClassifier *next;
    struct CvHidHaarStageClassifier *child;
    struct CvHidHaarStageClassifier *parent;
}
CvHidHaarStageClassifier;


struct CvHidHaarClassifierCascade
{
    int  count;
    int  is_stump_based;
    int  has_tilted_features;
    int  is_tree;
    double inv_window_area;
    CvMat sum, sqsum, tilted;
    CvHidHaarStageClassifier *stage_classifier;
    sqsumtype *pq0, *pq1, *pq2, *pq3;
    sumtype *p0, *p1, *p2, *p3;

    void **ipp_stages;
};
typedef struct
{
    int width_height;
    int grpnumperline_totalgrp;
    int imgoff;
    float factor;
} detect_piramid_info;
130
#ifdef _MSC_VER
131 132 133 134 135 136 137
#define _ALIGNED_ON(_ALIGNMENT) __declspec(align(_ALIGNMENT))

typedef _ALIGNED_ON(128) struct  GpuHidHaarTreeNode
{
    _ALIGNED_ON(64) int p[CV_HAAR_FEATURE_MAX][4];
    float weight[CV_HAAR_FEATURE_MAX] ;
    float threshold ;
138
    _ALIGNED_ON(16) float alpha[3] ;
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    _ALIGNED_ON(4) int left ;
    _ALIGNED_ON(4) int right ;
}
GpuHidHaarTreeNode;


typedef  _ALIGNED_ON(32) struct  GpuHidHaarClassifier
{
    _ALIGNED_ON(4) int count;
    _ALIGNED_ON(8) GpuHidHaarTreeNode *node ;
    _ALIGNED_ON(8) float *alpha ;
}
GpuHidHaarClassifier;


typedef _ALIGNED_ON(64) struct   GpuHidHaarStageClassifier
{
    _ALIGNED_ON(4) int  count ;
    _ALIGNED_ON(4) float threshold ;
    _ALIGNED_ON(4) int two_rects ;
    _ALIGNED_ON(8) GpuHidHaarClassifier *classifier ;
    _ALIGNED_ON(8) struct GpuHidHaarStageClassifier *next;
    _ALIGNED_ON(8) struct GpuHidHaarStageClassifier *child ;
    _ALIGNED_ON(8) struct GpuHidHaarStageClassifier *parent ;
}
GpuHidHaarStageClassifier;


typedef _ALIGNED_ON(64) struct  GpuHidHaarClassifierCascade
{
    _ALIGNED_ON(4) int  count ;
    _ALIGNED_ON(4) int  is_stump_based ;
    _ALIGNED_ON(4) int  has_tilted_features ;
    _ALIGNED_ON(4) int  is_tree ;
    _ALIGNED_ON(4) int pq0 ;
    _ALIGNED_ON(4) int pq1 ;
    _ALIGNED_ON(4) int pq2 ;
    _ALIGNED_ON(4) int pq3 ;
    _ALIGNED_ON(4) int p0 ;
    _ALIGNED_ON(4) int p1 ;
    _ALIGNED_ON(4) int p2 ;
    _ALIGNED_ON(4) int p3 ;
    _ALIGNED_ON(4) float inv_window_area ;
} GpuHidHaarClassifierCascade;
#else
#define _ALIGNED_ON(_ALIGNMENT) __attribute__((aligned(_ALIGNMENT) ))

typedef struct _ALIGNED_ON(128) GpuHidHaarTreeNode
{
    int p[CV_HAAR_FEATURE_MAX][4] _ALIGNED_ON(64);
    float weight[CV_HAAR_FEATURE_MAX];// _ALIGNED_ON(16);
    float threshold;// _ALIGNED_ON(4);
191
    float alpha[3] _ALIGNED_ON(16);
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
    int left _ALIGNED_ON(4);
    int right _ALIGNED_ON(4);
}
GpuHidHaarTreeNode;

typedef struct _ALIGNED_ON(32) GpuHidHaarClassifier
{
    int count _ALIGNED_ON(4);
    GpuHidHaarTreeNode *node _ALIGNED_ON(8);
    float *alpha _ALIGNED_ON(8);
}
GpuHidHaarClassifier;


typedef struct _ALIGNED_ON(64) GpuHidHaarStageClassifier
{
    int  count _ALIGNED_ON(4);
    float threshold _ALIGNED_ON(4);
    int two_rects _ALIGNED_ON(4);
    GpuHidHaarClassifier *classifier _ALIGNED_ON(8);
    struct GpuHidHaarStageClassifier *next _ALIGNED_ON(8);
    struct GpuHidHaarStageClassifier *child _ALIGNED_ON(8);
    struct GpuHidHaarStageClassifier *parent _ALIGNED_ON(8);
}
GpuHidHaarStageClassifier;


typedef struct _ALIGNED_ON(64) GpuHidHaarClassifierCascade
{
    int  count _ALIGNED_ON(4);
    int  is_stump_based _ALIGNED_ON(4);
    int  has_tilted_features _ALIGNED_ON(4);
    int  is_tree _ALIGNED_ON(4);
    int pq0 _ALIGNED_ON(4);
    int pq1 _ALIGNED_ON(4);
    int pq2 _ALIGNED_ON(4);
    int pq3 _ALIGNED_ON(4);
    int p0 _ALIGNED_ON(4);
    int p1 _ALIGNED_ON(4);
    int p2 _ALIGNED_ON(4);
    int p3 _ALIGNED_ON(4);
    float inv_window_area _ALIGNED_ON(4);
} GpuHidHaarClassifierCascade;
#endif

const int icv_object_win_border = 1;
const float icv_stage_threshold_bias = 0.0001f;
double globaltime = 0;

/* create more efficient internal representation of haar classifier cascade */
242
static GpuHidHaarClassifierCascade * gpuCreateHidHaarClassifierCascade( CvHaarClassifierCascade *cascade, int *size, int *totalclassifier)
243 244 245 246 247 248 249
{
    GpuHidHaarClassifierCascade *out = 0;

    int i, j, k, l;
    int datasize;
    int total_classifiers = 0;
    int total_nodes = 0;
250
    char errorstr[256];
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390

    GpuHidHaarStageClassifier *stage_classifier_ptr;
    GpuHidHaarClassifier *haar_classifier_ptr;
    GpuHidHaarTreeNode *haar_node_ptr;

    CvSize orig_window_size;
    int has_tilted_features = 0;

    if( !CV_IS_HAAR_CLASSIFIER(cascade) )
        CV_Error( !cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier pointer" );

    if( cascade->hid_cascade )
        CV_Error( CV_StsError, "hid_cascade has been already created" );

    if( !cascade->stage_classifier )
        CV_Error( CV_StsNullPtr, "" );

    if( cascade->count <= 0 )
        CV_Error( CV_StsOutOfRange, "Negative number of cascade stages" );

    orig_window_size = cascade->orig_window_size;

    /* check input structure correctness and calculate total memory size needed for
    internal representation of the classifier cascade */
    for( i = 0; i < cascade->count; i++ )
    {
        CvHaarStageClassifier *stage_classifier = cascade->stage_classifier + i;

        if( !stage_classifier->classifier ||
                stage_classifier->count <= 0 )
        {
            sprintf( errorstr, "header of the stage classifier #%d is invalid "
                     "(has null pointers or non-positive classfier count)", i );
            CV_Error( CV_StsError, errorstr );
        }

        total_classifiers += stage_classifier->count;

        for( j = 0; j < stage_classifier->count; j++ )
        {
            CvHaarClassifier *classifier = stage_classifier->classifier + j;

            total_nodes += classifier->count;
            for( l = 0; l < classifier->count; l++ )
            {
                for( k = 0; k < CV_HAAR_FEATURE_MAX; k++ )
                {
                    if( classifier->haar_feature[l].rect[k].r.width )
                    {
                        CvRect r = classifier->haar_feature[l].rect[k].r;
                        int tilted = classifier->haar_feature[l].tilted;
                        has_tilted_features |= tilted != 0;
                        if( r.width < 0 || r.height < 0 || r.y < 0 ||
                                r.x + r.width > orig_window_size.width
                                ||
                                (!tilted &&
                                 (r.x < 0 || r.y + r.height > orig_window_size.height))
                                ||
                                (tilted && (r.x - r.height < 0 ||
                                            r.y + r.width + r.height > orig_window_size.height)))
                        {
                            sprintf( errorstr, "rectangle #%d of the classifier #%d of "
                                     "the stage classifier #%d is not inside "
                                     "the reference (original) cascade window", k, j, i );
                            CV_Error( CV_StsNullPtr, errorstr );
                        }
                    }
                }
            }
        }
    }

    // this is an upper boundary for the whole hidden cascade size
    datasize = sizeof(GpuHidHaarClassifierCascade)                   +
               sizeof(GpuHidHaarStageClassifier) * cascade->count    +
               sizeof(GpuHidHaarClassifier)      * total_classifiers +
               sizeof(GpuHidHaarTreeNode)        * total_nodes;

    *totalclassifier = total_classifiers;
    *size = datasize;
    out = (GpuHidHaarClassifierCascade *)cvAlloc( datasize );
    memset( out, 0, sizeof(*out) );

    /* init header */
    out->count = cascade->count;
    stage_classifier_ptr = (GpuHidHaarStageClassifier *)(out + 1);
    haar_classifier_ptr = (GpuHidHaarClassifier *)(stage_classifier_ptr + cascade->count);
    haar_node_ptr = (GpuHidHaarTreeNode *)(haar_classifier_ptr + total_classifiers);

    out->is_stump_based = 1;
    out->has_tilted_features = has_tilted_features;
    out->is_tree = 0;

    /* initialize internal representation */
    for( i = 0; i < cascade->count; i++ )
    {
        CvHaarStageClassifier *stage_classifier = cascade->stage_classifier + i;
        GpuHidHaarStageClassifier *hid_stage_classifier = stage_classifier_ptr + i;

        hid_stage_classifier->count = stage_classifier->count;
        hid_stage_classifier->threshold = stage_classifier->threshold - icv_stage_threshold_bias;
        hid_stage_classifier->classifier = haar_classifier_ptr;
        hid_stage_classifier->two_rects = 1;
        haar_classifier_ptr += stage_classifier->count;

        for( j = 0; j < stage_classifier->count; j++ )
        {
            CvHaarClassifier *classifier         = stage_classifier->classifier + j;
            GpuHidHaarClassifier *hid_classifier = hid_stage_classifier->classifier + j;
            int node_count = classifier->count;

            float *alpha_ptr = &haar_node_ptr->alpha[0];

            hid_classifier->count = node_count;
            hid_classifier->node = haar_node_ptr;
            hid_classifier->alpha = alpha_ptr;

            for( l = 0; l < node_count; l++ )
            {
                GpuHidHaarTreeNode *node     = hid_classifier->node + l;
                CvHaarFeature      *feature = classifier->haar_feature + l;

                memset( node, -1, sizeof(*node) );
                node->threshold = classifier->threshold[l];
                node->left      = classifier->left[l];
                node->right     = classifier->right[l];

                if( fabs(feature->rect[2].weight) < DBL_EPSILON ||
                        feature->rect[2].r.width == 0 ||
                        feature->rect[2].r.height == 0 )
                {
                    node->p[2][0] = 0;
                    node->p[2][1] = 0;
                    node->p[2][2] = 0;
                    node->p[2][3] = 0;
                    node->weight[2] = 0;
                }
                else
                    hid_stage_classifier->two_rects = 0;

391 392 393
                memcpy( node->alpha, classifier->alpha, (node_count + 1)*sizeof(alpha_ptr[0]));
                haar_node_ptr = haar_node_ptr + 1;
            }
394 395 396 397 398 399 400 401 402 403 404 405
            out->is_stump_based &= node_count == 1;
        }
    }

    cascade->hid_cascade = (CvHidHaarClassifierCascade *)out;
    assert( (char *)haar_node_ptr - (char *)out <= datasize );

    return out;
}


#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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
                                      double scale,
                                      int step)
{
    GpuHidHaarClassifierCascade *cascade;
    int coi0 = 0, coi1 = 0;
    int i;
    int datasize;
    int total;
    CvRect equRect;
    double weight_scale;
    GpuHidHaarStageClassifier *stage_classifier;

    if( !CV_IS_HAAR_CLASSIFIER(_cascade) )
        CV_Error( !_cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier pointer" );

    if( scale <= 0 )
        CV_Error( CV_StsOutOfRange, "Scale must be positive" );

    if( coi0 || coi1 )
        CV_Error( CV_BadCOI, "COI is not supported" );

    if( !_cascade->hid_cascade )
        gpuCreateHidHaarClassifierCascade(_cascade, &datasize, &total);

    cascade = (GpuHidHaarClassifierCascade *) _cascade->hid_cascade;
    stage_classifier = (GpuHidHaarStageClassifier *) (cascade + 1);

    _cascade->scale = scale;
    _cascade->real_window_size.width = cvRound( _cascade->orig_window_size.width * scale );
    _cascade->real_window_size.height = cvRound( _cascade->orig_window_size.height * scale );

    equRect.x = equRect.y = cvRound(scale);
    equRect.width = cvRound((_cascade->orig_window_size.width - 2) * scale);
    equRect.height = cvRound((_cascade->orig_window_size.height - 2) * scale);
    weight_scale = 1. / (equRect.width * equRect.height);
    cascade->inv_window_area = weight_scale;

    cascade->pq0 = equRect.x;
    cascade->pq1 = equRect.y;
    cascade->pq2 = equRect.x + equRect.width;
    cascade->pq3 = equRect.y + equRect.height;

    cascade->p0 = equRect.x;
    cascade->p1 = equRect.y;
    cascade->p2 = equRect.x + equRect.width;
    cascade->p3 = equRect.y + equRect.height;


    /* init pointers in haar features according to real window size and
    given image pointers */
    for( i = 0; i < _cascade->count; i++ )
    {
        int j, k, l;
        for( j = 0; j < stage_classifier[i].count; j++ )
        {
            for( l = 0; l < stage_classifier[i].classifier[j].count; l++ )
            {
                CvHaarFeature *feature =
                    &_cascade->stage_classifier[i].classifier[j].haar_feature[l];
                GpuHidHaarTreeNode *hidnode = &stage_classifier[i].classifier[j].node[l];
                double sum0 = 0, area0 = 0;
                CvRect r[3];

                int base_w = -1, base_h = -1;
                int new_base_w = 0, new_base_h = 0;
                int kx, ky;
                int flagx = 0, flagy = 0;
                int x0 = 0, y0 = 0;
                int nr;

                /* align blocks */
                for( k = 0; k < CV_HAAR_FEATURE_MAX; k++ )
                {
                    if(!hidnode->p[k][0])
                        break;
                    r[k] = feature->rect[k].r;
                    base_w = (int)CV_IMIN( (unsigned)base_w, (unsigned)(r[k].width - 1) );
                    base_w = (int)CV_IMIN( (unsigned)base_w, (unsigned)(r[k].x - r[0].x - 1) );
                    base_h = (int)CV_IMIN( (unsigned)base_h, (unsigned)(r[k].height - 1) );
                    base_h = (int)CV_IMIN( (unsigned)base_h, (unsigned)(r[k].y - r[0].y - 1) );
                }

                nr = k;
                base_w += 1;
                base_h += 1;
                if(base_w == 0)
                    base_w = 1;
                kx = r[0].width / base_w;
                if(base_h == 0)
                    base_h = 1;
                ky = r[0].height / base_h;

                if( kx <= 0 )
                {
                    flagx = 1;
                    new_base_w = cvRound( r[0].width * scale ) / kx;
                    x0 = cvRound( r[0].x * scale );
                }

                if( ky <= 0 )
                {
                    flagy = 1;
                    new_base_h = cvRound( r[0].height * scale ) / ky;
                    y0 = cvRound( r[0].y * scale );
                }

                for( k = 0; k < nr; k++ )
                {
                    CvRect tr;
                    double correction_ratio;

                    if( flagx )
                    {
                        tr.x = (r[k].x - r[0].x) * new_base_w / base_w + x0;
                        tr.width = r[k].width * new_base_w / base_w;
                    }
                    else
                    {
                        tr.x = cvRound( r[k].x * scale );
                        tr.width = cvRound( r[k].width * scale );
                    }

                    if( flagy )
                    {
                        tr.y = (r[k].y - r[0].y) * new_base_h / base_h + y0;
                        tr.height = r[k].height * new_base_h / base_h;
                    }
                    else
                    {
                        tr.y = cvRound( r[k].y * scale );
                        tr.height = cvRound( r[k].height * scale );
                    }

#if CV_ADJUST_WEIGHTS
                    {
                        // RAINER START
                        const float orig_feature_size =  (float)(feature->rect[k].r.width) * feature->rect[k].r.height;
                        const float orig_norm_size = (float)(_cascade->orig_window_size.width) * (_cascade->orig_window_size.height);
                        const float feature_size = float(tr.width * tr.height);
                        //const float normSize    = float(equRect.width*equRect.height);
                        float target_ratio = orig_feature_size / orig_norm_size;
                        //float isRatio = featureSize / normSize;
                        //correctionRatio = targetRatio / isRatio / normSize;
                        correction_ratio = target_ratio / feature_size;
                        // RAINER END
                    }
#else
                    correction_ratio = weight_scale * (!feature->tilted ? 1 : 0.5);
#endif

                    if( !feature->tilted )
                    {
                        hidnode->p[k][0] = tr.x;
                        hidnode->p[k][1] = tr.y;
                        hidnode->p[k][2] = tr.x + tr.width;
                        hidnode->p[k][3] = tr.y + tr.height;
                    }
                    else
                    {
                        hidnode->p[k][2] = (tr.y + tr.width) * step + tr.x + tr.width;
                        hidnode->p[k][3] = (tr.y + tr.width + tr.height) * step + tr.x + tr.width - tr.height;
                        hidnode->p[k][0] = tr.y * step + tr.x;
                        hidnode->p[k][1] = (tr.y + tr.height) * step + tr.x - tr.height;
                    }
                    hidnode->weight[k] = (float)(feature->rect[k].weight * correction_ratio);
                    if( k == 0 )
                        area0 = tr.width * tr.height;
                    else
                        sum0 += hidnode->weight[k] * tr.width * tr.height;
                }
                hidnode->weight[0] = (float)(-sum0 / area0);
            } /* l */
        } /* j */
    }
}
591

592
static void gpuSetHaarClassifierCascade( CvHaarClassifierCascade *_cascade)
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
{
    GpuHidHaarClassifierCascade *cascade;
    int i;
    int datasize;
    int total;
    CvRect equRect;
    double weight_scale;
    GpuHidHaarStageClassifier *stage_classifier;

    if( !CV_IS_HAAR_CLASSIFIER(_cascade) )
        CV_Error( !_cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier pointer" );

    if( !_cascade->hid_cascade )
        gpuCreateHidHaarClassifierCascade(_cascade, &datasize, &total);

    cascade = (GpuHidHaarClassifierCascade *) _cascade->hid_cascade;
    stage_classifier = (GpuHidHaarStageClassifier *) cascade + 1;

    _cascade->scale = 1.0;
    _cascade->real_window_size.width =  _cascade->orig_window_size.width ;
    _cascade->real_window_size.height = _cascade->orig_window_size.height;

    equRect.x = equRect.y = 1;
    equRect.width = _cascade->orig_window_size.width - 2;
    equRect.height = _cascade->orig_window_size.height - 2;
    weight_scale = 1;
    cascade->inv_window_area = weight_scale;

    cascade->p0 = equRect.x;
    cascade->p1 = equRect.y;
    cascade->p2 = equRect.height;
    cascade->p3 = equRect.width ;
    for( i = 0; i < _cascade->count; i++ )
    {
627
        int j, l;
628 629 630 631
        for( j = 0; j < stage_classifier[i].count; j++ )
        {
            for( l = 0; l < stage_classifier[i].classifier[j].count; l++ )
            {
632
                const CvHaarFeature *feature =
633 634 635
                    &_cascade->stage_classifier[i].classifier[j].haar_feature[l];
                GpuHidHaarTreeNode *hidnode = &stage_classifier[i].classifier[j].node[l];

636
                for( int k = 0; k < CV_HAAR_FEATURE_MAX; k++ )
637
                {
638 639
                    const CvRect tr = feature->rect[k].r;
                    if (tr.width == 0)
640
                        break;
641
                    double correction_ratio = weight_scale * (!feature->tilted ? 1 : 0.5);
642 643 644 645 646 647 648 649 650 651
                    hidnode->p[k][0] = tr.x;
                    hidnode->p[k][1] = tr.y;
                    hidnode->p[k][2] = tr.width;
                    hidnode->p[k][3] = tr.height;
                    hidnode->weight[k] = (float)(feature->rect[k].weight * correction_ratio);
                }
            } /* l */
        } /* j */
    }
}
652 653 654 655 656
void OclCascadeClassifier::detectMultiScale(oclMat &gimg, CV_OUT std::vector<cv::Rect>& faces,
                                            double scaleFactor, int minNeighbors, int flags,
                                            Size minSize, Size maxSize)
//CvSeq *cv::ocl::OclCascadeClassifier::oclHaarDetectObjects( oclMat &gimg, CvMemStorage *storage, double scaleFactor,
//        int minNeighbors, int flags, CvSize minSize, CvSize maxSize)
657
{
658
    CvHaarClassifierCascade *cascade = (CvHaarClassifierCascade*)getOldCascade();
659 660 661 662 663 664 665

    const double GROUP_EPS = 0.2;

    cv::ConcurrentRectVector allCandidates;
    std::vector<cv::Rect> rectList;
    std::vector<int> rweights;
    double factor;
666 667
    int datasize=0;
    int totalclassifier=0;
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687

    GpuHidHaarClassifierCascade *gcascade;
    GpuHidHaarStageClassifier    *stage;
    GpuHidHaarClassifier         *classifier;
    GpuHidHaarTreeNode           *node;

    int *candidate;
    cl_int status;

    bool findBiggestObject = (flags & CV_HAAR_FIND_BIGGEST_OBJECT) != 0;

    if( maxSize.height == 0 || maxSize.width == 0 )
    {
        maxSize.height = gimg.rows;
        maxSize.width = gimg.cols;
    }

    if( !CV_IS_HAAR_CLASSIFIER(cascade) )
        CV_Error( !cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier cascade" );

688 689
    //if( !storage )
    //    CV_Error( CV_StsNullPtr, "Null storage pointer" );
690 691 692 693 694 695 696 697 698 699 700

    if( CV_MAT_DEPTH(gimg.type()) != CV_8U )
        CV_Error( CV_StsUnsupportedFormat, "Only 8-bit images are supported" );

    if( scaleFactor <= 1 )
        CV_Error( CV_StsOutOfRange, "scale factor must be > 1" );

    if( findBiggestObject )
        flags &= ~CV_HAAR_SCALE_IMAGE;

    if( !cascade->hid_cascade )
701
        gpuCreateHidHaarClassifierCascade(cascade, &datasize, &totalclassifier);
702

703
    //result_seq = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvAvgComp), storage );
704 705 706

    if( CV_MAT_CN(gimg.type()) > 1 )
    {
707
        oclMat gtemp;
708
        cvtColor( gimg, gtemp, COLOR_BGR2GRAY );
709 710 711 712 713 714 715 716 717
        gimg = gtemp;
    }

    if( findBiggestObject )
        flags &= ~(CV_HAAR_SCALE_IMAGE | CV_HAAR_DO_CANNY_PRUNING);

    if( gimg.cols < minSize.width || gimg.rows < minSize.height )
        CV_Error(CV_StsError, "Image too small");

718
    cl_command_queue qu = getClCommandQueue(Context::getContext());
719
    if( (flags & CV_HAAR_SCALE_IMAGE) )
720 721 722 723 724
    {
        CvSize winSize0 = cascade->orig_window_size;
        int totalheight = 0;
        int indexy = 0;
        CvSize sz;
725 726
        std::vector<CvSize> sizev;
        std::vector<float> scalev;
727 728
        for(factor = 1.f;; factor *= scaleFactor)
        {
Andrey Kamaev's avatar
Andrey Kamaev committed
729
            CvSize winSize( cvRound(winSize0.width * factor), cvRound(winSize0.height * factor) );
730 731
            sz.width     = cvRound( gimg.cols / factor ) + 1;
            sz.height    = cvRound( gimg.rows / factor ) + 1;
Andrey Kamaev's avatar
Andrey Kamaev committed
732
            CvSize sz1( sz.width - winSize0.width - 1,      sz.height - winSize0.height - 1 );
733 734 735 736 737 738 739 740 741 742 743 744 745 746

            if( sz1.width <= 0 || sz1.height <= 0 )
                break;
            if( winSize.width > maxSize.width || winSize.height > maxSize.height )
                break;
            if( winSize.width < minSize.width || winSize.height < minSize.height )
                continue;

            totalheight += sz.height;
            sizev.push_back(sz);
            scalev.push_back(factor);
        }

        oclMat gimg1(gimg.rows, gimg.cols, CV_8UC1);
747 748
        oclMat gsum(totalheight + 4, gimg.cols + 1, CV_32SC1);
        oclMat gsqsum(totalheight + 4, gimg.cols + 1, CV_32FC1);
749

perping's avatar
perping committed
750 751 752 753 754 755 756 757 758
        int sdepth = 0;
        if(Context::getContext()->supportsFeature(FEATURE_CL_DOUBLE))
            sdepth = CV_64FC1;
        else
            sdepth = CV_32FC1;
        sdepth = CV_MAT_DEPTH(sdepth);
        int type = CV_MAKE_TYPE(sdepth, 1);
        oclMat gsqsum_t(totalheight + 4, gimg.cols + 1, type);

759 760 761 762 763 764 765
        cl_mem stagebuffer;
        cl_mem nodebuffer;
        cl_mem candidatebuffer;
        cl_mem scaleinfobuffer;
        cv::Rect roi, roi2;
        cv::Mat imgroi, imgroisq;
        cv::ocl::oclMat resizeroi, gimgroi, gimgroisq;
766

767 768 769 770
        int grp_per_CU = 12;

        size_t blocksize = 8;
        size_t localThreads[3] = { blocksize, blocksize , 1 };
771
        size_t globalThreads[3] = { grp_per_CU *(gsum.clCxt->getDeviceInfo().maxComputeUnits) *localThreads[0],
772 773 774 775 776 777 778 779 780 781 782 783 784 785
                                    localThreads[1], 1
                                  };
        int outputsz = 256 * globalThreads[0] / localThreads[0];
        int loopcount = sizev.size();
        detect_piramid_info *scaleinfo = (detect_piramid_info *)malloc(sizeof(detect_piramid_info) * loopcount);

        for( int i = 0; i < loopcount; i++ )
        {
            sz = sizev[i];
            factor = scalev[i];
            roi = Rect(0, indexy, sz.width, sz.height);
            roi2 = Rect(0, 0, sz.width - 1, sz.height - 1);
            resizeroi = gimg1(roi2);
            gimgroi = gsum(roi);
786
            gimgroisq = gsqsum_t(roi);
787 788 789 790 791 792 793 794 795 796 797 798 799
            int width = gimgroi.cols - 1 - cascade->orig_window_size.width;
            int height = gimgroi.rows - 1 - cascade->orig_window_size.height;
            scaleinfo[i].width_height = (width << 16) | height;


            int grpnumperline = (width + localThreads[0] - 1) / localThreads[0];
            int totalgrp = ((height + localThreads[1] - 1) / localThreads[1]) * grpnumperline;

            scaleinfo[i].grpnumperline_totalgrp = (grpnumperline << 16) | totalgrp;
            scaleinfo[i].imgoff = gimgroi.offset >> 2;
            scaleinfo[i].factor = factor;
            cv::ocl::resize(gimg, resizeroi, Size(sz.width - 1, sz.height - 1), 0, 0, INTER_LINEAR);
            cv::ocl::integral(resizeroi, gimgroi, gimgroisq);
800

801 802
            indexy += sz.height;
        }
perping's avatar
perping committed
803
        if(gsqsum_t.depth() == CV_64F)
perping's avatar
perping committed
804 805 806
            gsqsum_t.convertTo(gsqsum, CV_32FC1);
        else
            gsqsum = gsqsum_t;
807

808 809 810 811 812 813 814
        gcascade   = (GpuHidHaarClassifierCascade *)cascade->hid_cascade;
        stage      = (GpuHidHaarStageClassifier *)(gcascade + 1);
        classifier = (GpuHidHaarClassifier *)(stage + gcascade->count);
        node       = (GpuHidHaarTreeNode *)(classifier->node);

        int nodenum = (datasize - sizeof(GpuHidHaarClassifierCascade) -
                       sizeof(GpuHidHaarStageClassifier) * gcascade->count - sizeof(GpuHidHaarClassifier) * totalclassifier) / sizeof(GpuHidHaarTreeNode);
815

816 817
        candidate = (int *)malloc(4 * sizeof(int) * outputsz);

818
        gpuSetImagesForHaarClassifierCascade( cascade, 1., gsum.step / 4 );
819

niko's avatar
niko committed
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));
niko's avatar
niko committed
827
        candidatebuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_WRITE_ONLY, 4 * sizeof(int) * outputsz);
828

829 830
        scaleinfobuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_READ_ONLY, sizeof(detect_piramid_info) * loopcount);
        openCLSafeCall(clEnqueueWriteBuffer(qu, scaleinfobuffer, 1, 0, sizeof(detect_piramid_info)*loopcount, scaleinfo, 0, NULL, NULL));
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847

        int startstage = 0;
        int endstage = gcascade->count;
        int startnode = 0;
        int pixelstep = gsum.step / 4;
        int splitstage = 3;
        int splitnode = stage[0].count + stage[1].count + stage[2].count;
        cl_int4 p, pq;
        p.s[0] = gcascade->p0;
        p.s[1] = gcascade->p1;
        p.s[2] = gcascade->p2;
        p.s[3] = gcascade->p3;
        pq.s[0] = gcascade->pq0;
        pq.s[1] = gcascade->pq1;
        pq.s[2] = gcascade->pq2;
        pq.s[3] = gcascade->pq3;
        float correction = gcascade->inv_window_area;
Niko's avatar
Niko committed
848

849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
        std::vector<std::pair<size_t, const void *> > args;
        args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&stagebuffer ));
        args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&scaleinfobuffer ));
        args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&nodebuffer ));
        args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&gsum.data ));
        args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&gsqsum.data ));
        args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&candidatebuffer ));
        args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&pixelstep ));
        args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&loopcount ));
        args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&startstage ));
        args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&splitstage ));
        args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&endstage ));
        args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&startnode ));
        args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&splitnode ));
        args.push_back ( std::make_pair(sizeof(cl_int4) , (void *)&p ));
        args.push_back ( std::make_pair(sizeof(cl_int4) , (void *)&pq ));
        args.push_back ( std::make_pair(sizeof(cl_float) , (void *)&correction ));
yao's avatar
yao committed
866

867 868
        if(gcascade->is_stump_based && gsum.clCxt->supportsFeature(FEATURE_CL_INTEL_DEVICE))
        {
869 870 871
            //setup local group size for "pixel step" = 1
            localThreads[0] = 16;
            localThreads[1] = 32;
872 873
            localThreads[2] = 1;

874
            //calc maximal number of workgroups
875 876 877
            int WGNumX = 1+(sizev[0].width /(localThreads[0]));
            int WGNumY = 1+(sizev[0].height/(localThreads[1]));
            int WGNumZ = loopcount;
878 879
            int WGNumTotal = 0; //accurate number of non-empty workgroups
            int WGNumSampled = 0; //accurate number of workgroups processed only 1/4 part of all pixels. it is made for large images with scale <= 2
880 881
            oclMat      oclWGInfo(1,sizeof(cl_int4) * WGNumX*WGNumY*WGNumZ,CV_8U);
            {
882
                cl_int4*    pWGInfo = (cl_int4*)clEnqueueMapBuffer(getClCommandQueue(oclWGInfo.clCxt),(cl_mem)oclWGInfo.datastart,true,CL_MAP_WRITE, 0, oclWGInfo.step, 0,0,0,&status);
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
                openCLVerifyCall(status);
                for(int z=0;z<WGNumZ;++z)
                {
                    int     Width  = (scaleinfo[z].width_height >> 16)&0xFFFF;
                    int     Height = (scaleinfo[z].width_height >> 0 )& 0xFFFF;
                    for(int y=0;y<WGNumY;++y)
                    {
                        int     gy = y*localThreads[1];
                        if(gy>=(Height-cascade->orig_window_size.height))
                            continue; // no data to process
                        for(int x=0;x<WGNumX;++x)
                        {
                            int     gx = x*localThreads[0];
                            if(gx>=(Width-cascade->orig_window_size.width))
                                continue; // no data to process

899 900 901 902
                            if(scaleinfo[z].factor<=2)
                            {
                                WGNumSampled++;
                            }
903
                            // save no-empty workgroup info into array
904 905 906 907 908
                            pWGInfo[WGNumTotal].s[0] = scaleinfo[z].width_height;
                            pWGInfo[WGNumTotal].s[1] = (gx << 16) | gy;
                            pWGInfo[WGNumTotal].s[2] = scaleinfo[z].imgoff;
                            memcpy(&(pWGInfo[WGNumTotal].s[3]),&(scaleinfo[z].factor),sizeof(float));
                            WGNumTotal++;
909 910 911 912 913 914
                        }
                    }
                }
                openCLSafeCall(clEnqueueUnmapMemObject(getClCommandQueue(oclWGInfo.clCxt),(cl_mem)oclWGInfo.datastart,pWGInfo,0,0,0));
                pWGInfo = NULL;
            }
915

916
#define NODE_SIZE 12
917
            // pack node info to have less memory loads on the device side
918 919 920
            oclMat  oclNodesPK(1,sizeof(cl_int) * NODE_SIZE * nodenum,CV_8U);
            {
                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 923 924 925
                openCLVerifyCall(status);
                //use known local data stride to precalulate indexes
                int DATA_SIZE_X = (localThreads[0]+cascade->orig_window_size.width);
                // 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 < (int)USHRT_MAX);
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
                for(int i = 0;i<nodenum;++i)
                {//process each node from classifier
                    struct NodePK
                    {
                        unsigned short  slm_index[3][4];
                        float           weight[3];
                        float           threshold;
                        float           alpha[2];
                    };
                    struct NodePK * pOut = (struct NodePK *)(pNodesPK + NODE_SIZE*i);
                    for(int k=0;k<3;++k)
                    {// calc 4 short indexes in shared local mem for each rectangle instead of 2 (x,y) pair.
                        int* p = &(node[i].p[k][0]);
                        pOut->slm_index[k][0] = (unsigned short)(p[1]*DATA_SIZE_X+p[0]);
                        pOut->slm_index[k][1] = (unsigned short)(p[1]*DATA_SIZE_X+p[2]);
                        pOut->slm_index[k][2] = (unsigned short)(p[3]*DATA_SIZE_X+p[0]);
                        pOut->slm_index[k][3] = (unsigned short)(p[3]*DATA_SIZE_X+p[2]);
                    }
                    //store used float point values for each node
                    pOut->weight[0] = node[i].weight[0];
                    pOut->weight[1] = node[i].weight[1];
                    pOut->weight[2] = node[i].weight[2];
                    pOut->threshold = node[i].threshold;
                    pOut->alpha[0] = node[i].alpha[0];
951
                   pOut->alpha[1] = node[i].alpha[1];
952 953 954 955 956
                }
                openCLSafeCall(clEnqueueUnmapMemObject(getClCommandQueue(oclNodesPK.clCxt),(cl_mem)oclNodesPK.datastart,pNodesPK,0,0,0));
                pNodesPK = NULL;
            }
            // add 2 additional buffers (WGinfo and packed nodes) as 2 last args
957 958
            args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&oclNodesPK.datastart ));
            args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&oclWGInfo.datastart ));
959 960

            //form build options for kernel
961
            String  options = "-D PACKED_CLASSIFIER";
962 963 964 965 966 967 968
            options += format(" -D NODE_SIZE=%d",NODE_SIZE);
            options += format(" -D WND_SIZE_X=%d",cascade->orig_window_size.width);
            options += format(" -D WND_SIZE_Y=%d",cascade->orig_window_size.height);
            options += format(" -D STUMP_BASED=%d",gcascade->is_stump_based);
            options += format(" -D SPLITNODE=%d",splitnode);
            options += format(" -D SPLITSTAGE=%d",splitstage);
            options += format(" -D OUTPUTSZ=%d",outputsz);
969 970 971 972

            // init candiate global count by 0
            int pattern = 0;
            openCLSafeCall(clEnqueueWriteBuffer(qu, candidatebuffer, 1, 0, 1 * sizeof(pattern),&pattern, 0, NULL, NULL));
973 974 975 976 977 978 979 980 981

            if(WGNumTotal>WGNumSampled)
            {// small images and each pixel is processed
                // setup global sizes to have linear array of workgroups with WGNum size
                int     pixelstep = 1;
                size_t  LS[3]={localThreads[0]/pixelstep,localThreads[1]/pixelstep,1};
                globalThreads[0] = LS[0]*(WGNumTotal-WGNumSampled);
                globalThreads[1] = LS[1];
                globalThreads[2] = 1;
982
                String options1 = options;
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
                options1 += format(" -D PIXEL_STEP=%d",pixelstep);
                options1 += format(" -D WGSTART=%d",WGNumSampled);
                options1 += format(" -D LSx=%d",LS[0]);
                options1 += format(" -D LSy=%d",LS[1]);
                // execute face detector
                openCLExecuteKernel(gsum.clCxt, &haarobjectdetect, "gpuRunHaarClassifierCascadePacked", globalThreads, LS, args, -1, -1, options1.c_str());
            }
            if(WGNumSampled>0)
            {// large images each 4th pixel is processed
                // setup global sizes to have linear array of workgroups with WGNum size
                int     pixelstep = 2;
                size_t  LS[3]={localThreads[0]/pixelstep,localThreads[1]/pixelstep,1};
                globalThreads[0] = LS[0]*WGNumSampled;
                globalThreads[1] = LS[1];
                globalThreads[2] = 1;
998
                String options2 = options;
999 1000 1001 1002 1003 1004 1005
                options2 += format(" -D PIXEL_STEP=%d",pixelstep);
                options2 += format(" -D WGSTART=%d",0);
                options2 += format(" -D LSx=%d",LS[0]);
                options2 += format(" -D LSy=%d",LS[1]);
                // execute face detector
                openCLExecuteKernel(gsum.clCxt, &haarobjectdetect, "gpuRunHaarClassifierCascadePacked", globalThreads, LS, args, -1, -1, options2.c_str());
            }
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
            //read candidate buffer back and put it into host list
            openCLReadBuffer( gsum.clCxt, candidatebuffer, candidate, 4 * sizeof(int)*outputsz );
            assert(candidate[0]<outputsz);
            //printf("candidate[0]=%d\n",candidate[0]);
            for(int i = 1; i <= candidate[0]; i++)
            {
                allCandidates.push_back(Rect(candidate[4 * i], candidate[4 * i + 1],candidate[4 * i + 2], candidate[4 * i + 3]));
            }
        }
        else
        {
            const char * build_options = gcascade->is_stump_based ? "-D STUMP_BASED=1" : "-D STUMP_BASED=0";

            openCLExecuteKernel(gsum.clCxt, &haarobjectdetect, "gpuRunHaarClassifierCascade", globalThreads, localThreads, args, -1, -1, build_options);
1020

1021 1022 1023 1024 1025 1026 1027
            openCLReadBuffer( gsum.clCxt, candidatebuffer, candidate, 4 * sizeof(int)*outputsz );

            for(int i = 0; i < outputsz; i++)
                if(candidate[4 * i + 2] != 0)
                    allCandidates.push_back(Rect(candidate[4 * i], candidate[4 * i + 1],
                    candidate[4 * i + 2], candidate[4 * i + 3]));
        }
1028

1029 1030 1031 1032 1033 1034
        free(scaleinfo);
        free(candidate);
        openCLSafeCall(clReleaseMemObject(stagebuffer));
        openCLSafeCall(clReleaseMemObject(scaleinfobuffer));
        openCLSafeCall(clReleaseMemObject(nodebuffer));
        openCLSafeCall(clReleaseMemObject(candidatebuffer));
1035

1036 1037 1038 1039 1040 1041 1042
    }
    else
    {
        CvSize winsize0 = cascade->orig_window_size;
        int n_factors = 0;
        oclMat gsum;
        oclMat gsqsum;
perping's avatar
perping committed
1043
        oclMat gsqsum_t;
1044
        cv::ocl::integral(gimg, gsum, gsqsum_t);
perping's avatar
perping committed
1045 1046 1047 1048
        if(gsqsum_t.depth() == CV_64F)
            gsqsum_t.convertTo(gsqsum, CV_32FC1);
        else
            gsqsum = gsqsum_t;
1049
        CvSize sz;
1050 1051
        std::vector<CvSize> sizev;
        std::vector<float> scalev;
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
        gpuSetHaarClassifierCascade(cascade);
        gcascade   = (GpuHidHaarClassifierCascade *)cascade->hid_cascade;
        stage      = (GpuHidHaarStageClassifier *)(gcascade + 1);
        classifier = (GpuHidHaarClassifier *)(stage + gcascade->count);
        node       = (GpuHidHaarTreeNode *)(classifier->node);
        cl_mem stagebuffer;
        cl_mem nodebuffer;
        cl_mem candidatebuffer;
        cl_mem scaleinfobuffer;
        cl_mem pbuffer;
        cl_mem correctionbuffer;
        for( n_factors = 0, factor = 1;
                cvRound(factor * winsize0.width) < gimg.cols - 10 &&
                cvRound(factor * winsize0.height) < gimg.rows - 10;
                n_factors++, factor *= scaleFactor )
        {
Andrey Kamaev's avatar
Andrey Kamaev committed
1068
            CvSize winSize( cvRound( winsize0.width * factor ), cvRound( winsize0.height * factor ) );
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
            if( winSize.width < minSize.width || winSize.height < minSize.height )
            {
                continue;
            }
            sizev.push_back(winSize);
            scalev.push_back(factor);
        }
        int loopcount = scalev.size();
        if(loopcount == 0)
        {
            loopcount = 1;
            n_factors = 1;
            sizev.push_back(minSize);
1082
            scalev.push_back( std::min(cvRound(minSize.width / winsize0.width), cvRound(minSize.height / winsize0.height)) );
1083 1084 1085 1086 1087 1088 1089
        }
        detect_piramid_info *scaleinfo = (detect_piramid_info *)malloc(sizeof(detect_piramid_info) * loopcount);
        cl_int4 *p = (cl_int4 *)malloc(sizeof(cl_int4) * loopcount);
        float *correction = (float *)malloc(sizeof(float) * loopcount);
        int grp_per_CU = 12;
        size_t blocksize = 8;
        size_t localThreads[3] = { blocksize, blocksize , 1 };
1090
        size_t globalThreads[3] = { grp_per_CU *gsum.clCxt->getDeviceInfo().maxComputeUnits *localThreads[0],
1091
                                    localThreads[1], 1 };
1092 1093 1094
        int outputsz = 256 * globalThreads[0] / localThreads[0];
        int nodenum = (datasize - sizeof(GpuHidHaarClassifierCascade) -
                       sizeof(GpuHidHaarStageClassifier) * gcascade->count - sizeof(GpuHidHaarClassifier) * totalclassifier) / sizeof(GpuHidHaarTreeNode);
niko's avatar
niko committed
1095
        nodebuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_READ_ONLY,
1096
                                        nodenum * sizeof(GpuHidHaarTreeNode));
1097
        openCLSafeCall(clEnqueueWriteBuffer(qu, nodebuffer, 1, 0,
1098 1099
                                            nodenum * sizeof(GpuHidHaarTreeNode),
                                            node, 0, NULL, NULL));
niko's avatar
niko committed
1100
        cl_mem newnodebuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_READ_WRITE,
1101
                               loopcount * nodenum * sizeof(GpuHidHaarTreeNode));
1102 1103 1104 1105 1106 1107
        int startstage = 0;
        int endstage = gcascade->count;
        for(int i = 0; i < loopcount; i++)
        {
            sz = sizev[i];
            factor = scalev[i];
perping's avatar
perping committed
1108 1109 1110 1111 1112
            double ystep = std::max(2., factor);
            int equRect_x = cvRound(factor * gcascade->p0);
            int equRect_y = cvRound(factor * gcascade->p1);
            int equRect_w = cvRound(factor * gcascade->p3);
            int equRect_h = cvRound(factor * gcascade->p2);
1113 1114 1115 1116 1117 1118 1119 1120 1121
            p[i].s[0] = equRect_x;
            p[i].s[1] = equRect_y;
            p[i].s[2] = equRect_x + equRect_w;
            p[i].s[3] = equRect_y + equRect_h;
            correction[i] = 1. / (equRect_w * equRect_h);
            int width = (gsum.cols - 1 - sz.width  + ystep - 1) / ystep;
            int height = (gsum.rows - 1 - sz.height + ystep - 1) / ystep;
            int grpnumperline = (width + localThreads[0] - 1) / localThreads[0];
            int totalgrp = ((height + localThreads[1] - 1) / localThreads[1]) * grpnumperline;
1122

1123 1124 1125 1126 1127 1128
            scaleinfo[i].width_height = (width << 16) | height;
            scaleinfo[i].grpnumperline_totalgrp = (grpnumperline << 16) | totalgrp;
            scaleinfo[i].imgoff = 0;
            scaleinfo[i].factor = factor;
            int startnodenum = nodenum * i;
            float factor2 = (float)factor;
1129

1130 1131 1132 1133 1134 1135
            std::vector<std::pair<size_t, const void *> > args1;
            args1.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&nodebuffer ));
            args1.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&newnodebuffer ));
            args1.push_back ( std::make_pair(sizeof(cl_float) , (void *)&factor2 ));
            args1.push_back ( std::make_pair(sizeof(cl_float) , (void *)&correction[i] ));
            args1.push_back ( std::make_pair(sizeof(cl_int) , (void *)&startnodenum ));
1136 1137 1138

            size_t globalThreads2[3] = {nodenum, 1, 1};
            openCLExecuteKernel(gsum.clCxt, &haarobjectdetect_scaled2, "gpuscaleclassifier", globalThreads2, NULL/*localThreads2*/, args1, -1, -1);
1139
        }
1140

1141 1142 1143
        int step = gsum.step / 4;
        int startnode = 0;
        int splitstage = 3;
niko's avatar
niko committed
1144
        stagebuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_READ_ONLY, sizeof(GpuHidHaarStageClassifier) * gcascade->count);
1145
        openCLSafeCall(clEnqueueWriteBuffer(qu, stagebuffer, 1, 0, sizeof(GpuHidHaarStageClassifier)*gcascade->count, stage, 0, NULL, NULL));
niko's avatar
niko committed
1146 1147
        candidatebuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_WRITE_ONLY | CL_MEM_ALLOC_HOST_PTR, 4 * sizeof(int) * outputsz);
        scaleinfobuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_READ_ONLY, sizeof(detect_piramid_info) * loopcount);
1148
        openCLSafeCall(clEnqueueWriteBuffer(qu, scaleinfobuffer, 1, 0, sizeof(detect_piramid_info)*loopcount, scaleinfo, 0, NULL, NULL));
niko's avatar
niko committed
1149
        pbuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_READ_ONLY, sizeof(cl_int4) * loopcount);
1150
        openCLSafeCall(clEnqueueWriteBuffer(qu, pbuffer, 1, 0, sizeof(cl_int4)*loopcount, p, 0, NULL, NULL));
niko's avatar
niko committed
1151
        correctionbuffer = openCLCreateBuffer(gsum.clCxt, CL_MEM_READ_ONLY, sizeof(cl_float) * loopcount);
1152
        openCLSafeCall(clEnqueueWriteBuffer(qu, correctionbuffer, 1, 0, sizeof(cl_float)*loopcount, correction, 0, NULL, NULL));
niko's avatar
niko committed
1153

1154 1155 1156 1157 1158 1159 1160
        std::vector<std::pair<size_t, const void *> > args;
        args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&stagebuffer ));
        args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&scaleinfobuffer ));
        args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&newnodebuffer ));
        args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&gsum.data ));
        args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&gsqsum.data ));
        args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&candidatebuffer ));
1161 1162
        args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&gsum.rows ));
        args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&gsum.cols ));
1163 1164 1165 1166 1167 1168 1169 1170 1171
        args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&step ));
        args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&loopcount ));
        args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&startstage ));
        args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&splitstage ));
        args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&endstage ));
        args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&startnode ));
        args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&pbuffer ));
        args.push_back ( std::make_pair(sizeof(cl_mem) , (void *)&correctionbuffer ));
        args.push_back ( std::make_pair(sizeof(cl_int) , (void *)&nodenum ));
1172
        const char * build_options = gcascade->is_stump_based ? "-D STUMP_BASED=1" : "-D STUMP_BASED=0";
1173
        openCLExecuteKernel(gsum.clCxt, &haarobjectdetect_scaled2, "gpuRunHaarClassifierCascade_scaled2", globalThreads, localThreads, args, -1, -1, build_options);
1174

1175
        candidate = (int *)clEnqueueMapBuffer(qu, candidatebuffer, 1, CL_MAP_READ, 0, 4 * sizeof(int) * outputsz, 0, 0, 0, &status);
1176 1177 1178

        for(int i = 0; i < outputsz; i++)
        {
1179 1180
            if(candidate[4 * i + 2] != 0)
                allCandidates.push_back(Rect(candidate[4 * i], candidate[4 * i + 1], candidate[4 * i + 2], candidate[4 * i + 3]));
1181 1182 1183 1184 1185
        }

        free(scaleinfo);
        free(p);
        free(correction);
1186
        clEnqueueUnmapMemObject(qu, candidatebuffer, candidate, 0, 0, 0);
1187 1188 1189 1190 1191 1192 1193 1194
        openCLSafeCall(clReleaseMemObject(stagebuffer));
        openCLSafeCall(clReleaseMemObject(scaleinfobuffer));
        openCLSafeCall(clReleaseMemObject(nodebuffer));
        openCLSafeCall(clReleaseMemObject(newnodebuffer));
        openCLSafeCall(clReleaseMemObject(candidatebuffer));
        openCLSafeCall(clReleaseMemObject(pbuffer));
        openCLSafeCall(clReleaseMemObject(correctionbuffer));
    }
1195

1196 1197 1198 1199 1200
    cvFree(&cascade->hid_cascade);
    rectList.resize(allCandidates.size());
    if(!allCandidates.empty())
        std::copy(allCandidates.begin(), allCandidates.end(), rectList.begin());

1201 1202 1203 1204 1205
    if( minNeighbors != 0 || findBiggestObject )
        groupRectangles(rectList, rweights, std::max(minNeighbors, 1), GROUP_EPS);
    else
        rweights.resize(rectList.size(), 0);

1206
    faces.clear();
1207 1208
    if( findBiggestObject && rectList.size() )
    {
1209
        Rect result_comp(0, 0, 0, 0);
1210 1211 1212
        for( size_t i = 0; i < rectList.size(); i++ )
        {
            cv::Rect r = rectList[i];
1213
            if( r.area() > result_comp.area() )
1214
            {
1215
                result_comp = r;
1216 1217
            }
        }
1218
        faces.push_back(result_comp);
1219 1220 1221
    }
    else
    {
1222
        faces = rectList;
1223 1224
    }
}