forked from kyamagu/mexopencv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackgroundSubtractorKNN.m
276 lines (257 loc) · 10.6 KB
/
BackgroundSubtractorKNN.m
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
classdef BackgroundSubtractorKNN < handle
%BACKGROUNDSUBTRACTORKNN K-nearest neighbours based Background/Foreground Segmentation Algorithm
%
% The class implements the K-nearest neighbours background subtraction
% described in [Zivkovic2006]. Very efficient if number of foreground
% pixels is low.
%
% ## References
% [Zivkovic2006]:
% > Zoran Zivkovic and Ferdinand van der Heijden. "Efficient adaptive
% > density estimation per image pixel for the task of background
% > subtraction". Pattern recognition letters, 27(7):773-780, 2006.
% > [PDF](http://www.zoranz.net/Publications/zivkovicPRL2006.pdf).
%
% [Prati03detectingmoving]:
% > Andrea Prati, Ivana Mikic, Mohan M. Trivedi, Rita Cucchiara.
% > "Detecting Moving Shadows: Algorithms and Evaluation", IEEE PAMI, 2003.
%
% See also: cv.BackgroundSubtractorKNN.BackgroundSubtractorKNN,
% cv.BackgroundSubtractorKNN.apply,
% cv.BackgroundSubtractorKNN.getBackgroundImage
%
properties (SetAccess = private)
% Object ID
id
end
properties (Dependent)
% The number of last frames that affect the background model.
History
% The number of data samples in the background model.
% The model needs to be reinitalized to reserve memory.
NSamples
% The number of neighbours, the k in the kNN.
% K is the number of samples that need to be within `Dist2Threshold`
% in order to decide that that pixel is matching the kNN background
% model.
KNNSamples
% The threshold on the squared distance between the pixel and the
% sample to decide whether a pixel is close to a data sample.
Dist2Threshold
% The shadow detection flag.
% If true, the algorithm detects shadows and marks them. See
% cv.BackgroundSubtractorKNN.BackgroundSubtractorKNN for details.
DetectShadows
% The shadow value.
% Shadow value is the value used to mark shadows in the foreground
% mask. Default value is 127. Value 0 in the mask always means
% background, 255 means foreground.
ShadowValue
% The shadow threshold.
% A shadow is detected if pixel is a darker version of the background.
% The shadow threshold (Tau in the paper) is a threshold defining how
% much darker the shadow can be. Tau=0.5 means that if a pixel is more
% than twice darker then it is not shadow.
% See [Prati03detectingmoving].
ShadowThreshold
end
%% BackgroundSubtractor
methods
function this = BackgroundSubtractorKNN(varargin)
%BACKGROUNDSUBTRACTORKNN Creates KNN Background Subtractor
%
% bs = cv.BackgroundSubtractorKNN()
% bs = cv.BackgroundSubtractorKNN('OptionName', optionValue, ...)
%
% ## Options
% * __History__ Length of the history. default 500
% * __Dist2Threshold__ Threshold on the squared distance between
% the pixel and the sample to decide whether a pixel is close to
% that sample. This parameter does not affect the background
% update. default 400.0
% * __DetectShadows__ If true, the algorithm will detect shadows
% and mark them. It decreases the speed a bit, so if you do not
% need this feature, set the parameter to false. default true
%
% See also: cv.BackgroundSubtractorKNN
%
this.id = BackgroundSubtractorKNN_(0, 'new', varargin{:});
end
function delete(this)
%DELETE Destructor
%
% bs.delete()
%
% See also: cv.BackgroundSubtractorKNN
%
if isempty(this.id), return; end
BackgroundSubtractorKNN_(this.id, 'delete');
end
function fgmask = apply(this, im, varargin)
%APPLY Updates the background model and computes the foreground mask
%
% fgmask = bs.apply(im)
% fgmask = bs.apply(im, 'OptionName', optionValue, ...)
%
% ## Input
% * __im__ Next video frame.
%
% ## Output
% * __fgmask__ The output foreground mask as an 8-bit binary image
% (0 for background, 255 for foregound, and `ShadowValue` for
% shadows if `DetectShadows` is true).
%
% ## Options
% * __LearningRate__ The value between 0 and 1 that indicates how
% fast the background model is learnt. Negative parameter value
% makes the algorithm to use some automatically chosen learning
% rate. 0 means that the background model is not updated at all,
% 1 means that the background model is completely reinitialized
% from the last frame. default -1
%
% See also: cv.BackgroundSubtractorKNN.getBackgroundImage
%
fgmask = BackgroundSubtractorKNN_(this.id, 'apply', im, varargin{:});
end
function bgImg = getBackgroundImage(this)
%GETBACKGROUNDIMAGE Computes a background image
%
% bgImg = bs.getBackgroundImage()
%
% ## Output
% * __bgImg__ The output background image, which is the mean of
% all background Gaussians.
%
% ### Note
% Sometimes the background image can be very blurry, as it contain
% the average background statistics.
%
% See also: cv.BackgroundSubtractorKNN.apply
%
bgImg = BackgroundSubtractorKNN_(this.id, 'getBackgroundImage');
end
end
%% Algorithm
methods (Hidden)
function clear(this)
%CLEAR Clears the algorithm state
%
% obj.clear()
%
% See also: cv.BackgroundSubtractorKNN.empty
%
BackgroundSubtractorKNN_(this.id, 'clear');
end
function b = empty(this)
%EMPTY Returns true if the algorithm is empty
%
% b = obj.empty()
%
% ## Output
% * __b__ Returns true if the algorithm is empty (e.g. in the very
% beginning or after unsuccessful read).
%
% See also: cv.BackgroundSubtractorKNN.clear
%
b = BackgroundSubtractorKNN_(this.id, 'empty');
end
function name = getDefaultName(this)
%GETDEFAULTNAME Returns the algorithm string identifier
%
% name = obj.getDefaultName()
%
% ## Output
% * __name__ This string is used as top level XML/YML node tag
% when the object is saved to a file or string.
%
% See also: cv.BackgroundSubtractorKNN.save, cv.BackgroundSubtractorKNN.load
%
name = BackgroundSubtractorKNN_(this.id, 'getDefaultName');
end
function save(this, filename)
%SAVE Saves the algorithm to a file
%
% obj.save(filename)
%
% ## Input
% * __filename__ Name of the file to save to.
%
% This method stores the algorithm parameters in a file storage.
%
% See also: cv.BackgroundSubtractorKNN.load
%
BackgroundSubtractorKNN_(this.id, 'save', filename);
end
function load(this, fname_or_str, varargin)
%LOAD Loads algorithm from a file or a string
%
% obj.load(fname)
% obj.load(str, 'FromString',true)
% obj.load(..., 'OptionName',optionValue, ...)
%
% ## Input
% * __fname__ Name of the file to read.
% * __str__ String containing the serialized model you want to
% load.
%
% ## Options
% * __ObjName__ The optional name of the node to read (if empty,
% the first top-level node will be used). default empty
% * __FromString__ Logical flag to indicate whether the input is a
% filename or a string containing the serialized model.
% default false
%
% This method reads algorithm parameters from a file storage.
% The previous model state is discarded.
%
% See also: cv.BackgroundSubtractorKNN.save
%
BackgroundSubtractorKNN_(this.id, 'load', fname_or_str, varargin{:});
end
end
%% Getters/Setters
methods
function value = get.History(this)
value = BackgroundSubtractorKNN_(this.id, 'get', 'History');
end
function set.History(this, value)
BackgroundSubtractorKNN_(this.id, 'set', 'History', value);
end
function value = get.NSamples(this)
value = BackgroundSubtractorKNN_(this.id, 'get', 'NSamples');
end
function set.NSamples(this, value)
BackgroundSubtractorKNN_(this.id, 'set', 'NSamples', value);
end
function value = get.KNNSamples(this)
value = BackgroundSubtractorKNN_(this.id, 'get', 'KNNSamples');
end
function set.KNNSamples(this, value)
BackgroundSubtractorKNN_(this.id, 'set', 'KNNSamples', value);
end
function value = get.Dist2Threshold(this)
value = BackgroundSubtractorKNN_(this.id, 'get', 'Dist2Threshold');
end
function set.Dist2Threshold(this, value)
BackgroundSubtractorKNN_(this.id, 'set', 'Dist2Threshold', value);
end
function value = get.DetectShadows(this)
value = BackgroundSubtractorKNN_(this.id, 'get', 'DetectShadows');
end
function set.DetectShadows(this, value)
BackgroundSubtractorKNN_(this.id, 'set', 'DetectShadows', value);
end
function value = get.ShadowValue(this)
value = BackgroundSubtractorKNN_(this.id, 'get', 'ShadowValue');
end
function set.ShadowValue(this, value)
BackgroundSubtractorKNN_(this.id, 'set', 'ShadowValue', value);
end
function value = get.ShadowThreshold(this)
value = BackgroundSubtractorKNN_(this.id, 'get', 'ShadowThreshold');
end
function set.ShadowThreshold(this, value)
BackgroundSubtractorKNN_(this.id, 'set', 'ShadowThreshold', value);
end
end
end