@@ -8,15 +8,15 @@ 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 Press q or ESC to exit\n " ;
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 'n' to change number of threads"
16
+ " \n Press SPACE to toggle wait delay of imshow"
17
+ " \n Press 'q' or ESC to exit\n " ;
14
18
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" };
20
20
21
21
static Ptr<BackgroundSubtractor> createBGSubtractorByName (const String& algoName)
22
22
{
@@ -31,42 +31,26 @@ static Ptr<BackgroundSubtractor> createBGSubtractorByName(const String& algoName
31
31
algo = createBackgroundSubtractorMOG ();
32
32
else if (algoName == String (" MOG2" ))
33
33
algo = createBackgroundSubtractorMOG2 ();
34
+ else if (algoName == String (" GSOC" ))
35
+ algo = createBackgroundSubtractorGSOC ();
36
+ else if (algoName == String (" LSBP" ))
37
+ algo = createBackgroundSubtractorLSBP ();
34
38
35
39
return algo;
36
40
}
37
41
38
42
int main (int argc, char ** argv)
39
43
{
40
- CommandLineParser parser (argc, argv, keys );
44
+ CommandLineParser parser (argc, argv, " {@video | vtest.avi | path to a video file} " );
41
45
parser.about (about);
42
46
parser.printMessage ();
43
- if (parser.has (" help" ))
44
- {
45
- parser.printMessage ();
46
- return 0 ;
47
- }
48
47
49
- String videoPath = parser.get <String>(" vid" );
50
- String algoName = parser.get <String>(" algo" );
48
+ String videoPath = samples::findFile (parser.get <String>(0 ),false );
51
49
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 ]);
64
51
65
52
VideoCapture cap;
66
- if (argc > 1 )
67
- cap.open (videoPath);
68
- else
69
- cap.open (0 );
53
+ cap.open (videoPath);
70
54
71
55
if (!cap.isOpened ())
72
56
{
@@ -76,24 +60,63 @@ int main(int argc, char** argv)
76
60
77
61
Mat frame, fgmask, segm;
78
62
79
- namedWindow (" FG Segmentation" , WINDOW_NORMAL);
63
+ int delay = 30 ;
64
+ int algo_index = 0 ;
65
+ int nthreads = getNumberOfCPUs ();
66
+ bool show_fgmask = false ;
80
67
81
68
for (;;)
82
69
{
83
70
cap >> frame;
84
71
85
72
if (frame.empty ())
86
- break ;
73
+ {
74
+ cap.set (CAP_PROP_POS_FRAMES, 0 );
75
+ cap >> frame;
76
+ }
87
77
88
78
bgfs->apply (frame, fgmask);
89
79
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);
92
90
93
91
imshow (" FG Segmentation" , segm);
94
92
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 )
97
120
break ;
98
121
}
99
122
0 commit comments