Skip to content

Commit b32180b

Browse files
committed
Merge pull request #2255 from sturkmen72:patch-4
2 parents 260d385 + bf688f0 commit b32180b

File tree

1 file changed

+60
-37
lines changed

1 file changed

+60
-37
lines changed

modules/bgsegm/samples/bgfg.cpp

Lines changed: 60 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ 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-
"\nPress q or ESC to exit\n";
13+
"\n\nPress 'c' to change the algorithm"
14+
"\nPress 'm' to toggle showing only foreground mask or ghost effect"
15+
"\nPress 'n' to change number of threads"
16+
"\nPress SPACE to toggle wait delay of imshow"
17+
"\nPress 'q' or ESC to exit\n";
1418

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-
;
19+
const String algos[7] = { "GMG", "CNT", "KNN", "MOG", "MOG2", "GSOC", "LSBP" };
2020

2121
static Ptr<BackgroundSubtractor> createBGSubtractorByName(const String& algoName)
2222
{
@@ -31,42 +31,26 @@ static Ptr<BackgroundSubtractor> createBGSubtractorByName(const String& algoName
3131
algo = createBackgroundSubtractorMOG();
3232
else if(algoName == String("MOG2"))
3333
algo = createBackgroundSubtractorMOG2();
34+
else if(algoName == String("GSOC"))
35+
algo = createBackgroundSubtractorGSOC();
36+
else if(algoName == String("LSBP"))
37+
algo = createBackgroundSubtractorLSBP();
3438

3539
return algo;
3640
}
3741

3842
int main(int argc, char** argv)
3943
{
40-
CommandLineParser parser(argc, argv, keys);
44+
CommandLineParser parser(argc, argv, "{@video | vtest.avi | path to a video file}");
4145
parser.about(about);
4246
parser.printMessage();
43-
if (parser.has("help"))
44-
{
45-
parser.printMessage();
46-
return 0;
47-
}
4847

49-
String videoPath = parser.get<String>("vid");
50-
String algoName = parser.get<String>("algo");
48+
String videoPath = samples::findFile(parser.get<String>(0),false);
5149

52-
if (!parser.check())
53-
{
54-
parser.printErrors();
55-
return 0;
56-
}
57-
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-
}
50+
Ptr<BackgroundSubtractor> bgfs = createBGSubtractorByName(algos[0]);
6451

6552
VideoCapture cap;
66-
if (argc > 1)
67-
cap.open(videoPath);
68-
else
69-
cap.open(0);
53+
cap.open(videoPath);
7054

7155
if (!cap.isOpened())
7256
{
@@ -76,24 +60,63 @@ int main(int argc, char** argv)
7660

7761
Mat frame, fgmask, segm;
7862

79-
namedWindow("FG Segmentation", WINDOW_NORMAL);
63+
int delay = 30;
64+
int algo_index = 0;
65+
int nthreads = getNumberOfCPUs();
66+
bool show_fgmask = false;
8067

8168
for (;;)
8269
{
8370
cap >> frame;
8471

8572
if (frame.empty())
86-
break;
73+
{
74+
cap.set(CAP_PROP_POS_FRAMES, 0);
75+
cap >> frame;
76+
}
8777

8878
bgfs->apply(frame, fgmask);
8979

90-
frame.convertTo(segm, CV_8U, 0.5);
91-
add(frame, Scalar(100, 100, 0), segm, fgmask);
80+
if (show_fgmask)
81+
segm = fgmask;
82+
else
83+
{
84+
frame.convertTo(segm, CV_8U, 0.5);
85+
add(frame, Scalar(100, 100, 0), segm, fgmask);
86+
}
87+
88+
putText(segm, algos[algo_index], Point(10, 30), FONT_HERSHEY_PLAIN, 2.0, Scalar(255, 0, 255), 2, LINE_AA);
89+
putText(segm, format("%d threads", nthreads), Point(10, 60), FONT_HERSHEY_PLAIN, 2.0, Scalar(255, 0, 255), 2, LINE_AA);
9290

9391
imshow("FG Segmentation", segm);
9492

95-
int c = waitKey(30);
96-
if (c == 'q' || c == 'Q' || (c & 255) == 27)
93+
int c = waitKey(delay);
94+
95+
if (c == ' ')
96+
delay = delay == 30 ? 1 : 30;
97+
98+
if (c == 'c' || c == 'C')
99+
{
100+
algo_index++;
101+
if ( algo_index > 6 )
102+
algo_index = 0;
103+
104+
bgfs = createBGSubtractorByName(algos[algo_index]);
105+
}
106+
107+
if (c == 'n' || c == 'N')
108+
{
109+
nthreads++;
110+
if ( nthreads > 8 )
111+
nthreads = 1;
112+
113+
setNumThreads(nthreads);
114+
}
115+
116+
if (c == 'm' || c == 'M')
117+
show_fgmask = !show_fgmask;
118+
119+
if (c == 'q' || c == 'Q' || c == 27)
97120
break;
98121
}
99122

0 commit comments

Comments
 (0)