fixing trailing whitespaces
This commit is contained in:
parent
a34f7b7714
commit
f0910466f9
@ -6,11 +6,11 @@ How to Use Background Subtraction Methods
|
||||
* Background subtraction (BS) is a common and widely used technique for generating a foreground mask (namely, a binary image containing the pixels belonging to moving objects in the scene) by using static cameras.
|
||||
|
||||
* As the name suggests, BS calculates the foreground mask performing a subtraction between the current frame and a background model, containing the static part of the scene or, more in general, everything that can be considered as background given the characteristics of the observed scene.
|
||||
|
||||
|
||||
.. image:: images/Background_Subtraction_Tutorial_Scheme.png
|
||||
:alt: Background Subtraction - General Scheme
|
||||
:align: center
|
||||
|
||||
:align: center
|
||||
|
||||
* Background modeling consists of two main steps:
|
||||
|
||||
#. Background Initialization;
|
||||
@ -62,7 +62,7 @@ The results as well as the input data are shown on the screen.
|
||||
Ptr<BackgroundSubtractor> pMOG; //MOG Background subtractor
|
||||
Ptr<BackgroundSubtractor> pMOG2; //MOG2 Background subtractor
|
||||
int keyboard;
|
||||
|
||||
|
||||
//function declarations
|
||||
void help();
|
||||
void processVideo(char* videoFilename);
|
||||
@ -74,7 +74,7 @@ The results as well as the input data are shown on the screen.
|
||||
<< "--------------------------------------------------------------------------" << endl
|
||||
<< "This program shows how to use background subtraction methods provided by " << endl
|
||||
<< " OpenCV. You can process both videos (-vid) and images (-img)." << endl
|
||||
<< endl
|
||||
<< endl
|
||||
<< "Usage:" << endl
|
||||
<< "./bs {-vid <video filename>|-img <image filename>}" << endl
|
||||
<< "for example: ./bs -vid video.avi" << endl
|
||||
@ -87,7 +87,7 @@ The results as well as the input data are shown on the screen.
|
||||
{
|
||||
//print help information
|
||||
help();
|
||||
|
||||
|
||||
//check for the input parameter correctness
|
||||
if(argc != 3) {
|
||||
cerr <<"Incorret input list" << endl;
|
||||
@ -99,7 +99,7 @@ The results as well as the input data are shown on the screen.
|
||||
namedWindow("Frame");
|
||||
namedWindow("FG Mask MOG");
|
||||
namedWindow("FG Mask MOG 2");
|
||||
|
||||
|
||||
//create Background Subtractor objects
|
||||
pMOG = createBackgroundSubtractorMOG(); //MOG approach
|
||||
pMOG2 = createBackgroundSubtractorMOG2(); //MOG2 approach
|
||||
@ -186,7 +186,7 @@ The results as well as the input data are shown on the screen.
|
||||
string suffix = fn.substr(index2);
|
||||
string frameNumberString = fn.substr(index+1, index2-index-1);
|
||||
istringstream iss(frameNumberString);
|
||||
int frameNumber;
|
||||
int frameNumber = 0;
|
||||
iss >> frameNumber;
|
||||
rectangle(frame, cv::Point(10, 2), cv::Point(100,20),
|
||||
cv::Scalar(255,255,255), -1);
|
||||
@ -214,7 +214,7 @@ The results as well as the input data are shown on the screen.
|
||||
fn.assign(nextFrameFilename);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
* The source file can be downloaded :download:`here <../../../../samples/cpp/tutorial_code/video/bg_sub.cpp>`.
|
||||
|
||||
|
||||
@ -226,18 +226,18 @@ We discuss the main parts of the above code:
|
||||
#. First, three Mat objects are allocated to store the current frame and two foreground masks, obtained by using two different BS algorithms.
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
|
||||
Mat frame; //current frame
|
||||
Mat fgMaskMOG; //fg mask generated by MOG method
|
||||
Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method
|
||||
|
||||
|
||||
#. Two :background_subtractor:`BackgroundSubtractor <>` objects will be used to generate the foreground masks. In this example, default parameters are used, but it is also possible to declare specific parameters in the create function.
|
||||
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
|
||||
Ptr<BackgroundSubtractor> pMOG; //MOG Background subtractor
|
||||
Ptr<BackgroundSubtractor> pMOG2; //MOG2 Background subtractor
|
||||
...
|
||||
...
|
||||
//create Background Subtractor objects
|
||||
pMOG = createBackgroundSubtractorMOG(); //MOG approach
|
||||
pMOG2 = createBackgroundSubtractorMOG2(); //MOG2 approach
|
||||
@ -277,11 +277,11 @@ We discuss the main parts of the above code:
|
||||
//update the background model
|
||||
pMOG->apply(frame, fgMaskMOG);
|
||||
pMOG2->apply(frame, fgMaskMOG2);
|
||||
|
||||
|
||||
#. The current frame number can be extracted from the :video_capture:`VideoCapture <>` object and stamped in the top left corner of the current frame. A white rectangle is used to highlight the black colored frame number.
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
|
||||
//get the frame number and write it on the current frame
|
||||
stringstream ss;
|
||||
rectangle(frame, cv::Point(10, 2), cv::Point(100,20),
|
||||
@ -299,7 +299,7 @@ We discuss the main parts of the above code:
|
||||
imshow("Frame", frame);
|
||||
imshow("FG Mask MOG", fgMaskMOG);
|
||||
imshow("FG Mask MOG 2", fgMaskMOG2);
|
||||
|
||||
|
||||
#. The same operations listed above can be performed using a sequence of images as input. The processImage function is called and, instead of using a :video_capture:`VideoCapture <>` object, the images are read by using :imread:`imread <>`, after individuating the correct path for the next frame to read.
|
||||
|
||||
.. code-block:: cpp
|
||||
@ -327,10 +327,7 @@ We discuss the main parts of the above code:
|
||||
//update the path of the current frame
|
||||
fn.assign(nextFrameFilename);
|
||||
|
||||
|
||||
Note that:
|
||||
|
||||
* this example works only on image sequences in which the filename format is <n>.png, where n is the frame number (e.g., 7.png).
|
||||
* Note that this example works only on image sequences in which the filename format is <n>.png, where n is the frame number (e.g., 7.png).
|
||||
|
||||
Results
|
||||
=======
|
||||
@ -382,13 +379,10 @@ In order to save the output images, we can use :imwrite:`imwrite <>`. Adding the
|
||||
}
|
||||
|
||||
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) <http://bmc.univ-bpclermont.fr/>`_, then the result images can be used as input for the `BMC Wizard <http://bmc.univ-bpclermont.fr/?q=node/7>`_. The wizard can compute different measures about the accuracy of the results.
|
||||
|
||||
|
||||
References
|
||||
==========
|
||||
|
||||
* Background Models Challenge (BMC) website, `<http://bmc.univ-bpclermont.fr/>`_
|
||||
|
||||
* Antoine Vacavant, Thierry Chateau, Alexis Wilhelm and Laurent Lequievre. A Benchmark Dataset for Foreground/Background Extraction. In ACCV 2012, Workshop: Background Models Challenge, LNCS 7728, 291-300. November 2012, Daejeon, Korea.
|
||||
|
||||
|
||||
|
||||
|
@ -13,15 +13,15 @@ Look here in order to find use on your video stream algoritms like: motion extra
|
||||
|
||||
=============== ======================================================
|
||||
|BgSub| **Title:** :ref:`Background_Subtraction`
|
||||
|
||||
*Compatibility:* > OpenCV 2.4.5
|
||||
|
||||
*Compatibility:* > OpenCV 2.4.6
|
||||
|
||||
*Author:* |Author_DomenicoB|
|
||||
|
||||
|
||||
We will learn how to extract foreground masks from both videos and sequences of images and to show them.
|
||||
|
||||
|
||||
=============== ======================================================
|
||||
|
||||
|
||||
.. |BgSub| image:: images/Background_Subtraction_Tutorial_Cover.jpg
|
||||
:height: 90pt
|
||||
:width: 90pt
|
||||
@ -34,4 +34,3 @@ Look here in order to find use on your video stream algoritms like: motion extra
|
||||
:hidden:
|
||||
|
||||
../background_subtraction/background_subtraction
|
||||
|
||||
|
@ -31,17 +31,17 @@ void processImages(char* firstFrameFilename);
|
||||
|
||||
void help()
|
||||
{
|
||||
cout
|
||||
<< "--------------------------------------------------------------------------" << endl
|
||||
<< "This program shows how to use background subtraction methods provided by " << endl
|
||||
<< " OpenCV. You can process both videos (-vid) and images (-img)." << endl
|
||||
<< endl
|
||||
<< "Usage:" << endl
|
||||
<< "./bs {-vid <video filename>|-img <image filename>}" << endl
|
||||
<< "for example: ./bs -vid video.avi" << endl
|
||||
<< "or: ./bs -img /data/images/1.png" << endl
|
||||
<< "--------------------------------------------------------------------------" << endl
|
||||
<< endl;
|
||||
cout
|
||||
<< "--------------------------------------------------------------------------" << endl
|
||||
<< "This program shows how to use background subtraction methods provided by " << endl
|
||||
<< " OpenCV. You can process both videos (-vid) and images (-img)." << endl
|
||||
<< endl
|
||||
<< "Usage:" << endl
|
||||
<< "./bs {-vid <video filename>|-img <image filename>}" << endl
|
||||
<< "for example: ./bs -vid video.avi" << endl
|
||||
<< "or: ./bs -img /data/images/1.png" << endl
|
||||
<< "--------------------------------------------------------------------------" << endl
|
||||
<< endl;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -49,38 +49,38 @@ void help()
|
||||
*/
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
//print help information
|
||||
help();
|
||||
//print help information
|
||||
help();
|
||||
|
||||
//check for the input parameter correctness
|
||||
if(argc != 3) {
|
||||
cerr <<"Incorret input list" << endl;
|
||||
cerr <<"exiting..." << endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
//check for the input parameter correctness
|
||||
if(argc != 3) {
|
||||
cerr <<"Incorret input list" << endl;
|
||||
cerr <<"exiting..." << endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
//create GUI windows
|
||||
//create GUI windows
|
||||
namedWindow("Frame");
|
||||
namedWindow("FG Mask MOG");
|
||||
namedWindow("FG Mask MOG 2");
|
||||
|
||||
//create Background Subtractor objects
|
||||
pMOG = createBackgroundSubtractorMOG(); //MOG approach
|
||||
pMOG2 = createBackgroundSubtractorMOG2(); //MOG2 approach
|
||||
//create Background Subtractor objects
|
||||
pMOG = createBackgroundSubtractorMOG(); //MOG approach
|
||||
pMOG2 = createBackgroundSubtractorMOG2(); //MOG2 approach
|
||||
|
||||
if(strcmp(argv[1], "-vid") == 0) {
|
||||
//input data coming from a video
|
||||
processVideo(argv[2]);
|
||||
//input data coming from a video
|
||||
processVideo(argv[2]);
|
||||
}
|
||||
else if(strcmp(argv[1], "-img") == 0) {
|
||||
//input data coming from a sequence of images
|
||||
processImages(argv[2]);
|
||||
//input data coming from a sequence of images
|
||||
processImages(argv[2]);
|
||||
}
|
||||
else {
|
||||
//error in reading input parameters
|
||||
cerr <<"Please, check the input parameters." << endl;
|
||||
cerr <<"Exiting..." << endl;
|
||||
return EXIT_FAILURE;
|
||||
//error in reading input parameters
|
||||
cerr <<"Please, check the input parameters." << endl;
|
||||
cerr <<"Exiting..." << endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
//destroy GUI windows
|
||||
destroyAllWindows();
|
||||
@ -94,35 +94,35 @@ void processVideo(char* videoFilename) {
|
||||
//create the capture object
|
||||
VideoCapture capture(videoFilename);
|
||||
if(!capture.isOpened()){
|
||||
//error in opening the video input
|
||||
cerr << "Unable to open video file: " << videoFilename << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
//error in opening the video input
|
||||
cerr << "Unable to open video file: " << videoFilename << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
//read input data. ESC or 'q' for quitting
|
||||
while( (char)keyboard != 'q' && (char)keyboard != 27 ){
|
||||
//read the current frame
|
||||
if(!capture.read(frame)) {
|
||||
cerr << "Unable to read next frame." << endl;
|
||||
cerr << "Exiting..." << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
//update the background model
|
||||
pMOG->apply(frame, fgMaskMOG);
|
||||
pMOG2->apply(frame, fgMaskMOG2);
|
||||
//get the frame number and write it on the current frame
|
||||
stringstream ss;
|
||||
rectangle(frame, cv::Point(10, 2), cv::Point(100,20),
|
||||
//read the current frame
|
||||
if(!capture.read(frame)) {
|
||||
cerr << "Unable to read next frame." << endl;
|
||||
cerr << "Exiting..." << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
//update the background model
|
||||
pMOG->apply(frame, fgMaskMOG);
|
||||
pMOG2->apply(frame, fgMaskMOG2);
|
||||
//get the frame number and write it on the current frame
|
||||
stringstream ss;
|
||||
rectangle(frame, cv::Point(10, 2), cv::Point(100,20),
|
||||
cv::Scalar(255,255,255), -1);
|
||||
ss << capture.get(CAP_PROP_POS_FRAMES);
|
||||
string frameNumberString = ss.str();
|
||||
putText(frame, frameNumberString.c_str(), cv::Point(15, 15),
|
||||
FONT_HERSHEY_SIMPLEX, 0.5 , cv::Scalar(0,0,0));
|
||||
//show the current frame and the fg masks
|
||||
imshow("Frame", frame);
|
||||
imshow("FG Mask MOG", fgMaskMOG);
|
||||
imshow("FG Mask MOG 2", fgMaskMOG2);
|
||||
//get the input from the keyboard
|
||||
keyboard = waitKey( 30 );
|
||||
//show the current frame and the fg masks
|
||||
imshow("Frame", frame);
|
||||
imshow("FG Mask MOG", fgMaskMOG);
|
||||
imshow("FG Mask MOG 2", fgMaskMOG2);
|
||||
//get the input from the keyboard
|
||||
keyboard = waitKey( 30 );
|
||||
}
|
||||
//delete capture object
|
||||
capture.release();
|
||||
@ -135,52 +135,52 @@ void processImages(char* fistFrameFilename) {
|
||||
//read the first file of the sequence
|
||||
frame = imread(fistFrameFilename);
|
||||
if(!frame.data){
|
||||
//error in opening the first image
|
||||
cerr << "Unable to open first image frame: " << fistFrameFilename << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
//error in opening the first image
|
||||
cerr << "Unable to open first image frame: " << fistFrameFilename << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
//current image filename
|
||||
string fn(fistFrameFilename);
|
||||
//read input data. ESC or 'q' for quitting
|
||||
while( (char)keyboard != 'q' && (char)keyboard != 27 ){
|
||||
//update the background model
|
||||
pMOG->apply(frame, fgMaskMOG);
|
||||
pMOG2->apply(frame, fgMaskMOG2);
|
||||
//get the frame number and write it on the current frame
|
||||
size_t index = fn.find_last_of("/");
|
||||
if(index == string::npos) {
|
||||
index = fn.find_last_of("\\");
|
||||
}
|
||||
size_t index2 = fn.find_last_of(".");
|
||||
string prefix = fn.substr(0,index+1);
|
||||
string suffix = fn.substr(index2);
|
||||
string frameNumberString = fn.substr(index+1, index2-index-1);
|
||||
istringstream iss(frameNumberString);
|
||||
int frameNumber;
|
||||
iss >> frameNumber;
|
||||
rectangle(frame, cv::Point(10, 2), cv::Point(100,20),
|
||||
//update the background model
|
||||
pMOG->apply(frame, fgMaskMOG);
|
||||
pMOG2->apply(frame, fgMaskMOG2);
|
||||
//get the frame number and write it on the current frame
|
||||
size_t index = fn.find_last_of("/");
|
||||
if(index == string::npos) {
|
||||
index = fn.find_last_of("\\");
|
||||
}
|
||||
size_t index2 = fn.find_last_of(".");
|
||||
string prefix = fn.substr(0,index+1);
|
||||
string suffix = fn.substr(index2);
|
||||
string frameNumberString = fn.substr(index+1, index2-index-1);
|
||||
istringstream iss(frameNumberString);
|
||||
int frameNumber = 0;
|
||||
iss >> frameNumber;
|
||||
rectangle(frame, cv::Point(10, 2), cv::Point(100,20),
|
||||
cv::Scalar(255,255,255), -1);
|
||||
putText(frame, frameNumberString.c_str(), cv::Point(15, 15),
|
||||
putText(frame, frameNumberString.c_str(), cv::Point(15, 15),
|
||||
FONT_HERSHEY_SIMPLEX, 0.5 , cv::Scalar(0,0,0));
|
||||
//show the current frame and the fg masks
|
||||
imshow("Frame", frame);
|
||||
imshow("FG Mask MOG", fgMaskMOG);
|
||||
imshow("FG Mask MOG 2", fgMaskMOG2);
|
||||
//get the input from the keyboard
|
||||
keyboard = waitKey( 30 );
|
||||
//search for the next image in the sequence
|
||||
ostringstream oss;
|
||||
oss << (frameNumber + 1);
|
||||
string nextFrameNumberString = oss.str();
|
||||
string nextFrameFilename = prefix + nextFrameNumberString + suffix;
|
||||
//read the next frame
|
||||
frame = imread(nextFrameFilename);
|
||||
if(!frame.data){
|
||||
//error in opening the next image in the sequence
|
||||
cerr << "Unable to open image frame: " << nextFrameFilename << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
//update the path of the current frame
|
||||
fn.assign(nextFrameFilename);
|
||||
}
|
||||
//show the current frame and the fg masks
|
||||
imshow("Frame", frame);
|
||||
imshow("FG Mask MOG", fgMaskMOG);
|
||||
imshow("FG Mask MOG 2", fgMaskMOG2);
|
||||
//get the input from the keyboard
|
||||
keyboard = waitKey( 30 );
|
||||
//search for the next image in the sequence
|
||||
ostringstream oss;
|
||||
oss << (frameNumber + 1);
|
||||
string nextFrameNumberString = oss.str();
|
||||
string nextFrameFilename = prefix + nextFrameNumberString + suffix;
|
||||
//read the next frame
|
||||
frame = imread(nextFrameFilename);
|
||||
if(!frame.data){
|
||||
//error in opening the next image in the sequence
|
||||
cerr << "Unable to open image frame: " << nextFrameFilename << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
//update the path of the current frame
|
||||
fn.assign(nextFrameFilename);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user