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
162 lines (141 loc) · 4.21 KB
/
BackgroundSubtract.cpp
File metadata and controls
162 lines (141 loc) · 4.21 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
#include "BackgroundSubtract.h"
//----------------------------------------------------------------------
//
//----------------------------------------------------------------------
BackgroundSubtract::BackgroundSubtract(
BGFG_ALGS algType,
int channels,
int samples,
int pixel_neighbor,
int distance_threshold,
int matching_threshold,
int update_factor
)
:
m_channels(channels),
m_algType(algType)
{
for (bool failed = true; failed;)
{
failed = false;
switch (m_algType)
{
case ALG_VIBE:
m_modelVibe = std::make_unique<vibe::VIBE>(m_channels, samples, pixel_neighbor, distance_threshold, matching_threshold, update_factor);
break;
#if USE_OCV_BGFG
case ALG_MOG:
m_modelOCV = cv::bgsegm::createBackgroundSubtractorMOG(100, 3, 0.7, 0);
break;
case ALG_GMG:
m_modelOCV = cv::bgsegm::createBackgroundSubtractorGMG(50, 0.7);
break;
case ALG_CNT:
#if (((CV_VERSION_MAJOR == 3) && (CV_VERSION_MINOR >= 2)) || (CV_VERSION_MAJOR > 3))
m_modelOCV = cv::bgsegm::createBackgroundSubtractorCNT(15, true, 15 * 60, true);
break;
#else
std::cerr << "OpenCV CNT algorithm is not implemented! Used Vibe by default." << std::endl;
failed = true;
break;
#endif
#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:
m_modelOCV = cv::createBackgroundSubtractorMOG2(500, 16, true).dynamicCast<cv::BackgroundSubtractor>();
break;
default:
m_modelVibe = std::make_unique<vibe::VIBE>(m_channels, samples, pixel_neighbor, distance_threshold, matching_threshold, update_factor);
break;
}
if (failed)
{
m_algType = ALG_VIBE;
failed = false;
}
}
}
//----------------------------------------------------------------------
//
//----------------------------------------------------------------------
BackgroundSubtract::~BackgroundSubtract()
{
}
//----------------------------------------------------------------------
//
//----------------------------------------------------------------------
void BackgroundSubtract::subtract(const cv::Mat& image, cv::Mat& foreground)
{
auto GetImg = [&]() -> cv::Mat
{
if (image.channels() != m_channels)
{
if (image.channels() == 1)
{
cv::Mat newImg;
cv::cvtColor(image, newImg, CV_GRAY2BGR);
return newImg;
}
else if (image.channels() == 3)
{
cv::Mat newImg;
cv::cvtColor(image, newImg, CV_BGR2GRAY);
return newImg;
}
}
return image;
};
switch (m_algType)
{
case ALG_VIBE:
m_modelVibe->update(GetImg());
foreground = m_modelVibe->getMask();
break;
case ALG_MOG:
case ALG_GMG:
case ALG_CNT:
#if USE_OCV_BGFG
m_modelOCV->apply(GetImg(), foreground);
break;
#else
std::cerr << "OpenCV bgfg algorithms are not implemented!" << std::endl;
break;
#endif
case ALG_SuBSENSE:
case ALG_LOBSTER:
if (foreground.size() != image.size())
{
m_modelSuBSENSE->initialize(GetImg(), cv::Mat());
foreground.create(image.size(), CV_8UC1);
}
else
{
m_modelSuBSENSE->apply(GetImg(), foreground);
}
break;
case ALG_MOG2:
m_modelOCV->apply(GetImg(), foreground);
break;
default:
m_modelVibe->update(GetImg());
foreground = m_modelVibe->getMask();
break;
}
//cv::imshow("before", foreground);
cv::medianBlur(foreground, 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);
//cv::imshow("after", foreground);
}