@@ -8,15 +8,14 @@ using namespace cv;
8
8
using namespace cv ::bgsegm;
9
9
10
10
const String about =
11
- " \n A program demonstrating the use and capabilities of different background subtraction algrorithms \n "
11
+ " \n A program demonstrating the use and capabilities of different background subtraction algorithms \n "
12
12
" Using OpenCV version " + String(CV_VERSION) +
13
+ " \n\n Press c to change the algorithm"
14
+ " \n Press m to toggle showing only foreground mask or ghost effect"
15
+ " \n Press SPACE to toggle wait delay of imshow"
13
16
" \n Press q or ESC to exit\n " ;
14
17
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" };
20
19
21
20
static Ptr<BackgroundSubtractor> createBGSubtractorByName (const String& algoName)
22
21
{
@@ -31,42 +30,26 @@ static Ptr<BackgroundSubtractor> createBGSubtractorByName(const String& algoName
31
30
algo = createBackgroundSubtractorMOG ();
32
31
else if (algoName == String (" MOG2" ))
33
32
algo = createBackgroundSubtractorMOG2 ();
33
+ else if (algoName == String (" GSOC" ))
34
+ algo = createBackgroundSubtractorGSOC ();
35
+ else if (algoName == String (" LSBP" ))
36
+ algo = createBackgroundSubtractorLSBP ();
34
37
35
38
return algo;
36
39
}
37
40
38
41
int main (int argc, char ** argv)
39
42
{
40
- CommandLineParser parser (argc, argv, keys );
43
+ CommandLineParser parser (argc, argv, " {vid | vtest.avi | path to a video file } " );
41
44
parser.about (about);
42
45
parser.printMessage ();
43
- if (parser.has (" help" ))
44
- {
45
- parser.printMessage ();
46
- return 0 ;
47
- }
48
46
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" ));
57
48
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 ]);
64
50
65
51
VideoCapture cap;
66
- if (argc > 1 )
67
- cap.open (videoPath);
68
- else
69
- cap.open (0 );
52
+ cap.open (videoPath);
70
53
71
54
if (!cap.isOpened ())
72
55
{
@@ -76,24 +59,52 @@ int main(int argc, char** argv)
76
59
77
60
Mat frame, fgmask, segm;
78
61
79
- namedWindow (" FG Segmentation" , WINDOW_NORMAL);
62
+ int delay = 30 ;
63
+ int algo_index = 0 ;
64
+ bool show_fgmask = false ;
80
65
81
66
for (;;)
82
67
{
83
68
cap >> frame;
84
69
85
70
if (frame.empty ())
86
- break ;
71
+ {
72
+ cap.set (CAP_PROP_POS_FRAMES, 0 );
73
+ cap >> frame;
74
+ }
87
75
88
76
bgfs->apply (frame, fgmask);
89
77
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);
92
87
93
88
imshow (" FG Segmentation" , segm);
94
89
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 )
97
108
break ;
98
109
}
99
110
0 commit comments