-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackgroundSubtract.cpp
137 lines (113 loc) · 2.93 KB
/
BackgroundSubtract.cpp
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
#include "BackgroundSubtract.h"
CBackgroundSubtract::CBackgroundSubtract()
{
}
void CBackgroundSubtract::runTraining(string file)
{
string file_name;
ifstream in;
in.open("background_subtract.txt"); // open file for collection of path to static files to train
while (!in.eof)
{
getline(in, file_name);
trainStaticImages(file_name); // train this background
time++;
}
in.close();
}
void CBackgroundSubtract::trainStaticImages(string file)
{
Mat img = imread(file);
int rows = img.rows;
int cols = img.cols;
CCodeWord* word;
float rgb[3] = {0,0,0};
int bright = 0;
// allocate the codebook for all pixels of the image only one time
if (time == 1)
{
number = rows*cols;
codeBook = new vector<CCodeWord>[number];
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
rgb[0] = img.at<cv::Vec3b>(i, j)[2];
rgb[1] = img.at<cv::Vec3b>(i, j)[1];
rgb[2] = img.at<cv::Vec3b>(i, j)[0];
// Calculate the brightness of the pixel by R G B: bright = R + G + B
bright = rgb[0] + rgb[1] + rgb[2];
// create a new temp codeword
word = new CCodeWord(rgb,bright,bright,1,time-1,time,time);
// if not find the codeword in the codebook
if (!trainCodeBook(*word, i*cols + j))
{
cout << "Added new codeword!!!!!!!!!!" << endl;
}
delete word;
}
}
}
void CBackgroundSubtract::trainRealTime()
{
}
bool CBackgroundSubtract::searchOnCodeBook(CCodeWord newWord,int idx, int type)
{
for (int i = 0; i < codeBook[idx].size(); i++)
{
if (codeBook[idx].at(i).compareCodeWord(newWord))
{
// update the codeword by some information of this newword
codeBook[idx].at(i).updateCodeWord(newWord);
return true;
}
}
// add new codeword to our codebook of this pixel idx
if(type == 1)
codeBook[idx].push_back(newWord);
return false;
}
Mat CBackgroundSubtract::subtractBackground(Mat img)
{
int rows = img.rows;
int cols = img.cols;
CCodeWord* word;
float rgb[3] = { 0,0,0 };
int bright = 0;
Mat bgk_sub = Mat::zeros(rows,cols,CV_32F);
// we consider test image as a tranining image but just updating the RGB and 6-tuple information, not adding
time++;
// allocate the codebook for all pixels of the image only one time
if (time == 1)
{
number = rows*cols;
codeBook = new vector<CCodeWord>[number];
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
rgb[0] = img.at<cv::Vec3b>(i, j)[2];
rgb[1] = img.at<cv::Vec3b>(i, j)[1];
rgb[2] = img.at<cv::Vec3b>(i, j)[0];
// Calculate the brightness of the pixel by R G B: bright = R + G + B
bright = rgb[0] + rgb[1] + rgb[2];
// create a new temp codeword
word = new CCodeWord(rgb, bright, bright, 1, time - 1, time, time);
// if find the codeword in the codebook
if (searchOnCodeBook(*word, i*cols + j, 2))
{
bgk_sub.at<float>(i, j) = 0;
}
else
{
bgk_sub.at<float>(i, j) = 255;
}
delete word;
}
}
}
CBackgroundSubtract::~CBackgroundSubtract()
{
}