Skip to content

Commit c34dba7

Browse files
committed
Update bgfg.cpp
1 parent bb5dadd commit c34dba7

File tree

1 file changed

+47
-36
lines changed

1 file changed

+47
-36
lines changed

modules/bgsegm/samples/bgfg.cpp

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ using namespace cv;
88
using namespace cv::bgsegm;
99

1010
const String about =
11-
"\nA program demonstrating the use and capabilities of different background subtraction algrorithms\n"
11+
"\nA program demonstrating the use and capabilities of different background subtraction algorithms\n"
1212
"Using OpenCV version " + String(CV_VERSION) +
13+
"\n\nPress c to change the algorithm"
14+
"\nPress m to toggle showing only foreground mask or ghost effect"
15+
"\nPress SPACE to toggle wait delay of imshow"
1316
"\nPress q or ESC to exit\n";
1417

15-
const String keys =
16-
"{help h usage ? | | print this message }"
17-
"{vid | | path to a video file }"
18-
"{algo | GMG | name of the algorithm (GMG, CNT, KNN, MOG, MOG2) }"
19-
;
18+
const String algos[7] = { "GMG", "CNT", "KNN", "MOG", "MOG2", "GSOC", "LSBP" };
2019

2120
static Ptr<BackgroundSubtractor> createBGSubtractorByName(const String& algoName)
2221
{
@@ -31,42 +30,26 @@ static Ptr<BackgroundSubtractor> createBGSubtractorByName(const String& algoName
3130
algo = createBackgroundSubtractorMOG();
3231
else if(algoName == String("MOG2"))
3332
algo = createBackgroundSubtractorMOG2();
33+
else if(algoName == String("GSOC"))
34+
algo = createBackgroundSubtractorGSOC();
35+
else if(algoName == String("LSBP"))
36+
algo = createBackgroundSubtractorLSBP();
3437

3538
return algo;
3639
}
3740

3841
int main(int argc, char** argv)
3942
{
40-
CommandLineParser parser(argc, argv, keys);
43+
CommandLineParser parser(argc, argv, "{vid | vtest.avi | path to a video file }");
4144
parser.about(about);
4245
parser.printMessage();
43-
if (parser.has("help"))
44-
{
45-
parser.printMessage();
46-
return 0;
47-
}
4846

49-
String videoPath = parser.get<String>("vid");
50-
String algoName = parser.get<String>("algo");
51-
52-
if (!parser.check())
53-
{
54-
parser.printErrors();
55-
return 0;
56-
}
47+
String videoPath = samples::findFile(parser.get<String>("vid"));
5748

58-
Ptr<BackgroundSubtractor> bgfs = createBGSubtractorByName(algoName);
59-
if (!bgfs)
60-
{
61-
std::cerr << "Failed to create " << algoName << " background subtractor" << std::endl;
62-
return -1;
63-
}
49+
Ptr<BackgroundSubtractor> bgfs = createBGSubtractorByName(algos[0]);
6450

6551
VideoCapture cap;
66-
if (argc > 1)
67-
cap.open(videoPath);
68-
else
69-
cap.open(0);
52+
cap.open(videoPath);
7053

7154
if (!cap.isOpened())
7255
{
@@ -76,24 +59,52 @@ int main(int argc, char** argv)
7659

7760
Mat frame, fgmask, segm;
7861

79-
namedWindow("FG Segmentation", WINDOW_NORMAL);
62+
int delay = 30;
63+
int algo_index = 0;
64+
bool show_fgmask = false;
8065

8166
for (;;)
8267
{
8368
cap >> frame;
8469

8570
if (frame.empty())
86-
break;
71+
{
72+
cap.set(CAP_PROP_POS_FRAMES, 0);
73+
cap >> frame;
74+
}
8775

8876
bgfs->apply(frame, fgmask);
8977

90-
frame.convertTo(segm, CV_8U, 0.5);
91-
add(frame, Scalar(100, 100, 0), segm, fgmask);
78+
if (show_fgmask)
79+
segm = fgmask;
80+
else
81+
{
82+
frame.convertTo(segm, CV_8U, 0.5);
83+
add(frame, Scalar(100, 100, 0), segm, fgmask);
84+
}
85+
86+
putText(segm, algos[algo_index], Point(10, 30), FONT_HERSHEY_PLAIN, 2.0, Scalar(255, 0, 255), 2, LINE_AA);
9287

9388
imshow("FG Segmentation", segm);
9489

95-
int c = waitKey(30);
96-
if (c == 'q' || c == 'Q' || (c & 255) == 27)
90+
int c = waitKey(delay);
91+
92+
if (c == ' ')
93+
delay = delay == 30 ? 1 : 30;
94+
95+
if (c == 'c' || c == 'C')
96+
{
97+
algo_index++;
98+
if ( algo_index > 6 )
99+
algo_index = 0;
100+
101+
bgfs = createBGSubtractorByName(algos[algo_index]);
102+
}
103+
104+
if (c == 'm' || c == 'M')
105+
show_fgmask = !show_fgmask;
106+
107+
if (c == 'q' || c == 'Q' || c == 27)
97108
break;
98109
}
99110

0 commit comments

Comments
 (0)