objdetect_hog.cl 24.8 KB
Newer Older
yao's avatar
yao committed
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/*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, Multicoreware, Inc., all rights reserved.
// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// @Authors
//    Wenju He, wenju@multicorewareinc.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
//     and/or other materials provided with the distribution.
//
//   * 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*/

#define CELL_WIDTH 8
#define CELL_HEIGHT 8
#define CELLS_PER_BLOCK_X 2
#define CELLS_PER_BLOCK_Y 2
#define NTHREADS 256
#define CV_PI_F 3.1415926535897932384626433832795f

yao's avatar
yao committed
53 54 55
//----------------------------------------------------------------------------
// Histogram computation
// 12 threads for a cell, 12x4 threads per block
56
// Use pre-computed gaussian and interp_weight lookup tables
yao's avatar
yao committed
57 58
__kernel void compute_hists_lut_kernel(
    const int cblock_stride_x, const int cblock_stride_y,
59
    const int cnbins, const int cblock_hist_size, const int img_block_width,
yao's avatar
yao committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    const int blocks_in_group, const int blocks_total,
    const int grad_quadstep, const int qangle_step,
    __global const float* grad, __global const uchar* qangle,
    __global const float* gauss_w_lut,
    __global float* block_hists, __local float* smem)
{
    const int lx = get_local_id(0);
    const int lp = lx / 24; /* local group id */
    const int gid = get_group_id(0) * blocks_in_group + lp;/* global group id */
    const int gidY = gid / img_block_width;
    const int gidX = gid - gidY * img_block_width;

    const int lidX = lx - lp * 24;
    const int lidY = get_local_id(1);

    const int cell_x = lidX / 12;
    const int cell_y = lidY;
    const int cell_thread_x = lidX - cell_x * 12;

79
    __local float* hists = smem + lp * cnbins * (CELLS_PER_BLOCK_X *
yao's avatar
yao committed
80
        CELLS_PER_BLOCK_Y * 12 + CELLS_PER_BLOCK_X * CELLS_PER_BLOCK_Y);
81
    __local float* final_hist = hists + cnbins *
yao's avatar
yao committed
82 83 84 85 86
        (CELLS_PER_BLOCK_X * CELLS_PER_BLOCK_Y * 12);

    const int offset_x = gidX * cblock_stride_x + (cell_x << 2) + cell_thread_x;
    const int offset_y = gidY * cblock_stride_y + (cell_y << 2);

87
    __global const float* grad_ptr = (gid < blocks_total) ?
yao's avatar
yao committed
88 89 90 91
        grad + offset_y * grad_quadstep + (offset_x << 1) : grad;
    __global const uchar* qangle_ptr = (gid < blocks_total) ?
        qangle + offset_y * qangle_step + (offset_x << 1) : qangle;

92
    __local float* hist = hists + 12 * (cell_y * CELLS_PER_BLOCK_Y + cell_x) +
yao's avatar
yao committed
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 130 131 132
        cell_thread_x;
    for (int bin_id = 0; bin_id < cnbins; ++bin_id)
        hist[bin_id * 48] = 0.f;

    const int dist_x = -4 + cell_thread_x - 4 * cell_x;
    const int dist_center_x = dist_x - 4 * (1 - 2 * cell_x);

    const int dist_y_begin = -4 - 4 * lidY;
    for (int dist_y = dist_y_begin; dist_y < dist_y_begin + 12; ++dist_y)
    {
        float2 vote = (float2) (grad_ptr[0], grad_ptr[1]);
        uchar2 bin = (uchar2) (qangle_ptr[0], qangle_ptr[1]);

        grad_ptr += grad_quadstep;
        qangle_ptr += qangle_step;

        int dist_center_y = dist_y - 4 * (1 - 2 * cell_y);

        int idx = (dist_center_y + 8) * 16 + (dist_center_x + 8);
        float gaussian = gauss_w_lut[idx];
        idx = (dist_y + 8) * 16 + (dist_x + 8);
        float interp_weight = gauss_w_lut[256+idx];

        hist[bin.x * 48] += gaussian * interp_weight * vote.x;
        hist[bin.y * 48] += gaussian * interp_weight * vote.y;
    }
    barrier(CLK_LOCAL_MEM_FENCE);

    volatile __local float* hist_ = hist;
    for (int bin_id = 0; bin_id < cnbins; ++bin_id, hist_ += 48)
    {
        if (cell_thread_x < 6)
            hist_[0] += hist_[6];
        barrier(CLK_LOCAL_MEM_FENCE);
        if (cell_thread_x < 3)
            hist_[0] += hist_[3];
#ifdef CPU
        barrier(CLK_LOCAL_MEM_FENCE);
#endif
        if (cell_thread_x == 0)
133
            final_hist[(cell_x * 2 + cell_y) * cnbins + bin_id] =
yao's avatar
yao committed
134 135
                hist_[0] + hist_[1] + hist_[2];
    }
yao's avatar
yao committed
136
#ifdef CPU
yao's avatar
yao committed
137
    barrier(CLK_LOCAL_MEM_FENCE);
yao's avatar
yao committed
138
#endif
yao's avatar
yao committed
139 140 141

    int tid = (cell_y * CELLS_PER_BLOCK_Y + cell_x) * 12 + cell_thread_x;
    if ((tid < cblock_hist_size) && (gid < blocks_total))
142
    {
143
        __global float* block_hist = block_hists +
144
            (gidY * img_block_width + gidX) * cblock_hist_size;
yao's avatar
yao committed
145
        block_hist[tid] = final_hist[tid];
146
    }
yao's avatar
yao committed
147 148
}

yao's avatar
yao committed
149 150 151
//-------------------------------------------------------------
//  Normalization of histograms via L2Hys_norm
//  optimized for the case of 9 bins
152
__kernel void normalize_hists_36_kernel(__global float* block_hists,
yao's avatar
yao committed
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 191 192 193 194 195 196 197 198 199
                                        const float threshold, __local float *squares)
{
    const int tid = get_local_id(0);
    const int gid = get_global_id(0);
    const int bid = tid / 36;      /* block-hist id, (0 - 6) */
    const int boffset = bid * 36;  /* block-hist offset in the work-group */
    const int hid = tid - boffset; /* histogram bin id, (0 - 35) */

    float elem = block_hists[gid];
    squares[tid] = elem * elem;
    barrier(CLK_LOCAL_MEM_FENCE);

    __local float* smem = squares + boffset;
    float sum = smem[hid];
    if (hid < 18)
        smem[hid] = sum = sum + smem[hid + 18];
    barrier(CLK_LOCAL_MEM_FENCE);
    if (hid < 9)
        smem[hid] = sum = sum + smem[hid + 9];
    barrier(CLK_LOCAL_MEM_FENCE);
    if (hid < 4)
        smem[hid] = sum + smem[hid + 4];
    barrier(CLK_LOCAL_MEM_FENCE);
    sum = smem[0] + smem[1] + smem[2] + smem[3] + smem[8];

    elem = elem / (sqrt(sum) + 3.6f);
    elem = min(elem, threshold);

    barrier(CLK_LOCAL_MEM_FENCE);
    squares[tid] = elem * elem;
    barrier(CLK_LOCAL_MEM_FENCE);

    sum = smem[hid];
    if (hid < 18)
      smem[hid] = sum = sum + smem[hid + 18];
    barrier(CLK_LOCAL_MEM_FENCE);
    if (hid < 9)
        smem[hid] = sum = sum + smem[hid + 9];
    barrier(CLK_LOCAL_MEM_FENCE);
    if (hid < 4)
        smem[hid] = sum + smem[hid + 4];
    barrier(CLK_LOCAL_MEM_FENCE);
    sum = smem[0] + smem[1] + smem[2] + smem[3] + smem[8];

    block_hists[gid] = elem / (sqrt(sum) + 1e-3f);
}

yao's avatar
yao committed
200 201 202 203 204 205 206 207
//-------------------------------------------------------------
//  Normalization of histograms via L2Hys_norm
//
float reduce_smem(volatile __local float* smem, int size)
{
    unsigned int tid = get_local_id(0);
    float sum = smem[tid];

208
    if (size >= 512) { if (tid < 256) smem[tid] = sum = sum + smem[tid + 256];
yao's avatar
yao committed
209
        barrier(CLK_LOCAL_MEM_FENCE); }
210
    if (size >= 256) { if (tid < 128) smem[tid] = sum = sum + smem[tid + 128];
yao's avatar
yao committed
211
        barrier(CLK_LOCAL_MEM_FENCE); }
212
    if (size >= 128) { if (tid < 64) smem[tid] = sum = sum + smem[tid + 64];
yao's avatar
yao committed
213 214
        barrier(CLK_LOCAL_MEM_FENCE); }
#ifdef CPU
215
    if (size >= 64) { if (tid < 32) smem[tid] = sum = sum + smem[tid + 32];
yao's avatar
yao committed
216
        barrier(CLK_LOCAL_MEM_FENCE); }
217
    if (size >= 32) { if (tid < 16) smem[tid] = sum = sum + smem[tid + 16];
yao's avatar
yao committed
218
        barrier(CLK_LOCAL_MEM_FENCE); }
219
    if (size >= 16) { if (tid < 8) smem[tid] = sum = sum + smem[tid + 8];
yao's avatar
yao committed
220
        barrier(CLK_LOCAL_MEM_FENCE); }
221 222 223 224 225
    if (size >= 8) { if (tid < 4) smem[tid] = sum = sum + smem[tid + 4];
        barrier(CLK_LOCAL_MEM_FENCE); }
    if (size >= 4) { if (tid < 2) smem[tid] = sum = sum + smem[tid + 2];
        barrier(CLK_LOCAL_MEM_FENCE); }
    if (size >= 2) { if (tid < 1) smem[tid] = sum = sum + smem[tid + 1];
yao's avatar
yao committed
226 227
        barrier(CLK_LOCAL_MEM_FENCE); }
#else
yao's avatar
yao committed
228 229 230
    if (tid < 32)
    {
        if (size >= 64) smem[tid] = sum = sum + smem[tid + 32];
yao's avatar
yao committed
231 232 233 234
#if WAVE_SIZE < 32
    } barrier(CLK_LOCAL_MEM_FENCE);
    if (tid < 16) {
#endif
yao's avatar
yao committed
235 236 237 238 239 240
        if (size >= 32) smem[tid] = sum = sum + smem[tid + 16];
        if (size >= 16) smem[tid] = sum = sum + smem[tid + 8];
        if (size >= 8) smem[tid] = sum = sum + smem[tid + 4];
        if (size >= 4) smem[tid] = sum = sum + smem[tid + 2];
        if (size >= 2) smem[tid] = sum = sum + smem[tid + 1];
    }
yao's avatar
yao committed
241
#endif
yao's avatar
yao committed
242 243 244 245

    return sum;
}

yao's avatar
yao committed
246 247 248
__kernel void normalize_hists_kernel(
    const int nthreads, const int block_hist_size, const int img_block_width,
    __global float* block_hists, const float threshold, __local float *squares)
yao's avatar
yao committed
249 250 251 252 253
{
    const int tid = get_local_id(0);
    const int gidX = get_group_id(0);
    const int gidY = get_group_id(1);

254
    __global float* hist = block_hists + (gidY * img_block_width + gidX) *
yao's avatar
yao committed
255
        block_hist_size + tid;
yao's avatar
yao committed
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

    float elem = 0.f;
    if (tid < block_hist_size)
        elem = hist[0];

    squares[tid] = elem * elem;

    barrier(CLK_LOCAL_MEM_FENCE);
    float sum = reduce_smem(squares, nthreads);

    float scale = 1.0f / (sqrt(sum) + 0.1f * block_hist_size);
    elem = min(elem * scale, threshold);

    barrier(CLK_LOCAL_MEM_FENCE);
    squares[tid] = elem * elem;

    barrier(CLK_LOCAL_MEM_FENCE);
    sum = reduce_smem(squares, nthreads);
    scale = 1.0f / (sqrt(sum) + 1e-3f);

    if (tid < block_hist_size)
        hist[0] = elem * scale;
}

//---------------------------------------------------------------------
//  Linear SVM based classification
yao's avatar
yao committed
282 283 284 285 286 287 288 289
//  48x96 window, 9 bins and default parameters
//  180 threads, each thread corresponds to a bin in a row
__kernel void classify_hists_180_kernel(
    const int cdescr_width, const int cdescr_height, const int cblock_hist_size,
    const int img_win_width, const int img_block_width,
    const int win_block_stride_x, const int win_block_stride_y,
    __global const float * block_hists, __global const float* coefs,
    float free_coef, float threshold, __global uchar* labels)
yao's avatar
yao committed
290 291 292 293 294
{
    const int tid = get_local_id(0);
    const int gidX = get_group_id(0);
    const int gidY = get_group_id(1);

295
    __global const float* hist = block_hists + (gidY * win_block_stride_y *
yao's avatar
yao committed
296
        img_block_width + gidX * win_block_stride_x) * cblock_hist_size;
yao's avatar
yao committed
297 298

    float product = 0.f;
yao's avatar
yao committed
299 300

    for (int i = 0; i < cdescr_height; i++)
yao's avatar
yao committed
301
    {
302
        product += coefs[i * cdescr_width + tid] *
yao's avatar
yao committed
303
            hist[i * img_block_width * cblock_hist_size + tid];
yao's avatar
yao committed
304 305
    }

yao's avatar
yao committed
306
    __local float products[180];
yao's avatar
yao committed
307 308 309 310

    products[tid] = product;

    barrier(CLK_LOCAL_MEM_FENCE);
311

yao's avatar
yao committed
312
    if (tid < 90) products[tid] = product = product + products[tid + 90];
yao's avatar
yao committed
313
    barrier(CLK_LOCAL_MEM_FENCE);
314

yao's avatar
yao committed
315
    if (tid < 45) products[tid] = product = product + products[tid + 45];
yao's avatar
yao committed
316 317
    barrier(CLK_LOCAL_MEM_FENCE);

318
    volatile __local float* smem = products;
yao's avatar
yao committed
319 320
#ifdef CPU
    if (tid < 13) smem[tid] = product = product + smem[tid + 32];
321
    barrier(CLK_LOCAL_MEM_FENCE);
yao's avatar
yao committed
322
    if (tid < 16) smem[tid] = product = product + smem[tid + 16];
323 324 325 326 327 328 329
    barrier(CLK_LOCAL_MEM_FENCE);
    if(tid<8) smem[tid] = product = product + smem[tid + 8];
    barrier(CLK_LOCAL_MEM_FENCE);
    if(tid<4) smem[tid] = product = product + smem[tid + 4];
    barrier(CLK_LOCAL_MEM_FENCE);
    if(tid<2) smem[tid] = product = product + smem[tid + 2];
    barrier(CLK_LOCAL_MEM_FENCE);
yao's avatar
yao committed
330 331
#else
    if (tid < 13)
yao's avatar
yao committed
332 333
    {
        smem[tid] = product = product + smem[tid + 32];
yao's avatar
yao committed
334
    }
yao's avatar
yao committed
335 336 337
#if WAVE_SIZE < 32
    barrier(CLK_LOCAL_MEM_FENCE);
#endif
yao's avatar
yao committed
338 339
    if (tid < 16)
    {
yao's avatar
yao committed
340 341
        smem[tid] = product = product + smem[tid + 16];
        smem[tid] = product = product + smem[tid + 8];
yao's avatar
yao committed
342 343
        smem[tid] = product = product + smem[tid + 4];
        smem[tid] = product = product + smem[tid + 2];
344 345
    }
#endif
yao's avatar
yao committed
346 347

    if (tid == 0){
348
        product = product + smem[tid + 1];
yao's avatar
yao committed
349
        labels[gidY * img_win_width + gidX] = (product + free_coef >= threshold);
350
    }
yao's avatar
yao committed
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
}

//---------------------------------------------------------------------
//  Linear SVM based classification
//  64x128 window, 9 bins and default parameters
//  256 threads, 252 of them are used
__kernel void classify_hists_252_kernel(
    const int cdescr_width, const int cdescr_height, const int cblock_hist_size,
    const int img_win_width, const int img_block_width,
    const int win_block_stride_x, const int win_block_stride_y,
    __global const float * block_hists, __global const float* coefs,
    float free_coef, float threshold, __global uchar* labels)
{
    const int tid = get_local_id(0);
    const int gidX = get_group_id(0);
    const int gidY = get_group_id(1);

368
    __global const float* hist = block_hists + (gidY * win_block_stride_y *
yao's avatar
yao committed
369 370 371 372 373 374
        img_block_width + gidX * win_block_stride_x) * cblock_hist_size;

    float product = 0.f;
    if (tid < cdescr_width)
    {
        for (int i = 0; i < cdescr_height; i++)
375
            product += coefs[i * cdescr_width + tid] *
yao's avatar
yao committed
376
                hist[i * img_block_width * cblock_hist_size + tid];
377
    }
yao's avatar
yao committed
378 379 380 381 382

    __local float products[NTHREADS];

    products[tid] = product;

383
    barrier(CLK_LOCAL_MEM_FENCE);
yao's avatar
yao committed
384 385 386 387 388 389 390

    if (tid < 128) products[tid] = product = product + products[tid + 128];
    barrier(CLK_LOCAL_MEM_FENCE);

    if (tid < 64) products[tid] = product = product + products[tid + 64];
    barrier(CLK_LOCAL_MEM_FENCE);

391
    volatile __local float* smem = products;
yao's avatar
yao committed
392
#ifdef CPU
393 394 395 396 397 398 399 400 401 402
    if(tid<32) smem[tid] = product = product + smem[tid + 32];
    barrier(CLK_LOCAL_MEM_FENCE);
    if(tid<16) smem[tid] = product = product + smem[tid + 16];
    barrier(CLK_LOCAL_MEM_FENCE);
    if(tid<8) smem[tid] = product = product + smem[tid + 8];
    barrier(CLK_LOCAL_MEM_FENCE);
    if(tid<4) smem[tid] = product = product + smem[tid + 4];
    barrier(CLK_LOCAL_MEM_FENCE);
    if(tid<2) smem[tid] = product = product + smem[tid + 2];
    barrier(CLK_LOCAL_MEM_FENCE);
yao's avatar
yao committed
403 404
#else
    if (tid < 32)
405
    {
yao's avatar
yao committed
406
        smem[tid] = product = product + smem[tid + 32];
yao's avatar
yao committed
407 408 409 410
#if WAVE_SIZE < 32
    } barrier(CLK_LOCAL_MEM_FENCE);
    if (tid < 16) {
#endif
yao's avatar
yao committed
411 412 413
        smem[tid] = product = product + smem[tid + 16];
        smem[tid] = product = product + smem[tid + 8];
        smem[tid] = product = product + smem[tid + 4];
yao's avatar
yao committed
414
        smem[tid] = product = product + smem[tid + 2];
415 416
    }
#endif
yao's avatar
yao committed
417
    if (tid == 0){
418
        product = product + smem[tid + 1];
yao's avatar
yao committed
419
        labels[gidY * img_win_width + gidX] = (product + free_coef >= threshold);
420
    }
yao's avatar
yao committed
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
}

//---------------------------------------------------------------------
//  Linear SVM based classification
//  256 threads
__kernel void classify_hists_kernel(
    const int cdescr_size, const int cdescr_width, const int cblock_hist_size,
    const int img_win_width, const int img_block_width,
    const int win_block_stride_x, const int win_block_stride_y,
    __global const float * block_hists, __global const float* coefs,
    float free_coef, float threshold, __global uchar* labels)
{
    const int tid = get_local_id(0);
    const int gidX = get_group_id(0);
    const int gidY = get_group_id(1);

437
    __global const float* hist = block_hists + (gidY * win_block_stride_y *
yao's avatar
yao committed
438 439 440 441 442 443 444
        img_block_width + gidX * win_block_stride_x) * cblock_hist_size;

    float product = 0.f;
    for (int i = tid; i < cdescr_size; i += NTHREADS)
    {
        int offset_y = i / cdescr_width;
        int offset_x = i - offset_y * cdescr_width;
445
        product += coefs[i] *
yao's avatar
yao committed
446
            hist[offset_y * img_block_width * cblock_hist_size + offset_x];
yao's avatar
yao committed
447 448
    }

yao's avatar
yao committed
449 450 451 452 453 454 455 456 457 458 459 460
    __local float products[NTHREADS];

    products[tid] = product;

    barrier(CLK_LOCAL_MEM_FENCE);

    if (tid < 128) products[tid] = product = product + products[tid + 128];
    barrier(CLK_LOCAL_MEM_FENCE);

    if (tid < 64) products[tid] = product = product + products[tid + 64];
    barrier(CLK_LOCAL_MEM_FENCE);

461
    volatile __local float* smem = products;
yao's avatar
yao committed
462
#ifdef CPU
463 464 465 466 467 468 469 470 471 472
    if(tid<32) smem[tid] = product = product + smem[tid + 32];
    barrier(CLK_LOCAL_MEM_FENCE);
    if(tid<16) smem[tid] = product = product + smem[tid + 16];
    barrier(CLK_LOCAL_MEM_FENCE);
    if(tid<8) smem[tid] = product = product + smem[tid + 8];
    barrier(CLK_LOCAL_MEM_FENCE);
    if(tid<4) smem[tid] = product = product + smem[tid + 4];
    barrier(CLK_LOCAL_MEM_FENCE);
    if(tid<2) smem[tid] = product = product + smem[tid + 2];
    barrier(CLK_LOCAL_MEM_FENCE);
yao's avatar
yao committed
473 474
#else
    if (tid < 32)
475
    {
yao's avatar
yao committed
476
        smem[tid] = product = product + smem[tid + 32];
yao's avatar
yao committed
477 478 479 480
#if WAVE_SIZE < 32
    } barrier(CLK_LOCAL_MEM_FENCE);
    if (tid < 16) {
#endif
yao's avatar
yao committed
481 482 483 484 485 486 487
        smem[tid] = product = product + smem[tid + 16];
        smem[tid] = product = product + smem[tid + 8];
        smem[tid] = product = product + smem[tid + 4];
        smem[tid] = product = product + smem[tid + 2];
    }
#endif
    if (tid == 0){
488
        smem[tid] = product = product + smem[tid + 1];
yao's avatar
yao committed
489
        labels[gidY * img_win_width + gidX] = (product + free_coef >= threshold);
490
    }
yao's avatar
yao committed
491 492 493 494 495
}

//----------------------------------------------------------------------------
// Extract descriptors

yao's avatar
yao committed
496
__kernel void extract_descrs_by_rows_kernel(
497 498
    const int cblock_hist_size, const int descriptors_quadstep,
    const int cdescr_size, const int cdescr_width, const int img_block_width,
yao's avatar
yao committed
499 500
    const int win_block_stride_x, const int win_block_stride_y,
    __global const float* block_hists, __global float* descriptors)
yao's avatar
yao committed
501 502 503 504
{
    int tid = get_local_id(0);
    int gidX = get_group_id(0);
    int gidY = get_group_id(1);
505

yao's avatar
yao committed
506
    // Get left top corner of the window in src
507
    __global const float* hist = block_hists + (gidY * win_block_stride_y *
yao's avatar
yao committed
508
        img_block_width + gidX * win_block_stride_x) * cblock_hist_size;
yao's avatar
yao committed
509 510

    // Get left top corner of the window in dst
511
    __global float* descriptor = descriptors +
yao's avatar
yao committed
512
        (gidY * get_num_groups(0) + gidX) * descriptors_quadstep;
yao's avatar
yao committed
513 514 515 516 517 518 519 520 521 522

    // Copy elements from src to dst
    for (int i = tid; i < cdescr_size; i += NTHREADS)
    {
        int offset_y = i / cdescr_width;
        int offset_x = i - offset_y * cdescr_width;
        descriptor[i] = hist[offset_y * img_block_width * cblock_hist_size + offset_x];
    }
}

yao's avatar
yao committed
523 524
__kernel void extract_descrs_by_cols_kernel(
    const int cblock_hist_size, const int descriptors_quadstep, const int cdescr_size,
525 526
    const int cnblocks_win_x, const int cnblocks_win_y, const int img_block_width,
    const int win_block_stride_x, const int win_block_stride_y,
yao's avatar
yao committed
527
    __global const float* block_hists, __global float* descriptors)
yao's avatar
yao committed
528 529 530 531 532 533
{
    int tid = get_local_id(0);
    int gidX = get_group_id(0);
    int gidY = get_group_id(1);

    // Get left top corner of the window in src
534
    __global const float* hist = block_hists +  (gidY * win_block_stride_y *
yao's avatar
yao committed
535
        img_block_width + gidX * win_block_stride_x) * cblock_hist_size;
yao's avatar
yao committed
536 537

    // Get left top corner of the window in dst
538
    __global float* descriptor = descriptors +
yao's avatar
yao committed
539
        (gidY * get_num_groups(0) + gidX) * descriptors_quadstep;
yao's avatar
yao committed
540 541 542 543 544 545 546 547 548 549

    // Copy elements from src to dst
    for (int i = tid; i < cdescr_size; i += NTHREADS)
    {
        int block_idx = i / cblock_hist_size;
        int idx_in_block = i - block_idx * cblock_hist_size;

        int y = block_idx / cnblocks_win_x;
        int x = block_idx - y * cnblocks_win_x;

550
        descriptor[(x * cnblocks_win_y + y) * cblock_hist_size + idx_in_block] =
yao's avatar
yao committed
551
            hist[(y * img_block_width  + x) * cblock_hist_size + idx_in_block];
yao's avatar
yao committed
552 553 554 555 556 557
    }
}

//----------------------------------------------------------------------------
// Gradients computation

yao's avatar
yao committed
558
__kernel void compute_gradients_8UC4_kernel(
559
    const int height, const int width,
yao's avatar
yao committed
560 561 562
    const int img_step, const int grad_quadstep, const int qangle_step,
    const __global uchar4 * img, __global float * grad, __global uchar * qangle,
    const float angle_scale, const char correct_gamma, const int cnbins)
yao's avatar
yao committed
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 591 592 593 594 595 596 597 598 599 600 601 602
{
    const int x = get_global_id(0);
    const int tid = get_local_id(0);
    const int gSizeX = get_local_size(0);
    const int gidX = get_group_id(0);
    const int gidY = get_group_id(1);

    __global const uchar4* row = img + gidY * img_step;

    __local float sh_row[(NTHREADS + 2) * 3];

    uchar4 val;
    if (x < width)
        val = row[x];
    else
        val = row[width - 2];

    sh_row[tid + 1] = val.x;
    sh_row[tid + 1 + (NTHREADS + 2)] = val.y;
    sh_row[tid + 1 + 2 * (NTHREADS + 2)] = val.z;

    if (tid == 0)
    {
        val = row[max(x - 1, 1)];
        sh_row[0] = val.x;
        sh_row[(NTHREADS + 2)] = val.y;
        sh_row[2 * (NTHREADS + 2)] = val.z;
    }

    if (tid == gSizeX - 1)
    {
        val = row[min(x + 1, width - 2)];
        sh_row[gSizeX + 1] = val.x;
        sh_row[gSizeX + 1 + (NTHREADS + 2)] = val.y;
        sh_row[gSizeX + 1 + 2 * (NTHREADS + 2)] = val.z;
    }

    barrier(CLK_LOCAL_MEM_FENCE);
    if (x < width)
    {
603
        float3 a = (float3) (sh_row[tid], sh_row[tid + (NTHREADS + 2)],
yao's avatar
yao committed
604
            sh_row[tid + 2 * (NTHREADS + 2)]);
605
        float3 b = (float3) (sh_row[tid + 2], sh_row[tid + 2 + (NTHREADS + 2)],
yao's avatar
yao committed
606
            sh_row[tid + 2 + 2 * (NTHREADS + 2)]);
yao's avatar
yao committed
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660

        float3 dx;
        if (correct_gamma == 1)
            dx = sqrt(b) - sqrt(a);
        else
            dx = b - a;

        float3 dy = (float3) 0.f;

        if (gidY > 0 && gidY < height - 1)
        {
            a = convert_float3(img[(gidY - 1) * img_step + x].xyz);
            b = convert_float3(img[(gidY + 1) * img_step + x].xyz);

            if (correct_gamma == 1)
                dy = sqrt(b) - sqrt(a);
            else
                dy = b - a;
        }

        float best_dx = dx.x;
        float best_dy = dy.x;

        float mag0 = dx.x * dx.x + dy.x * dy.x;
        float mag1 = dx.y * dx.y + dy.y * dy.y;
        if (mag0 < mag1)
        {
            best_dx = dx.y;
            best_dy = dy.y;
            mag0 = mag1;
        }

        mag1 = dx.z * dx.z + dy.z * dy.z;
        if (mag0 < mag1)
        {
            best_dx = dx.z;
            best_dy = dy.z;
            mag0 = mag1;
        }

        mag0 = sqrt(mag0);

        float ang = (atan2(best_dy, best_dx) + CV_PI_F) * angle_scale - 0.5f;
        int hidx = (int)floor(ang);
        ang -= hidx;
        hidx = (hidx + cnbins) % cnbins;

        qangle[(gidY * qangle_step + x) << 1] = hidx;
        qangle[((gidY * qangle_step + x) << 1) + 1] = (hidx + 1) % cnbins;
        grad[(gidY * grad_quadstep + x) << 1] = mag0 * (1.f - ang);
        grad[((gidY * grad_quadstep + x) << 1) + 1] = mag0 * ang;
    }
}

yao's avatar
yao committed
661
__kernel void compute_gradients_8UC1_kernel(
662
    const int height, const int width,
yao's avatar
yao committed
663 664 665
    const int img_step, const int grad_quadstep, const int qangle_step,
    __global const uchar * img, __global float * grad, __global uchar * qangle,
    const float angle_scale, const char correct_gamma, const int cnbins)
yao's avatar
yao committed
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
{
    const int x = get_global_id(0);
    const int tid = get_local_id(0);
    const int gSizeX = get_local_size(0);
    const int gidX = get_group_id(0);
    const int gidY = get_group_id(1);

    __global const uchar* row = img + gidY * img_step;

    __local float sh_row[NTHREADS + 2];

    if (x < width)
        sh_row[tid + 1] = row[x];
    else
        sh_row[tid + 1] = row[width - 2];

    if (tid == 0)
        sh_row[0] = row[max(x - 1, 1)];

    if (tid == gSizeX - 1)
        sh_row[gSizeX + 1] = row[min(x + 1, width - 2)];

    barrier(CLK_LOCAL_MEM_FENCE);
    if (x < width)
    {
        float dx;

        if (correct_gamma == 1)
            dx = sqrt(sh_row[tid + 2]) - sqrt(sh_row[tid]);
        else
            dx = sh_row[tid + 2] - sh_row[tid];

        float dy = 0.f;
        if (gidY > 0 && gidY < height - 1)
        {
            float a = (float) img[ (gidY + 1) * img_step + x ];
            float b = (float) img[ (gidY - 1) * img_step + x ];
            if (correct_gamma == 1)
                dy = sqrt(a) - sqrt(b);
            else
                dy = a - b;
        }
        float mag = sqrt(dx * dx + dy * dy);

        float ang = (atan2(dy, dx) + CV_PI_F) * angle_scale - 0.5f;
        int hidx = (int)floor(ang);
        ang -= hidx;
        hidx = (hidx + cnbins) % cnbins;

        qangle[ (gidY * qangle_step + x) << 1 ]     = hidx;
        qangle[ ((gidY * qangle_step + x) << 1) + 1 ] = (hidx + 1) % cnbins;
        grad[ (gidY * grad_quadstep + x) << 1 ]       = mag * (1.f - ang);
        grad[ ((gidY * grad_quadstep + x) << 1) + 1 ]   = mag * ang;
    }
720
}