forked from Smorodov/Multitarget-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackgroundSubtract.cpp
More file actions
308 lines (277 loc) · 9.51 KB
/
BackgroundSubtract.cpp
File metadata and controls
308 lines (277 loc) · 9.51 KB
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
#include "BackgroundSubtract.h"
#include <tuple>
//----------------------------------------------------------------------
//
//----------------------------------------------------------------------
BackgroundSubtract::BackgroundSubtract(BGFG_ALGS algType, int channels)
:
m_channels(channels),
m_algType(algType)
{
config_t config;
Init(config);
}
//----------------------------------------------------------------------
//
//----------------------------------------------------------------------
bool BackgroundSubtract::Init(const config_t& config)
{
bool failed = true;
for (; failed;)
{
failed = false;
switch (m_algType)
{
case ALG_VIBE:
{
std::array<int, 5> params = { 20, 1, 20, 3, 16 };
std::array<std::string, params.size()> paramsConf = { "samples", "pixelNeighbor", "distanceThreshold", "matchingThreshold", "updateFactor" };
for (size_t i = 0; i < paramsConf.size(); ++i)
{
auto conf = config.find(paramsConf[i]);
if (conf != config.end())
{
params[i] = std::stoi(conf->second);
}
}
m_modelVibe = std::make_unique<vibe::VIBE>(m_channels, params[0], params[1], params[2], params[3], params[4]);
break;
}
#ifdef USE_OCV_BGFG
case ALG_MOG:
{
auto params = std::make_tuple(100, 3, 0.7, 0);
std::array<std::string, std::tuple_size<decltype(params)>::value> paramsConf = { "history", "nmixtures", "backgroundRatio", "noiseSigma" };
for (size_t i = 0; i < paramsConf.size(); ++i)
{
auto conf = config.find(paramsConf[i]);
if (conf != config.end())
{
std::stringstream ss(conf->second);
switch (i)
{
case 0:
ss >> std::get<0>(params);
break;
case 1:
ss >> std::get<1>(params);
break;
case 2:
ss >> std::get<2>(params);
break;
case 3:
ss >> std::get<3>(params);
break;
}
}
}
m_modelOCV = cv::bgsegm::createBackgroundSubtractorMOG(std::get<0>(params), std::get<1>(params), std::get<2>(params), std::get<3>(params));
break;
}
case ALG_GMG:
{
auto params = std::make_tuple(50, 0.7);
std::array<std::string, std::tuple_size<decltype(params)>::value> paramsConf = { "initializationFrames", "decisionThreshold" };
for (size_t i = 0; i < paramsConf.size(); ++i)
{
auto conf = config.find(paramsConf[i]);
if (conf != config.end())
{
std::stringstream ss(conf->second);
switch (i)
{
case 0:
ss >> std::get<0>(params);
break;
case 1:
ss >> std::get<1>(params);
break;
}
}
}
m_modelOCV = cv::bgsegm::createBackgroundSubtractorGMG(std::get<0>(params), std::get<1>(params));
break;
}
case ALG_CNT:
{
#if (((CV_VERSION_MAJOR == 3) && (CV_VERSION_MINOR >= 2)) || (CV_VERSION_MAJOR > 3))
auto params = std::make_tuple(15, 1, 15 * 60, 1);
std::array<std::string, std::tuple_size<decltype(params)>::value> paramsConf = { "minPixelStability", "useHistory", "maxPixelStability", "isParallel" };
for (size_t i = 0; i < paramsConf.size(); ++i)
{
auto conf = config.find(paramsConf[i]);
if (conf != config.end())
{
std::stringstream ss(conf->second);
switch (i)
{
case 0:
ss >> std::get<0>(params);
break;
case 1:
ss >> std::get<1>(params);
break;
case 2:
ss >> std::get<2>(params);
break;
case 3:
ss >> std::get<3>(params);
break;
}
}
}
m_modelOCV = cv::bgsegm::createBackgroundSubtractorCNT(std::get<0>(params), std::get<1>(params) != 0, std::get<2>(params), std::get<3>(params) != 0);
#else
std::cerr << "OpenCV CNT algorithm is not implemented! Used Vibe by default." << std::endl;
failed = true;
#endif
break;
}
#else
case ALG_MOG:
case ALG_GMG:
case ALG_CNT:
std::cerr << "OpenCV bgfg algorithms are not implemented! Used Vibe by default." << std::endl;
failed = true;
break;
#endif
case ALG_SuBSENSE:
m_modelSuBSENSE = std::make_unique<BackgroundSubtractorSuBSENSE>(); // default params
break;
case ALG_LOBSTER:
m_modelSuBSENSE = std::make_unique<BackgroundSubtractorLOBSTER>(); // default params
break;
case ALG_MOG2:
{
auto params = std::make_tuple(500, 16, 1);
std::array<std::string, std::tuple_size<decltype(params)>::value> paramsConf = { "history", "varThreshold", "detectShadows" };
for (size_t i = 0; i < paramsConf.size(); ++i)
{
auto conf = config.find(paramsConf[i]);
if (conf != config.end())
{
std::stringstream ss(conf->second);
switch (i)
{
case 0:
ss >> std::get<0>(params);
break;
case 1:
ss >> std::get<1>(params);
break;
case 2:
ss >> std::get<2>(params);
break;
}
}
}
m_modelOCV = cv::createBackgroundSubtractorMOG2(std::get<0>(params), std::get<1>(params), std::get<2>(params) != 0).dynamicCast<cv::BackgroundSubtractor>();
break;
}
default:
{
m_algType = ALG_VIBE;
failed = false;
break;
}
}
if (failed)
{
m_algType = ALG_VIBE;
failed = false;
}
}
return !failed;
}
//----------------------------------------------------------------------
//
//----------------------------------------------------------------------
cv::UMat BackgroundSubtract::GetImg(const cv::UMat& image)
{
if (image.channels() != m_channels)
{
if (image.channels() == 1)
{
cv::UMat newImg;
#if (CV_VERSION_MAJOR < 4)
cv::cvtColor(image, newImg, CV_GRAY2BGR);
#else
cv::cvtColor(image, newImg, cv::COLOR_GRAY2BGR);
#endif
return newImg;
}
else if (image.channels() == 3)
{
cv::UMat newImg;
#if (CV_VERSION_MAJOR < 4)
cv::cvtColor(image, newImg, CV_BGR2GRAY);
#else
cv::cvtColor(image, newImg, cv::COLOR_BGR2GRAY);
#endif
return newImg;
}
}
return image;
}
//----------------------------------------------------------------------
//
//----------------------------------------------------------------------
void BackgroundSubtract::Subtract(const cv::UMat& image, cv::UMat& foreground)
{
switch (m_algType)
{
case ALG_VIBE:
m_modelVibe->update(GetImg(image).getMat(cv::ACCESS_READ));
m_rawForeground = m_modelVibe->getMask().getUMat(cv::ACCESS_READ);
break;
case ALG_MOG:
case ALG_GMG:
case ALG_CNT:
#ifdef USE_OCV_BGFG
m_modelOCV->apply(GetImg(image), m_rawForeground);
break;
#else
std::cerr << "OpenCV bgfg algorithms are not implemented!" << std::endl;
break;
#endif
case ALG_SuBSENSE:
case ALG_LOBSTER:
if (m_rawForeground.size() != image.size() || m_rawForeground.type() != CV_8UC1)
{
m_modelSuBSENSE->initialize(GetImg(image).getMat(cv::ACCESS_READ), cv::Mat());
m_rawForeground.create(image.size(), CV_8UC1);
}
else
{
m_modelSuBSENSE->apply(GetImg(image), m_rawForeground);
}
break;
case ALG_MOG2:
m_modelOCV->apply(GetImg(image), m_rawForeground);
cv::threshold(m_rawForeground, m_rawForeground, 200, 255, cv::THRESH_BINARY);
break;
default:
m_modelVibe->update(GetImg(image).getMat(cv::ACCESS_READ));
m_rawForeground = m_modelVibe->getMask().getUMat(cv::ACCESS_READ);
break;
}
#ifndef SILENT_WORK
//cv::imshow("before", foreground);
#endif
cv::medianBlur(m_rawForeground, foreground, 3);
//cv::Mat dilateElement = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3), cv::Point(-1, -1));
//cv::dilate(foreground, foreground, dilateElement, cv::Point(-1, -1), 2);
#ifndef SILENT_WORK
//cv::imshow("after", foreground);
#endif
}
//----------------------------------------------------------------------
//
//----------------------------------------------------------------------
void BackgroundSubtract::ResetModel(const cv::UMat& img, const cv::Rect& roiRect)
{
if (m_algType == ALG_VIBE)
{
m_modelVibe->ResetModel(GetImg(img).getMat(cv::ACCESS_READ), roiRect);
}
}