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
71 lines (64 loc) · 1.55 KB
/
BackgroundSubtract.cpp
File metadata and controls
71 lines (64 loc) · 1.55 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
#include "BackgroundSubtract.h"
BackgroundSubtract::BackgroundSubtract(
int channels,
int samples,
int pixel_neighbor,
int distance_threshold,
int matching_threshold,
int update_factor
)
{
m_model = std::make_unique<vibe::VIBE>(channels, samples, pixel_neighbor, distance_threshold, matching_threshold, update_factor);
}
BackgroundSubtract::~BackgroundSubtract()
{
}
void BackgroundSubtract::init(const cv::Mat& image)
{
if (image.channels() != m_model->GetChannels())
{
if (image.channels() == 1)
{
cv::Mat newImg;
cv::cvtColor(image, newImg, CV_GRAY2BGR);
m_model->init(newImg);
}
else if (image.channels() == 3)
{
cv::Mat newImg;
cv::cvtColor(image, newImg, CV_BGR2GRAY);
m_model->init(newImg);
}
}
else
{
m_model->init(image);
}
}
void BackgroundSubtract::subtract(const cv::Mat& image, cv::Mat& foreground)
{
cv::Mat erodeElement = cv::getStructuringElement(0, cv::Size(5, 5), cv::Point(-1, -1));
cv::Mat dilateElement = cv::getStructuringElement(0, cv::Size(3, 3), cv::Point(-1, -1));
if (image.channels() != m_model->GetChannels())
{
if (image.channels() == 1)
{
cv::Mat newImg;
cv::cvtColor(image, newImg, CV_GRAY2BGR);
m_model->update(newImg);
}
else if (image.channels() == 3)
{
cv::Mat newImg;
cv::cvtColor(image, newImg, CV_BGR2GRAY);
m_model->update(newImg);
}
}
else
{
m_model->update(image);
}
foreground = m_model->getMask();
cv::erode(foreground, foreground, erodeElement, cv::Point(-1, -1), 1);
cv::dilate(foreground, foreground, dilateElement, cv::Point(-1, -1), 2);
}