Path: blob/master/modules/video/src/opencl/bgfg_knn.cl
16337 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2018 Ya-Chiu Wu, all rights reserved.13// Third party copyrights are property of their respective owners.14//15// @Authors16// Ya-Chiu Wu, yacwu@cs.nctu.edu.tw17//18// Redistribution and use in source and binary forms, with or without modification,19// are permitted provided that the following conditions are met:20//21// * Redistribution's of source code must retain the above copyright notice,22// this list of conditions and the following disclaimer.23//24// * Redistribution's in binary form must reproduce the above copyright notice,25// this list of conditions and the following disclaimer in the documentation26// and/or other materials provided with the distribution.27//28// * The name of the copyright holders may not be used to endorse or promote products29// derived from this software without specific prior written permission.30//31// This software is provided by the copyright holders and contributors "as is" and32// any express or implied warranties, including, but not limited to, the implied33// warranties of merchantability and fitness for a particular purpose are disclaimed.34// In no event shall the Intel Corporation or contributors be liable for any direct,35// indirect, incidental, special, exemplary, or consequential damages36// (including, but not limited to, procurement of substitute goods or services;37// loss of use, data, or profits; or business interruption) however caused38// and on any theory of liability, whether in contract, strict liability,39// or tort (including negligence or otherwise) arising in any way out of40// the use of this software, even if advised of the possibility of such damage.41//42//M*/4344#if CN==14546#define T_MEAN float47#define F_ZERO (0.0f)4849#define frameToMean(a, b) (b) = *(a);50#define meanToFrame(a, b) *b = convert_uchar_sat(a);5152#else5354#define T_MEAN float455#define F_ZERO (0.0f, 0.0f, 0.0f, 0.0f)5657#define meanToFrame(a, b)\58b[0] = convert_uchar_sat(a.x); \59b[1] = convert_uchar_sat(a.y); \60b[2] = convert_uchar_sat(a.z);6162#define frameToMean(a, b)\63b.x = a[0]; \64b.y = a[1]; \65b.z = a[2]; \66b.w = 0.0f;6768#endif6970__kernel void knn_kernel(__global const uchar* frame, int frame_step, int frame_offset, int frame_row, int frame_col,71__global const uchar* nNextLongUpdate,72__global const uchar* nNextMidUpdate,73__global const uchar* nNextShortUpdate,74__global uchar* aModelIndexLong,75__global uchar* aModelIndexMid,76__global uchar* aModelIndexShort,77__global uchar* flag,78__global uchar* sample,79__global uchar* fgmask, int fgmask_step, int fgmask_offset,80int nLongCounter, int nMidCounter, int nShortCounter,81float c_Tb, int c_nkNN, float c_tau82#ifdef SHADOW_DETECT83, uchar c_shadowVal84#endif85)86{87int x = get_global_id(0);88int y = get_global_id(1);8990if( x < frame_col && y < frame_row)91{92__global const uchar* _frame = (frame + mad24(y, frame_step, mad24(x, CN, frame_offset)));93T_MEAN pix;94frameToMean(_frame, pix);9596uchar foreground = 255; // 0 - the pixel classified as background9798int Pbf = 0;99int Pb = 0;100uchar include = 0;101102int pt_idx = mad24(y, frame_col, x);103int idx_step = frame_row * frame_col;104105__global T_MEAN* _sample = (__global T_MEAN*)(sample);106107for (uchar n = 0; n < (NSAMPLES) * 3 ; ++n)108{109int n_idx = mad24(n, idx_step, pt_idx);110111T_MEAN c_mean = _sample[n_idx];112113uchar c_flag = flag[n_idx];114115T_MEAN diff = c_mean - pix;116float dist2 = dot(diff, diff);117118if (dist2 < c_Tb)119{120Pbf++;121if (c_flag)122{123Pb++;124if (Pb >= c_nkNN)125{126include = 1;127foreground = 0;128break;129}130}131}132}133if (Pbf >= c_nkNN)134{135include = 1;136}137138#ifdef SHADOW_DETECT139if (foreground)140{141int Ps = 0;142for (uchar n = 0; n < (NSAMPLES) * 3 ; ++n)143{144int n_idx = mad24(n, idx_step, pt_idx);145uchar c_flag = flag[n_idx];146147if (c_flag)148{149T_MEAN c_mean = _sample[n_idx];150151float numerator = dot(pix, c_mean);152float denominator = dot(c_mean, c_mean);153154if (denominator == 0)155break;156157if (numerator <= denominator && numerator >= c_tau * denominator)158{159float a = numerator / denominator;160161T_MEAN dD = mad(a, c_mean, -pix);162163if (dot(dD, dD) < c_Tb * a * a)164{165Ps++;166if (Ps >= c_nkNN)167{168foreground = c_shadowVal;169break;170}171}172}173}174}175}176#endif177__global uchar* _fgmask = fgmask + mad24(y, fgmask_step, x + fgmask_offset);178*_fgmask = (uchar)foreground;179180__global const uchar* _nNextLongUpdate = nNextLongUpdate + pt_idx;181__global const uchar* _nNextMidUpdate = nNextMidUpdate + pt_idx;182__global const uchar* _nNextShortUpdate = nNextShortUpdate + pt_idx;183__global uchar* _aModelIndexLong = aModelIndexLong + pt_idx;184__global uchar* _aModelIndexMid = aModelIndexMid + pt_idx;185__global uchar* _aModelIndexShort = aModelIndexShort + pt_idx;186187uchar nextLongUpdate = _nNextLongUpdate[0];188uchar nextMidUpdate = _nNextMidUpdate[0];189uchar nextShortUpdate = _nNextShortUpdate[0];190uchar modelIndexLong = _aModelIndexLong[0];191uchar modelIndexMid = _aModelIndexMid[0];192uchar modelIndexShort = _aModelIndexShort[0];193int offsetLong = mad24(mad24(2, (NSAMPLES), modelIndexLong), idx_step, pt_idx);194int offsetMid = mad24((NSAMPLES)+modelIndexMid, idx_step, pt_idx);195int offsetShort = mad24(modelIndexShort, idx_step, pt_idx);196if (nextLongUpdate == nLongCounter)197{198_sample[offsetLong] = _sample[offsetMid];199flag[offsetLong] = flag[offsetMid];200_aModelIndexLong[0] = (modelIndexLong >= ((NSAMPLES)-1)) ? 0 : (modelIndexLong + 1);201}202203if (nextMidUpdate == nMidCounter)204{205_sample[offsetMid] = _sample[offsetShort];206flag[offsetMid] = flag[offsetShort];207_aModelIndexMid[0] = (modelIndexMid >= ((NSAMPLES)-1)) ? 0 : (modelIndexMid + 1);208}209210if (nextShortUpdate == nShortCounter)211{212_sample[offsetShort] = pix;213flag[offsetShort] = include;214_aModelIndexShort[0] = (modelIndexShort >= ((NSAMPLES)-1)) ? 0 : (modelIndexShort + 1);215}216}217}218219__kernel void getBackgroundImage2_kernel(__global const uchar* flag,220__global const uchar* sample,221__global uchar* dst, int dst_step, int dst_offset, int dst_row, int dst_col)222{223int x = get_global_id(0);224int y = get_global_id(1);225226if(x < dst_col && y < dst_row)227{228int pt_idx = mad24(y, dst_col, x);229230T_MEAN meanVal = (T_MEAN)F_ZERO;231232__global T_MEAN* _sample = (__global T_MEAN*)(sample);233int idx_step = dst_row * dst_col;234for (uchar n = 0; n < (NSAMPLES) * 3 ; ++n)235{236int n_idx = mad24(n, idx_step, pt_idx);237uchar c_flag = flag[n_idx];238if(c_flag)239{240meanVal = _sample[n_idx];241break;242}243}244__global uchar* _dst = dst + mad24(y, dst_step, mad24(x, CN, dst_offset));245meanToFrame(meanVal, _dst);246}247}248249250