forked from sagi-z/BackgroundSubtractorCNT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
171 lines (157 loc) · 3.92 KB
/
main.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
#include <opencv2/opencv.hpp>
#include <iostream>
#ifdef HAVE_OPENCV_CONTRIB
#include <opencv2/bgsegm.hpp>
using namespace cv::bgsegm;
#endif
#include "bgsubcnt.h"
using namespace cv;
using namespace std;
const string keys =
"{help h usage ? || print this message}"
"{file || use file (default is system camera)}"
"{type |CNT| bg subtraction type from - CNT/MOG2/KNN"
#ifdef HAVE_OPENCV_CONTRIB
"/GMG/MOG"
#endif
"}"
"{bg || calculate also the background}"
"{nogui || run without GUI to measure times}";
int main( int argc, char** argv )
{
VideoCapture cap;
CommandLineParser parser(argc, argv, keys);
parser.about("cv::bgsubcnt::BackgroundSubtractorCNT demo/benchmark/comparison");
if (parser.has("help"))
{
parser.printMessage();
return 0;
}
bool hasFile = parser.has("file");
bool hasGui = ! parser.has("nogui");
bool bgImage = parser.has("bg");
string type = parser.get<string>("type");
string filePath;
if (hasFile)
{
filePath = parser.get<string>("file");
if (filePath == "true")
{
cout << "You must supply a file path argument with -file=filePath\n";
return 1;
}
cap.open(filePath);
}
else
{
cap.open(0);
}
if (! parser.check())
{
parser.printErrors();
return 1;
}
if( !cap.isOpened() )
{
cout << "Could not initialize capturing...\n";
return 0;
}
Ptr<BackgroundSubtractor> pBgSub;
if (type == "CNT")
{
int fps = 15;
if (hasFile)
{
fps = int(cap.get(CAP_PROP_FPS));
}
pBgSub = cv::bgsubcnt::createBackgroundSubtractorCNT(fps, true, fps*60);
}
else if (type == "MOG2")
{
Ptr<BackgroundSubtractorMOG2> pBgSubMOG2 = createBackgroundSubtractorMOG2();
pBgSubMOG2->setDetectShadows(false);
pBgSub = pBgSubMOG2;
}
else if (type == "KNN")
{
pBgSub = createBackgroundSubtractorKNN();
}
#ifdef HAVE_OPENCV_CONTRIB
else if (type == "GMG")
{
pBgSub = createBackgroundSubtractorGMG();
}
else if (type == "MOG")
{
pBgSub = createBackgroundSubtractorMOG();
}
#endif
else
{
parser.printMessage();
cout << "\nWrong type - please see above\n";
return 1;
}
bool showFG=true;
if (hasGui)
{
namedWindow("Orig", 1);
namedWindow("FG", 1);
if (bgImage)
{
namedWindow("BG", 1);
}
cout << "Press 's' to save a frame to the current directory.\n"
"Use ESC to quit.\n" << endl;
}
int64 startTime = getTickCount();
for(;;)
{
Mat frame, fgMask, fg, bg;
cap >> frame;
if( frame.empty() )
{
break;
}
Mat gray;
cvtColor(frame, gray, COLOR_BGR2GRAY);
pBgSub->apply(gray, fgMask);
if (hasGui)
{
imshow("Orig", frame);
if (showFG)
{
frame.copyTo(fg, fgMask);
imshow("FG", fg);
}
}
if (bgImage)
{
pBgSub->getBackgroundImage(bg);
if (hasGui)
{
imshow("BG", bg);
}
}
if (hasGui)
{
char c = (char)waitKey(1);
if (c == 27)
{
break;
}
else if (c == 's')
{
imwrite("frame.jpg", frame);
imwrite("fg.jpg", fg);
if (bgImage)
{
imwrite("bg.jpg", bg);
}
}
}
}
double tfreq = getTickFrequency();
double secs = ((double) getTickCount() - startTime)/tfreq;
cout << "Execution took " << fixed << secs << " seconds." << endl;
}