Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/video/src/opencl/bgfg_knn.cl
16337 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2018 Ya-Chiu Wu, all rights reserved.
14
// Third party copyrights are property of their respective owners.
15
//
16
// @Authors
17
// Ya-Chiu Wu, yacwu@cs.nctu.edu.tw
18
//
19
// Redistribution and use in source and binary forms, with or without modification,
20
// are permitted provided that the following conditions are met:
21
//
22
// * Redistribution's of source code must retain the above copyright notice,
23
// this list of conditions and the following disclaimer.
24
//
25
// * Redistribution's in binary form must reproduce the above copyright notice,
26
// this list of conditions and the following disclaimer in the documentation
27
// and/or other materials provided with the distribution.
28
//
29
// * The name of the copyright holders may not be used to endorse or promote products
30
// derived from this software without specific prior written permission.
31
//
32
// This software is provided by the copyright holders and contributors "as is" and
33
// any express or implied warranties, including, but not limited to, the implied
34
// warranties of merchantability and fitness for a particular purpose are disclaimed.
35
// In no event shall the Intel Corporation or contributors be liable for any direct,
36
// indirect, incidental, special, exemplary, or consequential damages
37
// (including, but not limited to, procurement of substitute goods or services;
38
// loss of use, data, or profits; or business interruption) however caused
39
// and on any theory of liability, whether in contract, strict liability,
40
// or tort (including negligence or otherwise) arising in any way out of
41
// the use of this software, even if advised of the possibility of such damage.
42
//
43
//M*/
44
45
#if CN==1
46
47
#define T_MEAN float
48
#define F_ZERO (0.0f)
49
50
#define frameToMean(a, b) (b) = *(a);
51
#define meanToFrame(a, b) *b = convert_uchar_sat(a);
52
53
#else
54
55
#define T_MEAN float4
56
#define F_ZERO (0.0f, 0.0f, 0.0f, 0.0f)
57
58
#define meanToFrame(a, b)\
59
b[0] = convert_uchar_sat(a.x); \
60
b[1] = convert_uchar_sat(a.y); \
61
b[2] = convert_uchar_sat(a.z);
62
63
#define frameToMean(a, b)\
64
b.x = a[0]; \
65
b.y = a[1]; \
66
b.z = a[2]; \
67
b.w = 0.0f;
68
69
#endif
70
71
__kernel void knn_kernel(__global const uchar* frame, int frame_step, int frame_offset, int frame_row, int frame_col,
72
__global const uchar* nNextLongUpdate,
73
__global const uchar* nNextMidUpdate,
74
__global const uchar* nNextShortUpdate,
75
__global uchar* aModelIndexLong,
76
__global uchar* aModelIndexMid,
77
__global uchar* aModelIndexShort,
78
__global uchar* flag,
79
__global uchar* sample,
80
__global uchar* fgmask, int fgmask_step, int fgmask_offset,
81
int nLongCounter, int nMidCounter, int nShortCounter,
82
float c_Tb, int c_nkNN, float c_tau
83
#ifdef SHADOW_DETECT
84
, uchar c_shadowVal
85
#endif
86
)
87
{
88
int x = get_global_id(0);
89
int y = get_global_id(1);
90
91
if( x < frame_col && y < frame_row)
92
{
93
__global const uchar* _frame = (frame + mad24(y, frame_step, mad24(x, CN, frame_offset)));
94
T_MEAN pix;
95
frameToMean(_frame, pix);
96
97
uchar foreground = 255; // 0 - the pixel classified as background
98
99
int Pbf = 0;
100
int Pb = 0;
101
uchar include = 0;
102
103
int pt_idx = mad24(y, frame_col, x);
104
int idx_step = frame_row * frame_col;
105
106
__global T_MEAN* _sample = (__global T_MEAN*)(sample);
107
108
for (uchar n = 0; n < (NSAMPLES) * 3 ; ++n)
109
{
110
int n_idx = mad24(n, idx_step, pt_idx);
111
112
T_MEAN c_mean = _sample[n_idx];
113
114
uchar c_flag = flag[n_idx];
115
116
T_MEAN diff = c_mean - pix;
117
float dist2 = dot(diff, diff);
118
119
if (dist2 < c_Tb)
120
{
121
Pbf++;
122
if (c_flag)
123
{
124
Pb++;
125
if (Pb >= c_nkNN)
126
{
127
include = 1;
128
foreground = 0;
129
break;
130
}
131
}
132
}
133
}
134
if (Pbf >= c_nkNN)
135
{
136
include = 1;
137
}
138
139
#ifdef SHADOW_DETECT
140
if (foreground)
141
{
142
int Ps = 0;
143
for (uchar n = 0; n < (NSAMPLES) * 3 ; ++n)
144
{
145
int n_idx = mad24(n, idx_step, pt_idx);
146
uchar c_flag = flag[n_idx];
147
148
if (c_flag)
149
{
150
T_MEAN c_mean = _sample[n_idx];
151
152
float numerator = dot(pix, c_mean);
153
float denominator = dot(c_mean, c_mean);
154
155
if (denominator == 0)
156
break;
157
158
if (numerator <= denominator && numerator >= c_tau * denominator)
159
{
160
float a = numerator / denominator;
161
162
T_MEAN dD = mad(a, c_mean, -pix);
163
164
if (dot(dD, dD) < c_Tb * a * a)
165
{
166
Ps++;
167
if (Ps >= c_nkNN)
168
{
169
foreground = c_shadowVal;
170
break;
171
}
172
}
173
}
174
}
175
}
176
}
177
#endif
178
__global uchar* _fgmask = fgmask + mad24(y, fgmask_step, x + fgmask_offset);
179
*_fgmask = (uchar)foreground;
180
181
__global const uchar* _nNextLongUpdate = nNextLongUpdate + pt_idx;
182
__global const uchar* _nNextMidUpdate = nNextMidUpdate + pt_idx;
183
__global const uchar* _nNextShortUpdate = nNextShortUpdate + pt_idx;
184
__global uchar* _aModelIndexLong = aModelIndexLong + pt_idx;
185
__global uchar* _aModelIndexMid = aModelIndexMid + pt_idx;
186
__global uchar* _aModelIndexShort = aModelIndexShort + pt_idx;
187
188
uchar nextLongUpdate = _nNextLongUpdate[0];
189
uchar nextMidUpdate = _nNextMidUpdate[0];
190
uchar nextShortUpdate = _nNextShortUpdate[0];
191
uchar modelIndexLong = _aModelIndexLong[0];
192
uchar modelIndexMid = _aModelIndexMid[0];
193
uchar modelIndexShort = _aModelIndexShort[0];
194
int offsetLong = mad24(mad24(2, (NSAMPLES), modelIndexLong), idx_step, pt_idx);
195
int offsetMid = mad24((NSAMPLES)+modelIndexMid, idx_step, pt_idx);
196
int offsetShort = mad24(modelIndexShort, idx_step, pt_idx);
197
if (nextLongUpdate == nLongCounter)
198
{
199
_sample[offsetLong] = _sample[offsetMid];
200
flag[offsetLong] = flag[offsetMid];
201
_aModelIndexLong[0] = (modelIndexLong >= ((NSAMPLES)-1)) ? 0 : (modelIndexLong + 1);
202
}
203
204
if (nextMidUpdate == nMidCounter)
205
{
206
_sample[offsetMid] = _sample[offsetShort];
207
flag[offsetMid] = flag[offsetShort];
208
_aModelIndexMid[0] = (modelIndexMid >= ((NSAMPLES)-1)) ? 0 : (modelIndexMid + 1);
209
}
210
211
if (nextShortUpdate == nShortCounter)
212
{
213
_sample[offsetShort] = pix;
214
flag[offsetShort] = include;
215
_aModelIndexShort[0] = (modelIndexShort >= ((NSAMPLES)-1)) ? 0 : (modelIndexShort + 1);
216
}
217
}
218
}
219
220
__kernel void getBackgroundImage2_kernel(__global const uchar* flag,
221
__global const uchar* sample,
222
__global uchar* dst, int dst_step, int dst_offset, int dst_row, int dst_col)
223
{
224
int x = get_global_id(0);
225
int y = get_global_id(1);
226
227
if(x < dst_col && y < dst_row)
228
{
229
int pt_idx = mad24(y, dst_col, x);
230
231
T_MEAN meanVal = (T_MEAN)F_ZERO;
232
233
__global T_MEAN* _sample = (__global T_MEAN*)(sample);
234
int idx_step = dst_row * dst_col;
235
for (uchar n = 0; n < (NSAMPLES) * 3 ; ++n)
236
{
237
int n_idx = mad24(n, idx_step, pt_idx);
238
uchar c_flag = flag[n_idx];
239
if(c_flag)
240
{
241
meanVal = _sample[n_idx];
242
break;
243
}
244
}
245
__global uchar* _dst = dst + mad24(y, dst_step, mad24(x, CN, dst_offset));
246
meanToFrame(meanVal, _dst);
247
}
248
}
249
250