In the following you can find the source code. We will let the user chose to process either a video file or a sequence of images.
The results as well as the input data are shown on the screen. The source file can be downloaded here.
30 void processVideo(
char* videoFilename);
31 void processImages(
char* firstFrameFilename);
36 <<
"--------------------------------------------------------------------------" << endl
37 <<
"This program shows how to use background subtraction methods provided by " << endl
38 <<
" OpenCV. You can process both videos (-vid) and images (-img)." << endl
41 <<
"./bs {-vid <video filename>|-img <image filename>}" << endl
42 <<
"for example: ./bs -vid video.avi" << endl
43 <<
"or: ./bs -img /data/images/1.png" << endl
44 <<
"--------------------------------------------------------------------------" << endl
51 int main(
int argc,
char* argv[])
58 cerr <<
"Incorret input list" << endl;
59 cerr <<
"exiting..." << endl;
70 if(strcmp(argv[1],
"-vid") == 0) {
72 processVideo(argv[2]);
74 else if(strcmp(argv[1],
"-img") == 0) {
76 processImages(argv[2]);
80 cerr <<
"Please, check the input parameters." << endl;
81 cerr <<
"Exiting..." << endl;
92 void processVideo(
char* videoFilename) {
97 cerr <<
"Unable to open video file: " << videoFilename << endl;
101 while( (
char)keyboard !=
'q' && (
char)keyboard != 27 ){
103 if(!capture.
read(frame)) {
104 cerr <<
"Unable to read next frame." << endl;
105 cerr <<
"Exiting..." << endl;
109 pMOG2->apply(frame, fgMaskMOG2);
115 string frameNumberString = ss.str();
120 imshow(
"FG Mask MOG 2", fgMaskMOG2);
131 void processImages(
char* fistFrameFilename) {
133 frame =
imread(fistFrameFilename);
136 cerr <<
"Unable to open first image frame: " << fistFrameFilename << endl;
140 string fn(fistFrameFilename);
142 while( (
char)keyboard !=
'q' && (
char)keyboard != 27 ){
144 pMOG2->apply(frame, fgMaskMOG2);
146 size_t index = fn.find_last_of(
"/");
147 if(index == string::npos) {
148 index = fn.find_last_of(
"\\");
150 size_t index2 = fn.find_last_of(
".");
151 string prefix = fn.substr(0,index+1);
152 string suffix = fn.substr(index2);
153 string frameNumberString = fn.substr(index+1, index2-index-1);
154 istringstream iss(frameNumberString);
163 imshow(
"FG Mask MOG 2", fgMaskMOG2);
168 oss << (frameNumber + 1);
169 string nextFrameNumberString = oss.str();
170 string nextFrameFilename = prefix + nextFrameNumberString + suffix;
172 frame =
imread(nextFrameFilename);
175 cerr <<
"Unable to open image frame: " << nextFrameFilename << endl;
179 fn.assign(nextFrameFilename);
bool empty() const
Returns true if the array has no elements.
Scalar_< double > Scalar
Definition: types.hpp:597
virtual void release()
Closes video file or capturing device.
Mat imread(const String &filename, int flags=IMREAD_COLOR)
Loads an image from a file.
virtual bool read(OutputArray image)
Grabs, decodes and returns the next video frame.
void destroyAllWindows()
Destroys all of the HighGUI windows.
void rectangle(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a simple, thick, or filled up-right rectangle.
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
Class for video capturing from video files, image sequences or cameras. The class provides C++ API fo...
Definition: videoio.hpp:429
Template class for 2D points specified by its coordinates x and y.
Definition: types.hpp:147
virtual double get(int propId) const
Returns the specified VideoCapture property.
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
Template class for smart pointers with shared ownership.
Definition: cvstd.hpp:283
normal size sans-serif font
Definition: core.hpp:213
virtual bool isOpened() const
Returns true if video capturing has been initialized already.
void putText(InputOutputArray img, const String &text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=LINE_8, bool bottomLeftOrigin=false)
Draws a text string.
Definition: videoio.hpp:95
int main(int argc, const char *argv[])
Definition: facerec_demo.cpp:67
Point2i Point
Definition: types.hpp:181
int waitKey(int delay=0)
Waits for a pressed key.
Ptr< BackgroundSubtractorMOG2 > createBackgroundSubtractorMOG2(int history=500, double varThreshold=16, bool detectShadows=true)
Creates MOG2 Background Subtractor.
Once we have collected the result images, we can compare them with the ground truth data. There exist several publicly available sequences for background subtraction that come with ground truth data. If you decide to use the Background Models Challenge (BMC), then the result images can be used as input for the BMC Wizard. The wizard can compute different measures about the accuracy of the results.