-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCBM_model.cpp
executable file
·407 lines (341 loc) · 13.2 KB
/
CBM_model.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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include "CBM_model.h"
#include <iostream>
//#include <omp.h>
using namespace std;
using namespace cv;
CBM_model::CBM_model(cv::Mat &input, int set_MOG_LearnFrame, int set_min_area, float set_resize,
cv::Mat &mask) {
// id = 0 ;
frame_count = 0;
MOG_LEARN_FRAMES = set_MOG_LearnFrame;
MIN_AREA = set_min_area;
new_width = (int) (input.cols * set_resize);
new_height = (int) (input.rows * set_resize);
Initialize();
// Select parameters for Gaussian model.
_myGMM = new myGMM(0.0001);//0.0001
_myGMM2 = new myGMM(0.002);
maskROI = mask;
}
CBM_model::~CBM_model() {
Uninitialize();
}
void CBM_model::Initialize() {
_writer1.open("long.avi", cv::VideoWriter::fourcc('D', 'I', 'V', 'X'), 30, cv::Size(new_width, new_height), 0);
_writer2.open("short.avi", cv::VideoWriter::fourcc('D', 'I', 'V', 'X'), 30, cv::Size(new_width, new_height), 0);
_writer3.open("static.avi", cv::VideoWriter::fourcc('D', 'I', 'V', 'X'), 30, cv::Size(new_width, new_height), 1);
_writer5.open("DPM.avi", cv::VideoWriter::fourcc('D', 'I', 'V', 'X'), 30, cv::Size(new_width, new_height), 1);
my_mog_fg = cv::Mat(cv::Size(new_width, new_height), CV_8UC1, cv::Scalar::all(0));
my_mog_fg2 = cv::Mat(cv::Size(new_width, new_height), CV_8UC1, cv::Scalar::all(0));
my_imgCandiStatic = cv::Mat(cv::Size(new_width, new_height), CV_8UC3, cv::Scalar::all(0));
my_imgStatic = cv::Mat(cv::Size(new_width, new_height), CV_8UC3, cv::Scalar::all(0));
imageFSM = (pixelFSM **) malloc((int) new_width * sizeof(pixelFSM *));
mog_fg.setTo(0);
mog_fg2.setTo(0);
imgStatic.setTo(0);
input_temp = cv::Mat(cv::Size(new_width, new_height), CV_8UC1, cv::Scalar::all(0));
for (int i = 0; i < new_width; i++) {
imageFSM[i] = (pixelFSM *) malloc((int) new_height * sizeof(pixelFSM));
memset(imageFSM[i], 0, (int) new_height * sizeof(pixelFSM));
}
printf("..\n");
_Previous_Img = cv::Mat(cv::Size(new_width, new_height), CV_8UC3, cv::Scalar::all(0));
printf("....\n");
}
void CBM_model::Uninitialize() {
_writer1.release();
_writer2.release();
_writer3.release();
_writer5.release();
free(*imageFSM);
free(imageFSM);
cout << "CBM_model Released!" << endl;
}
//
void CBM_model::System_Reset() {
#pragma omp parallel for
for (int i = 0; i < new_width; i++) {
for (int j = 0; j < new_height; j++) {
imageFSM[i][j].state_now = 0;
imageFSM[i][j].staticFG_stable = false;
imageFSM[i][j].staticFG_candidate = false;
imageFSM[i][j].static_count = 0;
}
}
static_object_result.clear();
}
//
bool CBM_model::Motion_Detection(cv::Mat &img) {
cv::resize(img, _Previous_Img, cv::Size(img.cols, img.rows));
if (frame_count < MOG_LEARN_FRAMES) {
//printf("update mog %d\n",MOG_LEARN_FRAMES-frame_count);
std::cout << "updata mog " << MOG_LEARN_FRAMES - frame_count << std::endl;
if (frame_count == 0) {
_myGMM->initial(_Previous_Img);
_myGMM2->initial(_Previous_Img);
}
_myGMM->process(_Previous_Img, my_mog_fg);
_myGMM2->process(_Previous_Img, my_mog_fg2);
frame_count++;
mog_fg = my_mog_fg.clone();
mog_fg2 = my_mog_fg2.clone();
// cvWriteFrame( _writer1, mog_fg);
_writer1.write(mog_fg);
// cvWriteFrame( _writer2, mog_fg2);
_writer2.write(mog_fg2);
// cvWriteFrame( _writer3, imgStatic);
_writer3.write(imgStatic);
return false;
} else {
_myGMM->process(_Previous_Img, my_mog_fg, maskROI);
//my_mog_fg = input_temp & maskROI;
_myGMM2->process(_Previous_Img, my_mog_fg2, maskROI);
//my_mog_fg2 = input_temp & maskROI;
cv::Mat structureElement = getStructuringElement(cv::MORPH_RECT, cv::Size(7, 7), Point(-1, -1));
cv::dilate(my_mog_fg, my_mog_fg, structureElement, cv::Point(-1, -1));
cv::dilate(my_mog_fg2, my_mog_fg2, structureElement, cv::Point(-1, -1));
if (check_foreground2(my_mog_fg) >
(my_mog_fg.cols * my_mog_fg.rows * 0.30)) {//if motion detection cannot work well
_myGMM->ChangeLearningRate(0.02);//speed up long-term model's learning rate to adapt the lighting changes.
} else {
_myGMM->ChangeLearningRate(0.0001);//defult long-term model learning rate
}
myFSM(my_mog_fg2, my_mog_fg, imageFSM);
myConvertFSM2Img(imageFSM, my_imgCandiStatic, my_imgStatic);
// staticFG_pixel_num_pre2 = staticFG_pixel_num_pre;
// staticFG_pixel_num_pre = staticFG_pixel_num_now;
// staticFG_pixel_num_now = check_foreground2(my_imgStatic);
int stateCurrent = check_foreground2(my_imgStatic);
stateHistory.push_back(stateCurrent);
if (stateHistory.size() > CONSTTIME) {
stateHistory.pop_front();
}
cv::imshow("my_imgCandiStatic", my_imgCandiStatic);
cv::imshow("static obj", my_imgStatic);
cv::imshow("Long-term", my_mog_fg);
cv::imshow("Short-term", my_mog_fg2);
bool static_object_detected = false;
if (isEqual() && (stateCurrent > 0)) {
static_object_detected = myClustering2(my_imgStatic, 1);
}
_writer1.write(my_mog_fg);
_writer2.write(my_mog_fg2);
_writer3.write(my_imgStatic);
return static_object_detected;
}
}
//
//
bool CBM_model::isEqual() {
bool isEqualDeque = true;
if (stateHistory.size() < CONSTTIME) {
isEqualDeque = false;
return isEqualDeque;
}
int val = stateHistory[0];
for (int i = 0; i < stateHistory.size(); i++) {
if (val != stateHistory[i]) {
isEqualDeque = false;
}
}
return isEqualDeque;
}
bool CBM_model::myClustering2(cv::Mat &img, int option) {
int area_threshold = 0;
cv::Mat temp;
temp = cv::Mat(cv::Size(new_width, new_height), CV_8UC1, cv::Scalar::all(0));
if (img.channels() == 3)//static foreground object
{
cv::cvtColor(img, temp, COLOR_BGR2GRAY);
area_threshold = MIN_AREA / 2;//0;
} else if (img.channels() == 1)//foreground detection
{
img.copyTo(temp);
area_threshold = MIN_AREA;
}
int found_objnum = 0;
found_objnum = GetLabeling2(temp, area_threshold, option);
if (found_objnum > 0) {
return true;
} else {
return false;
}
}
//
///************************************************************************/
///*
//GetLabeling : input a binary frame, bounding the connected component.
//Ignore the connected component when : case1. It's pixel is more than a areaThreshold.
// case2. The bounding rectangle is too thin or fat. */
///************************************************************************/
int CBM_model::GetLabeling2(cv::Mat &pImg1, int areaThreshold, int option) {
// std::cout << "GetLabeling2" << std::endl;
int found_objnum = 0;
if (option == 0) {
detected_result.clear();//clear the vector
}
if (option == 1) {
static_object_result.clear();//clear the vectors
}
cv::threshold(pImg1, pImg1, 0, 255, cv::THRESH_BINARY);
std::vector<std::vector<cv::Point>> contours1;
std::vector<cv::Vec4i> hierachy1;
cv::findContours(my_mog_fg, contours1, hierachy1, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
int areaThreshold_max = 0, areaThreshold_min = 0;
if (option == 0)//for moving foreground
{
areaThreshold_max = MAX_FG;
areaThreshold_min = MIN_FG;
} else if (option == 1) {
areaThreshold_max = MAX_SFG;
areaThreshold_min = MIN_SFG;
}
for (int ii = 0; ii < contours1.size(); ii++) {
cv::Mat imgTemp = cv::Mat(my_mog_fg.rows, my_mog_fg.cols, CV_8UC1, cv::Scalar::all(0));
cv::drawContours(imgTemp, contours1, ii, cv::Scalar::all(255), cv::FILLED);
cv::Mat tt = imgTemp & pImg1;
std::vector<cv::Point> vecP;
cv::findNonZero(tt, vecP);
cv::Rect currentBox = cv::boundingRect(contours1[ii]);
if (vecP.size() > 0) {
if (vecP.size() > areaThreshold_min && vecP.size() < areaThreshold_max) {
Obj_info *element;
element = new Obj_info;
element->x = currentBox.x;
element->y = currentBox.y;
element->width = currentBox.width;
element->height = currentBox.height;
element->contours.push_back(contours1[ii]);
if (option == 0)
detected_result.push_back(element);
if (option == 1)
static_object_result.push_back(element);
}
found_objnum++;
}
}
return found_objnum;
}
//
//
vector<Obj_info *> CBM_model::GetDetectResult() {
return detected_result;
}
vector<Obj_info *> CBM_model::GetStaticForegourdResult() {
return static_object_result;
}
void CBM_model::myFSM(cv::Mat &short_term, cv::Mat &long_term, pixelFSM **imageFSM) {
//short long state
// 0 0 0
// 0 1 1
// 1 0 2
// 1 1 3
myColor buffer[2];
#pragma omp parallel for
for (int i = 0; i < new_width; i++) {
for (int j = 0; j < new_height; j++) {
buffer[0] = myGet2D(short_term, i, j);
buffer[1] = myGet2D(long_term, i, j);
imageFSM[i][j].state_pre = imageFSM[i][j].state_now;
imageFSM[i][j].state_now = 0;
if ((buffer[0].B == 255) && (buffer[0].G == 255) && (buffer[0].R == 255)) {
imageFSM[i][j].state_now += 2;
} else {
imageFSM[i][j].state_now = 0;
}
if ((buffer[1].B == 255) && (buffer[1].G == 255) && (buffer[1].R == 255)) {
imageFSM[i][j].state_now++;
} else {
imageFSM[i][j].state_now = 0;
}
if ((imageFSM[i][j].state_now == 1) && (imageFSM[i][j].state_pre == 1)) {
if (imageFSM[i][j].static_count == (staticTime)) {
imageFSM[i][j].staticFG_stable = true;
}
if (imageFSM[i][j].staticFG_candidate == true) {
imageFSM[i][j].static_count++;
}
} else {
imageFSM[i][j].static_count = 0;
imageFSM[i][j].staticFG_candidate = false;
}
if ((imageFSM[i][j].state_now == 1) && (imageFSM[i][j].state_pre == 3)) {
imageFSM[i][j].staticFG_candidate = true;
}
}
}
}
void CBM_model::myConvertFSM2Img(pixelFSM **Array, cv::Mat &Candidate_Fg, cv::Mat &Static_Fg) {
myColor color1, color2;
color1.B = 0;
color1.G = 0;
color1.R = 255;
color2.B = 0;
color2.G = 200;
color2.R = 255;
#pragma omp parallel for
for (int i = 0; i < new_width; i++) {
for (int j = 0; j < new_height; j++) {
if (Array[i][j].staticFG_candidate == true)
mySet2D(Candidate_Fg, color1, i, j);
else {
myColor a;
a.B = 0;
a.G = 0;
a.R = 0;
mySet2D(Candidate_Fg, a, i, j);
}
if (Array[i][j].staticFG_stable == true)
mySet2D(Static_Fg, color2, i, j);
else {
myColor a;
a.B = 0;
a.G = 0;
a.R = 0;
mySet2D(Static_Fg, a, i, j);
}
}
}
}
int CBM_model::check_foreground2(cv::Mat &img) {
cv::Mat grayImg;
if (img.channels() == 3) {
cv::cvtColor(img, grayImg, cv::COLOR_BGR2GRAY);
} else {
grayImg = img;
}
std::vector<cv::Point> pNoZero;
cv::findNonZero(grayImg, pNoZero);
return pNoZero.size();
}
myColor myGet2D(cv::Mat &input, int x, int y) {
int width = input.cols;
int height = input.rows;
int depth = input.channels();
myColor colors;
if (depth == 1) {
colors.B = input.at<uchar>(y, x);//B
colors.G = colors.B;//G
colors.R = colors.B;//R
} else if (depth == 3) {
colors.B = input.at<cv::Vec3b>(y, x)[0];//B
colors.G = input.at<cv::Vec3b>(y, x)[1];//G
colors.R = input.at<cv::Vec3b>(y, x)[2];//R
}
return colors;
}
/************************************************************************/
/* mySet2D: assign RGB value */
/************************************************************************/
void mySet2D(cv::Mat &input, myColor colors, int x, int y) {
int width = input.cols;
int height = input.rows;
int depth = input.channels();
if (depth == 1) {
input.at<uchar>(y, x) = colors.B;
} else if (depth == 3) {
input.at<cv::Vec3b>(y, x)[0] = colors.B;
input.at<cv::Vec3b>(y, x)[1] = colors.G;
input.at<cv::Vec3b>(y, x)[2] = colors.R;
}
}