bgfg_mog.cpp 24.3 KB
Newer Older
wester 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 53 54 55 56 57 58 59 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 130 131 132 133 134 135 136 137 138 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 191 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 242 243 244 245 246 247 248 249 250 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 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 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 591 592 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 627 628 629 630 631 632 633 634 635 636 637 638
/*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-2013, Multicoreware, Inc., all rights reserved.
// Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// @Authors
//    Jin Ma, jin@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*/

#include "precomp.hpp"
#include "opencl_kernels.hpp"

using namespace cv;
using namespace cv::ocl;

namespace cv
{
    namespace ocl
    {
        typedef struct _contant_struct
        {
            cl_float c_Tb;
            cl_float c_TB;
            cl_float c_Tg;
            cl_float c_varInit;
            cl_float c_varMin;
            cl_float c_varMax;
            cl_float c_tau;
            cl_uchar c_shadowVal;
        }contant_struct;

        cl_mem cl_constants = NULL;
        float c_TB;
    }
}

#if defined _MSC_VER
#define snprintf sprintf_s
#endif

namespace cv { namespace ocl { namespace device
{
    namespace mog
    {
        void mog_ocl(const oclMat& frame, int cn, oclMat& fgmask, oclMat& weight, oclMat& sortKey, oclMat& mean, oclMat& var,
            int nmixtures, float varThreshold, float learningRate, float backgroundRatio, float noiseSigma);

        void getBackgroundImage_ocl(int cn, const oclMat& weight, const oclMat& mean, oclMat& dst, int nmixtures, float backgroundRatio);

        void loadConstants(float Tb, float TB, float Tg, float varInit, float varMin, float varMax, float tau,
                            unsigned char shadowVal);

        void mog2_ocl(const oclMat& frame, int cn, oclMat& fgmask, oclMat& modesUsed, oclMat& weight, oclMat& variance, oclMat& mean,
                      float alphaT, float prune, bool detectShadows, int nmixtures);

        void getBackgroundImage2_ocl(int cn, const oclMat& modesUsed, const oclMat& weight, const oclMat& mean, oclMat& dst, int nmixtures);
    }
}}}

namespace mog
{
    const int defaultNMixtures = 5;
    const int defaultHistory = 200;
    const float defaultBackgroundRatio = 0.7f;
    const float defaultVarThreshold = 2.5f * 2.5f;
    const float defaultNoiseSigma = 30.0f * 0.5f;
}
void cv::ocl::BackgroundSubtractor::operator()(const oclMat&, oclMat&, float)
{

}
cv::ocl::BackgroundSubtractor::~BackgroundSubtractor()
{

}

cv::ocl::MOG::MOG(int nmixtures) :
frameSize_(0, 0), frameType_(0), nframes_(0)
{
    nmixtures_ = std::min(nmixtures > 0 ? nmixtures : mog::defaultNMixtures, 8);
    history = mog::defaultHistory;
    varThreshold = mog::defaultVarThreshold;
    backgroundRatio = mog::defaultBackgroundRatio;
    noiseSigma = mog::defaultNoiseSigma;
}

void cv::ocl::MOG::initialize(cv::Size frameSize, int frameType)
{
    CV_Assert(frameType == CV_8UC1 || frameType == CV_8UC3 || frameType == CV_8UC4);

    frameSize_ = frameSize;
    frameType_ = frameType;

    int ch = CV_MAT_CN(frameType);
    int work_ch = ch;

    // for each gaussian mixture of each pixel bg model we store
    // the mixture sort key (w/sum_of_variances), the mixture weight (w),
    // the mean (nchannels values) and
    // the diagonal covariance matrix (another nchannels values)

    weight_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC1);
    sortKey_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC1);
    mean_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC(work_ch));
    var_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC(work_ch));

    weight_.setTo(cv::Scalar::all(0));
    sortKey_.setTo(cv::Scalar::all(0));
    mean_.setTo(cv::Scalar::all(0));
    var_.setTo(cv::Scalar::all(0));

    nframes_ = 0;
}

void cv::ocl::MOG::operator()(const cv::ocl::oclMat& frame, cv::ocl::oclMat& fgmask, float learningRate)
{
    using namespace cv::ocl::device::mog;

    CV_Assert(frame.depth() == CV_8U);

    int ch = frame.oclchannels();
    int work_ch = ch;

    if (nframes_ == 0 || learningRate >= 1.0 || frame.size() != frameSize_ || work_ch != mean_.oclchannels())
        initialize(frame.size(), frame.type());

    fgmask.create(frameSize_, CV_8UC1);

    ++nframes_;
    learningRate = learningRate >= 0.0f && nframes_ > 1 ? learningRate : 1.0f / std::min(nframes_, history);
    CV_Assert(learningRate >= 0.0f);

    mog_ocl(frame, ch, fgmask, weight_, sortKey_, mean_, var_, nmixtures_,
        varThreshold, learningRate, backgroundRatio, noiseSigma);
}

void cv::ocl::MOG::getBackgroundImage(oclMat& backgroundImage) const
{
    using namespace cv::ocl::device::mog;

    backgroundImage.create(frameSize_, frameType_);

    cv::ocl::device::mog::getBackgroundImage_ocl(backgroundImage.oclchannels(), weight_, mean_, backgroundImage, nmixtures_, backgroundRatio);
}

void cv::ocl::MOG::release()
{
    frameSize_ = Size(0, 0);
    frameType_ = 0;
    nframes_ = 0;

    weight_.release();
    sortKey_.release();
    mean_.release();
    var_.release();
    clReleaseMemObject(cl_constants);
}

static void mog_withoutLearning(const oclMat& frame, int cn, oclMat& fgmask, oclMat& weight, oclMat& mean, oclMat& var,
    int nmixtures, float varThreshold, float backgroundRatio)
{
    Context* clCxt = Context::getContext();

    size_t local_thread[] = {32, 8, 1};
    size_t global_thread[] = {(size_t)frame.cols, (size_t)frame.rows, 1};

    int frame_step = (int)(frame.step/frame.elemSize());
    int fgmask_step = (int)(fgmask.step/fgmask.elemSize());
    int weight_step = (int)(weight.step/weight.elemSize());
    int mean_step = (int)(mean.step/mean.elemSize());
    int var_step = (int)(var.step/var.elemSize());

    int fgmask_offset_y = (int)(fgmask.offset/fgmask.step);
    int fgmask_offset_x = (int)(fgmask.offset%fgmask.step);
    fgmask_offset_x = fgmask_offset_x/(int)fgmask.elemSize();

    int frame_offset_y = (int)(frame.offset/frame.step);
    int frame_offset_x = (int)(frame.offset%frame.step);
    frame_offset_x = frame_offset_x/(int)frame.elemSize();

    char build_option[50];
    if(cn == 1)
    {
        snprintf(build_option, 50, "-D CN1 -D NMIXTURES=%d", nmixtures);
    }else
    {
        snprintf(build_option, 50, "-D NMIXTURES=%d", nmixtures);
    }

    String kernel_name = "mog_withoutLearning_kernel";
    vector< pair<size_t, const void*> > args;

    args.push_back(make_pair(sizeof(cl_mem), (void*)&frame.data));
    args.push_back(make_pair(sizeof(cl_mem), (void*)&fgmask.data));
    args.push_back(make_pair(sizeof(cl_mem), (void*)&weight.data));
    args.push_back(make_pair(sizeof(cl_mem), (void*)&mean.data));
    args.push_back(make_pair(sizeof(cl_mem), (void*)&var.data));

    args.push_back(make_pair(sizeof(cl_int), (void*)&frame.rows));
    args.push_back(make_pair(sizeof(cl_int), (void*)&frame.cols));

    args.push_back(make_pair(sizeof(cl_int), (void*)&frame_step));
    args.push_back(make_pair(sizeof(cl_int), (void*)&fgmask_step));
    args.push_back(make_pair(sizeof(cl_int), (void*)&weight_step));
    args.push_back(make_pair(sizeof(cl_int), (void*)&mean_step));
    args.push_back(make_pair(sizeof(cl_int), (void*)&var_step));

    args.push_back(make_pair(sizeof(cl_float), (void*)&varThreshold));
    args.push_back(make_pair(sizeof(cl_float), (void*)&backgroundRatio));

    args.push_back(make_pair(sizeof(cl_int), (void*)&fgmask_offset_x));
    args.push_back(make_pair(sizeof(cl_int), (void*)&fgmask_offset_y));

    args.push_back(make_pair(sizeof(cl_int), (void*)&frame_offset_x));
    args.push_back(make_pair(sizeof(cl_int), (void*)&frame_offset_y));

    openCLExecuteKernel(clCxt, &bgfg_mog, kernel_name, global_thread, local_thread, args, -1, -1, build_option);
}


static void mog_withLearning(const oclMat& frame, int cn, oclMat& fgmask_raw, oclMat& weight, oclMat& sortKey, oclMat& mean, oclMat& var,
    int nmixtures, float varThreshold, float backgroundRatio, float learningRate, float minVar)
{
    Context* clCxt = Context::getContext();

    size_t local_thread[] = {32, 8, 1};
    size_t global_thread[] = {(size_t)frame.cols, (size_t)frame.rows, 1};

    oclMat fgmask(fgmask_raw.size(), CV_32SC1);

    int frame_step = (int)(frame.step/frame.elemSize());
    int fgmask_step = (int)(fgmask.step/fgmask.elemSize());
    int weight_step = (int)(weight.step/weight.elemSize());
    int sortKey_step = (int)(sortKey.step/sortKey.elemSize());
    int mean_step = (int)(mean.step/mean.elemSize());
    int var_step = (int)(var.step/var.elemSize());

    int fgmask_offset_y = (int)(fgmask.offset/fgmask.step);
    int fgmask_offset_x = (int)(fgmask.offset%fgmask.step);
    fgmask_offset_x = fgmask_offset_x/(int)fgmask.elemSize();

    int frame_offset_y = (int)(frame.offset/frame.step);
    int frame_offset_x = (int)(frame.offset%frame.step);
    frame_offset_x = frame_offset_x/(int)frame.elemSize();

    char build_option[50];
    if(cn == 1)
    {
        snprintf(build_option, 50, "-D CN1 -D NMIXTURES=%d", nmixtures);
    }else
    {
        snprintf(build_option, 50, "-D NMIXTURES=%d", nmixtures);
    }

    String kernel_name = "mog_withLearning_kernel";
    vector< pair<size_t, const void*> > args;

    args.push_back(make_pair(sizeof(cl_mem), (void*)&frame.data));
    args.push_back(make_pair(sizeof(cl_mem), (void*)&fgmask.data));
    args.push_back(make_pair(sizeof(cl_mem), (void*)&weight.data));
    args.push_back(make_pair(sizeof(cl_mem), (void*)&sortKey.data));
    args.push_back(make_pair(sizeof(cl_mem), (void*)&mean.data));
    args.push_back(make_pair(sizeof(cl_mem), (void*)&var.data));

    args.push_back(make_pair(sizeof(cl_int), (void*)&frame.rows));
    args.push_back(make_pair(sizeof(cl_int), (void*)&frame.cols));

    args.push_back(make_pair(sizeof(cl_int), (void*)&frame_step));
    args.push_back(make_pair(sizeof(cl_int), (void*)&fgmask_step));
    args.push_back(make_pair(sizeof(cl_int), (void*)&weight_step));
    args.push_back(make_pair(sizeof(cl_int), (void*)&sortKey_step));
    args.push_back(make_pair(sizeof(cl_int), (void*)&mean_step));
    args.push_back(make_pair(sizeof(cl_int), (void*)&var_step));

    args.push_back(make_pair(sizeof(cl_float), (void*)&varThreshold));
    args.push_back(make_pair(sizeof(cl_float), (void*)&backgroundRatio));
    args.push_back(make_pair(sizeof(cl_float), (void*)&learningRate));
    args.push_back(make_pair(sizeof(cl_float), (void*)&minVar));

    args.push_back(make_pair(sizeof(cl_int), (void*)&fgmask_offset_x));
    args.push_back(make_pair(sizeof(cl_int), (void*)&fgmask_offset_y));

    args.push_back(make_pair(sizeof(cl_int), (void*)&frame_offset_x));
    args.push_back(make_pair(sizeof(cl_int), (void*)&frame_offset_y));

    openCLExecuteKernel(clCxt, &bgfg_mog, kernel_name, global_thread, local_thread, args, -1, -1, build_option);
    fgmask.convertTo(fgmask, CV_8U);
    fgmask.copyTo(fgmask_raw);
}

void cv::ocl::device::mog::mog_ocl(const oclMat& frame, int cn, oclMat& fgmask, oclMat& weight, oclMat& sortKey, oclMat& mean, oclMat& var,
    int nmixtures, float varThreshold, float learningRate, float backgroundRatio, float noiseSigma)
{
    const float minVar = noiseSigma * noiseSigma;

    if(learningRate > 0.0f)
        mog_withLearning(frame, cn, fgmask, weight, sortKey, mean, var, nmixtures,
                         varThreshold, backgroundRatio, learningRate, minVar);
    else
        mog_withoutLearning(frame, cn, fgmask, weight, mean, var, nmixtures, varThreshold, backgroundRatio);
}

void cv::ocl::device::mog::getBackgroundImage_ocl(int cn, const oclMat& weight, const oclMat& mean, oclMat& dst, int nmixtures, float backgroundRatio)
{
    Context* clCxt = Context::getContext();

    size_t local_thread[] = {32, 8, 1};
    size_t global_thread[] = {(size_t)dst.cols, (size_t)dst.rows, 1};

    int weight_step = (int)(weight.step/weight.elemSize());
    int mean_step = (int)(mean.step/mean.elemSize());
    int dst_step = (int)(dst.step/dst.elemSize());

    char build_option[50];
    if(cn == 1)
    {
        snprintf(build_option, 50, "-D CN1 -D NMIXTURES=%d", nmixtures);
    }else
    {
        snprintf(build_option, 50, "-D NMIXTURES=%d", nmixtures);
    }

    String kernel_name = "getBackgroundImage_kernel";
    vector< pair<size_t, const void*> > args;

    args.push_back(make_pair(sizeof(cl_mem), (void*)&weight.data));
    args.push_back(make_pair(sizeof(cl_mem), (void*)&mean.data));
    args.push_back(make_pair(sizeof(cl_mem), (void*)&dst.data));

    args.push_back(make_pair(sizeof(cl_int), (void*)&dst.rows));
    args.push_back(make_pair(sizeof(cl_int), (void*)&dst.cols));

    args.push_back(make_pair(sizeof(cl_int), (void*)&weight_step));
    args.push_back(make_pair(sizeof(cl_int), (void*)&mean_step));
    args.push_back(make_pair(sizeof(cl_int), (void*)&dst_step));

    args.push_back(make_pair(sizeof(cl_float), (void*)&backgroundRatio));

    openCLExecuteKernel(clCxt, &bgfg_mog, kernel_name, global_thread, local_thread, args, -1, -1, build_option);
}

void cv::ocl::device::mog::loadConstants(float Tb, float TB, float Tg, float varInit, float varMin, float varMax, float tau, unsigned char shadowVal)
{
    varMin = cv::min(varMin, varMax);
    varMax = cv::max(varMin, varMax);

    c_TB = TB;

    _contant_struct *constants = new _contant_struct;
    constants->c_Tb = Tb;
    constants->c_TB = TB;
    constants->c_Tg = Tg;
    constants->c_varInit = varInit;
    constants->c_varMin = varMin;
    constants->c_varMax = varMax;
    constants->c_tau = tau;
    constants->c_shadowVal = shadowVal;

    cl_constants = load_constant(*((cl_context*)getClContextPtr()), *((cl_command_queue*)getClCommandQueuePtr()),
        (void *)constants, sizeof(_contant_struct));
}

void cv::ocl::device::mog::mog2_ocl(const oclMat& frame, int cn, oclMat& fgmaskRaw, oclMat& modesUsed, oclMat& weight, oclMat& variance,
                                oclMat& mean, float alphaT, float prune, bool detectShadows, int nmixtures)
{
    oclMat fgmask(fgmaskRaw.size(), CV_32SC1);

    Context* clCxt = Context::getContext();

    const float alpha1 = 1.0f - alphaT;

    cl_int detectShadows_flag = 0;
    if(detectShadows)
        detectShadows_flag = 1;

    size_t local_thread[] = {32, 8, 1};
    size_t global_thread[] = {(size_t)frame.cols, (size_t)frame.rows, 1};

    int frame_step = (int)(frame.step/frame.elemSize());
    int fgmask_step = (int)(fgmask.step/fgmask.elemSize());
    int weight_step = (int)(weight.step/weight.elemSize());
    int modesUsed_step = (int)(modesUsed.step/modesUsed.elemSize());
    int mean_step = (int)(mean.step/mean.elemSize());
    int var_step = (int)(variance.step/variance.elemSize());

    int fgmask_offset_y = (int)(fgmask.offset/fgmask.step);
    int fgmask_offset_x = (int)(fgmask.offset%fgmask.step);
    fgmask_offset_x = fgmask_offset_x/(int)fgmask.elemSize();

    int frame_offset_y = (int)(frame.offset/frame.step);
    int frame_offset_x = (int)(frame.offset%frame.step);
    frame_offset_x = frame_offset_x/(int)frame.elemSize();

    String kernel_name = "mog2_kernel";
    vector< pair<size_t, const void*> > args;

    char build_option[50];
    if(cn == 1)
    {
        snprintf(build_option, 50, "-D CN1 -D NMIXTURES=%d", nmixtures);
    }else
    {
        snprintf(build_option, 50, "-D NMIXTURES=%d", nmixtures);
    }

    args.push_back(make_pair(sizeof(cl_mem), (void*)&frame.data));
    args.push_back(make_pair(sizeof(cl_mem), (void*)&fgmask.data));
    args.push_back(make_pair(sizeof(cl_mem), (void*)&weight.data));
    args.push_back(make_pair(sizeof(cl_mem), (void*)&mean.data));
    args.push_back(make_pair(sizeof(cl_mem), (void*)&modesUsed.data));
    args.push_back(make_pair(sizeof(cl_mem), (void*)&variance.data));

    args.push_back(make_pair(sizeof(cl_int), (void*)&frame.rows));
    args.push_back(make_pair(sizeof(cl_int), (void*)&frame.cols));

    args.push_back(make_pair(sizeof(cl_int), (void*)&frame_step));
    args.push_back(make_pair(sizeof(cl_int), (void*)&fgmask_step));
    args.push_back(make_pair(sizeof(cl_int), (void*)&weight_step));
    args.push_back(make_pair(sizeof(cl_int), (void*)&mean_step));
    args.push_back(make_pair(sizeof(cl_int), (void*)&modesUsed_step));
    args.push_back(make_pair(sizeof(cl_int), (void*)&var_step));

    args.push_back(make_pair(sizeof(cl_float), (void*)&alphaT));
    args.push_back(make_pair(sizeof(cl_float), (void*)&alpha1));
    args.push_back(make_pair(sizeof(cl_float), (void*)&prune));

    args.push_back(make_pair(sizeof(cl_int), (void*)&detectShadows_flag));

    args.push_back(make_pair(sizeof(cl_int), (void*)&fgmask_offset_x));
    args.push_back(make_pair(sizeof(cl_int), (void*)&fgmask_offset_y));

    args.push_back(make_pair(sizeof(cl_int), (void*)&frame_offset_x));
    args.push_back(make_pair(sizeof(cl_int), (void*)&frame_offset_y));
    args.push_back(make_pair(sizeof(cl_mem), (void*)&cl_constants));

    openCLExecuteKernel(clCxt, &bgfg_mog, kernel_name, global_thread, local_thread, args, -1, -1, build_option);

    fgmask.convertTo(fgmask, CV_8U);
    fgmask.copyTo(fgmaskRaw);
}

void cv::ocl::device::mog::getBackgroundImage2_ocl(int cn, const oclMat& modesUsed, const oclMat& weight, const oclMat& mean, oclMat& dst, int nmixtures)
{
    Context* clCxt = Context::getContext();

    size_t local_thread[] = {32, 8, 1};
    size_t global_thread[] = {(size_t)modesUsed.cols, (size_t)modesUsed.rows, 1};

    int weight_step = (int)(weight.step/weight.elemSize());
    int modesUsed_step = (int)(modesUsed.step/modesUsed.elemSize());
    int mean_step = (int)(mean.step/mean.elemSize());
    int dst_step = (int)(dst.step/dst.elemSize());

    int dst_y = (int)(dst.offset/dst.step);
    int dst_x = (int)(dst.offset%dst.step);
    dst_x = dst_x/(int)dst.elemSize();

    String kernel_name = "getBackgroundImage2_kernel";
    vector< pair<size_t, const void*> > args;

    char build_option[50];
    if(cn == 1)
    {
        snprintf(build_option, 50, "-D CN1 -D NMIXTURES=%d", nmixtures);
    }else
    {
        snprintf(build_option, 50, "-D NMIXTURES=%d", nmixtures);
    }

    args.push_back(make_pair(sizeof(cl_mem), (void*)&modesUsed.data));
    args.push_back(make_pair(sizeof(cl_mem), (void*)&weight.data));
    args.push_back(make_pair(sizeof(cl_mem), (void*)&mean.data));
    args.push_back(make_pair(sizeof(cl_mem), (void*)&dst.data));
    args.push_back(make_pair(sizeof(cl_float), (void*)&c_TB));

    args.push_back(make_pair(sizeof(cl_int), (void*)&modesUsed.rows));
    args.push_back(make_pair(sizeof(cl_int), (void*)&modesUsed.cols));

    args.push_back(make_pair(sizeof(cl_int), (void*)&modesUsed_step));
    args.push_back(make_pair(sizeof(cl_int), (void*)&weight_step));
    args.push_back(make_pair(sizeof(cl_int), (void*)&mean_step));
    args.push_back(make_pair(sizeof(cl_int), (void*)&dst_step));

    args.push_back(make_pair(sizeof(cl_int), (void*)&dst_x));
    args.push_back(make_pair(sizeof(cl_int), (void*)&dst_y));

    openCLExecuteKernel(clCxt, &bgfg_mog, kernel_name, global_thread, local_thread, args, -1, -1, build_option);
}

/////////////////////////////////////////////////////////////////
// MOG2

namespace mog2
{
    // default parameters of gaussian background detection algorithm
    const int defaultHistory = 500; // Learning rate; alpha = 1/defaultHistory2
    const float defaultVarThreshold = 4.0f * 4.0f;
    const int defaultNMixtures = 5; // maximal number of Gaussians in mixture
    const float defaultBackgroundRatio = 0.9f; // threshold sum of weights for background test
    const float defaultVarThresholdGen = 3.0f * 3.0f;
    const float defaultVarInit = 15.0f; // initial variance for new components
    const float defaultVarMax = 5.0f * defaultVarInit;
    const float defaultVarMin = 4.0f;

    // additional parameters
    const float defaultfCT = 0.05f; // complexity reduction prior constant 0 - no reduction of number of components
    const unsigned char defaultnShadowDetection = 127; // value to use in the segmentation mask for shadows, set 0 not to do shadow detection
    const float defaultfTau = 0.5f; // Tau - shadow threshold, see the paper for explanation
}

cv::ocl::MOG2::MOG2(int nmixtures) : frameSize_(0, 0), frameType_(0), nframes_(0)
{
    nmixtures_ = nmixtures > 0 ? nmixtures : mog2::defaultNMixtures;

    history = mog2::defaultHistory;
    varThreshold = mog2::defaultVarThreshold;
    bShadowDetection = true;

    backgroundRatio = mog2::defaultBackgroundRatio;
    fVarInit = mog2::defaultVarInit;
    fVarMax  = mog2::defaultVarMax;
    fVarMin = mog2::defaultVarMin;

    varThresholdGen = mog2::defaultVarThresholdGen;
    fCT = mog2::defaultfCT;
    nShadowDetection =  mog2::defaultnShadowDetection;
    fTau = mog2::defaultfTau;
}

void cv::ocl::MOG2::initialize(cv::Size frameSize, int frameType)
{
    using namespace cv::ocl::device::mog;
    CV_Assert(frameType == CV_8UC1 || frameType == CV_8UC3 || frameType == CV_8UC4);

    frameSize_ = frameSize;
    frameType_ = frameType;
    nframes_ = 0;

    int ch = CV_MAT_CN(frameType);
    int work_ch = ch;

    // for each gaussian mixture of each pixel bg model we store ...
    // the mixture weight (w),
    // the mean (nchannels values) and
    // the covariance
    weight_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC1);
    weight_.setTo(Scalar::all(0));

    variance_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC1);
    variance_.setTo(Scalar::all(0));

    mean_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC(work_ch)); //4 channels
    mean_.setTo(Scalar::all(0));

    //make the array for keeping track of the used modes per pixel - all zeros at start
    bgmodelUsedModes_.create(frameSize_, CV_32FC1);
    bgmodelUsedModes_.setTo(cv::Scalar::all(0));

    loadConstants(varThreshold, backgroundRatio, varThresholdGen, fVarInit, fVarMin, fVarMax, fTau, nShadowDetection);
}

void cv::ocl::MOG2::operator()(const oclMat& frame, oclMat& fgmask, float learningRate)
{
    using namespace cv::ocl::device::mog;

    int ch = frame.oclchannels();
    int work_ch = ch;

    if (nframes_ == 0 || learningRate >= 1.0f || frame.size() != frameSize_ || work_ch != mean_.oclchannels())
        initialize(frame.size(), frame.type());

    fgmask.create(frameSize_, CV_8UC1);
    fgmask.setTo(cv::Scalar::all(0));

    ++nframes_;
    learningRate = learningRate >= 0.0f && nframes_ > 1 ? learningRate : 1.0f / std::min(2 * nframes_, history);
    CV_Assert(learningRate >= 0.0f);

    mog2_ocl(frame, frame.oclchannels(), fgmask, bgmodelUsedModes_, weight_, variance_, mean_, learningRate, -learningRate * fCT, bShadowDetection, nmixtures_);
}

void cv::ocl::MOG2::getBackgroundImage(oclMat& backgroundImage) const
{
    using namespace cv::ocl::device::mog;

    backgroundImage.create(frameSize_, frameType_);

    cv::ocl::device::mog::getBackgroundImage2_ocl(backgroundImage.oclchannels(), bgmodelUsedModes_, weight_, mean_, backgroundImage, nmixtures_);
}

void cv::ocl::MOG2::release()
{
    frameSize_ = Size(0, 0);
    frameType_ = 0;
    nframes_ = 0;

    weight_.release();
    variance_.release();
    mean_.release();

    bgmodelUsedModes_.release();
}