-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbg_sub.cpp
More file actions
204 lines (166 loc) · 5.8 KB
/
Copy pathbg_sub.cpp
File metadata and controls
204 lines (166 loc) · 5.8 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
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
//opencv
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/video.hpp>
#include <opencv2/core/types_c.h>
//C
#include <stdio.h>
//C++
#include <iostream>
#include <sstream>
using namespace cv;
using namespace std;
// Global variables
Mat frame; //current frame
Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method
Ptr<BackgroundSubtractorMOG2> pMOG2; //MOG2 Background subtractor
int keyboard; //input from keyboard
/** Function Headers */
void help();
void processVideo(char* videoFilename);
void help()
{
cout
<< "--------------------------------------------------------------------------" << endl
<< "Usage:" << endl
<< "BGSubtract.exe -vid <video filename>" << endl
<< "--------------------------------------------------------------------------" << endl
<< endl;
}
void morph(Mat &frame){
Mat erodeMat = getStructuringElement(MORPH_ELLIPSE, Size(4, 4));
Mat dilateMat = getStructuringElement(MORPH_ELLIPSE, Size(8, 8));
erode(frame, frame, erodeMat);
erode(frame, frame, erodeMat);
dilate(frame, frame, dilateMat);
dilate(frame, frame, dilateMat);
}
void MyCallbackForShadowValue(int iValueForShadowValue, void*)
{
pMOG2->setShadowValue(iValueForShadowValue);
}
void MyCallbackForNMixtures(int iValueForNMixtures, void*)
{
pMOG2->setNMixtures(iValueForNMixtures);
}
void MyCallbackForHistory(int iValueForHistory, void*)
{
pMOG2->setHistory(iValueForHistory);
}
void MyCallbackForShadowThreshold(int iValueForShadowThreshold, void*)
{
double iShadowThreshold = (iValueForShadowThreshold / (double)100);
pMOG2->setShadowThreshold(iShadowThreshold);
}
void MyCallbackForVarThreshold(int iValueForVarThreshold, void*)
{
pMOG2->setVarThreshold(iValueForVarThreshold);
}
void MyCallbackForBackgroundRatio(int iValueForBackgroundRatio, void*)
{
double iBackgroundRatio = (iValueForBackgroundRatio / (double)100);
pMOG2->setBackgroundRatio(iBackgroundRatio);
}
void MyCallbackForVarThresholdGen(int iValueForVarThresholdGen, void*)
{
pMOG2->setVarThresholdGen(iValueForVarThresholdGen);
}
void MyCallbackForBlur(int iValueForBlur, void *userdata)
{
if (iValueForBlur <= 0) {
return;
}
Mat img = *((Mat*)userdata);
blur(img, img, Size(iValueForBlur, iValueForBlur)); // Blur the image
imshow("Frame", img);
}
/**
* @function processVideo
*/
void processVideo(char* videoFilename) {
//create the capture object
VideoCapture capture(videoFilename);
if(!capture.isOpened()){
//error in opening the video input
cerr << "Unable to open video file: " << videoFilename << endl;
exit(EXIT_FAILURE);
}
// Default params for trackbar (Same as initial params setup in Main)
int iValueForShadowValue = 0;
int iValueForNMixtures = 3;
int iValueForHistory = 200;
int iValueForShadowThreshold = 10;
int iValueForVarThreshold = 400;
int iValueForBackgroundRatio = 60;
int iValueForVarThresholdGen = 2;
int iValueForBlur = 5;
//read input data. ESC or 'q' for quitting
while( (char)keyboard != 'q' && (char)keyboard != 27 ){
//read the current frame
if(!capture.read(frame)) {
cerr << "Unable to read next frame." << endl;
cerr << "Exiting..." << endl;
exit(EXIT_FAILURE);
}
//cvtColor(frame, frame, CV_BGR2GRAY);// Convert the image to grayscale
createTrackbar("Blur", "Frame", &iValueForBlur, 20, MyCallbackForBlur, (void*)(&frame));
MyCallbackForBlur(iValueForBlur, &frame);
pMOG2->apply(frame, fgMaskMOG2, -.1); // (original, new mat, learing rate)
//Create trackbars to change backgroundsubtractorMOG2 parameters
createTrackbar("ShadowValue", "FG Mask MOG 2", &iValueForShadowValue, 254, MyCallbackForShadowValue);
createTrackbar("NMixtures", "FG Mask MOG 2", &iValueForNMixtures, 7, MyCallbackForNMixtures);
createTrackbar("History", "FG Mask MOG 2", &iValueForHistory, 500, MyCallbackForHistory);
createTrackbar("ShadowThreshold", "FG Mask MOG 2", &iValueForShadowThreshold, 100, MyCallbackForShadowThreshold);
createTrackbar("VarThreshold", "FG Mask MOG 2", &iValueForVarThreshold, 500, MyCallbackForVarThreshold);
createTrackbar("BGRatio", "FG Mask MOG 2", &iValueForBackgroundRatio, 100, MyCallbackForBackgroundRatio);
createTrackbar("ThresholdGen", "FG Mask MOG 2", &iValueForVarThresholdGen, 5, MyCallbackForVarThresholdGen);
imshow("FG Mask MOG 2", fgMaskMOG2);
//medianBlur(fgMaskMOG2, fgMaskMOG2,9); // Too slow
morph(fgMaskMOG2);
imshow("Morph", fgMaskMOG2);
//get the input from the keyboard
keyboard = waitKey( 30 );
}
//delete capture object
capture.release();
}
/** Main */
int main(int argc, char* argv[])
{
//print help information
help();
//check for the input parameter correctness
if (argc != 3) {
cerr << "Incorret input list" << endl;
cerr << "exiting..." << endl;
return EXIT_FAILURE;
}
//create GUI windows
namedWindow("Frame");
namedWindow("FG Mask MOG 2", 1);
namedWindow("Morph");
//create Background Subtractor objects
pMOG2 = createBackgroundSubtractorMOG2(200, 16, true); //MOG2 approach
pMOG2->setDetectShadows(true);
pMOG2->setShadowValue(0); // Sets the showdow color (0-black, 255-white)
pMOG2->setShadowThreshold(.5); //Originally ftau?
pMOG2->setVarThreshold(400); //Double
pMOG2->setNMixtures(3); // Int
pMOG2->setBackgroundRatio(0.6); //Double
pMOG2->setVarThresholdGen(2);
if (strcmp(argv[1], "-vid") == 0) {
//input data coming from a video
processVideo(argv[2]);
}
else {
//error in reading input parameters
cerr << "Please, check the input parameters." << endl;
cerr << "Exiting..." << endl;
return EXIT_FAILURE;
}
//destroy GUI windows
destroyAllWindows();
return EXIT_SUCCESS;
}