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
107 lines (94 loc) · 2.6 KB
/
BackgroundSubtract.cpp
File metadata and controls
107 lines (94 loc) · 2.6 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
#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)
{
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;
#else
case ALG_MOG:
case ALG_GMG:
std::cerr << "OpenCV bgfg algorithms are not implemented! Used Vibe by default." << std::endl;
#endif
default:
m_modelVibe = std::make_unique<vibe::VIBE>(m_channels, samples, pixel_neighbor, distance_threshold, matching_threshold, update_factor);
break;
}
}
//----------------------------------------------------------------------
//
//----------------------------------------------------------------------
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:
#if USE_OCV_BGFG
m_modelOCV->apply(GetImg(), foreground);
break;
#else
std::cerr << "OpenCV bgfg algorithms are not implemented!" << std::endl;
#endif
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);
}