From 449b4d8a65fe7143d13fc844732f20099ffbd9e1 Mon Sep 17 00:00:00 2001 From: zio dodo Date: Wed, 21 Aug 2013 12:06:48 +0200 Subject: [PATCH 1/8] Added the tutorial on background subtraction, in the video section --- doc/conf.py | 6 +- doc/tutorials/definitions/tocDefinitions.rst | 1 + .../background_subtraction.rst | 394 ++++++++++++++++++ ...ckground_Subtraction_Tutorial_Result_1.png | Bin 0 -> 144597 bytes ...ckground_Subtraction_Tutorial_Result_2.png | Bin 0 -> 162431 bytes ...Background_Subtraction_Tutorial_Scheme.png | Bin 0 -> 79560 bytes .../Background_Subtraction_Tutorial_Cover.jpg | Bin 0 -> 2719 bytes .../table_of_content_video.rst | 29 +- modules/stitching/src/motion_estimators.cpp | 2 +- samples/cpp/tutorial_code/video/bg_sub.cpp | 172 ++++++++ 10 files changed, 599 insertions(+), 5 deletions(-) create mode 100644 doc/tutorials/video/background_subtraction/background_subtraction.rst create mode 100644 doc/tutorials/video/background_subtraction/images/Background_Subtraction_Tutorial_Result_1.png create mode 100644 doc/tutorials/video/background_subtraction/images/Background_Subtraction_Tutorial_Result_2.png create mode 100644 doc/tutorials/video/background_subtraction/images/Background_Subtraction_Tutorial_Scheme.png create mode 100644 doc/tutorials/video/table_of_content_video/images/Background_Subtraction_Tutorial_Cover.jpg create mode 100644 samples/cpp/tutorial_code/video/bg_sub.cpp diff --git a/doc/conf.py b/doc/conf.py index 312a1c2b9e..ff683cc6b7 100755 --- a/doc/conf.py +++ b/doc/conf.py @@ -401,5 +401,7 @@ extlinks = { 'brute_force_matcher' : ('http://docs.opencv.org/modules/features2d/doc/common_interfaces_of_descriptor_matchers.html?highlight=bruteforcematcher#bruteforcematcher%s', None ), 'cascade_classifier' : ('http://docs.opencv.org/modules/objdetect/doc/cascade_classification.html?highlight=cascadeclassifier#cascadeclassifier%s', None ), 'cascade_classifier_load' : ('http://docs.opencv.org/modules/objdetect/doc/cascade_classification.html?highlight=load#cascadeclassifier-load%s', None ), - 'cascade_classifier_detect_multiscale' : ('http://docs.opencv.org/modules/objdetect/doc/cascade_classification.html?highlight=detectmultiscale#cascadeclassifier-detectmultiscale%s', None ) - } + 'cascade_classifier_detect_multiscale' : ('http://docs.opencv.org/modules/objdetect/doc/cascade_classification.html?highlight=detectmultiscale#cascadeclassifier-detectmultiscale%s', None ), + 'background_subtractor' : ('http://opencv.itseez.com/modules/video/doc/motion_analysis_and_object_tracking.html?highlight=backgroundsubtractor#backgroundsubtractor%s', None), + 'video_capture' : ('http://opencv.itseez.com/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=videocapture#videocapture%s', None) + } diff --git a/doc/tutorials/definitions/tocDefinitions.rst b/doc/tutorials/definitions/tocDefinitions.rst index 4695623cca..c918aa2e8b 100644 --- a/doc/tutorials/definitions/tocDefinitions.rst +++ b/doc/tutorials/definitions/tocDefinitions.rst @@ -11,3 +11,4 @@ .. |Author_EricCh| unicode:: Eric U+0020 Christiansen .. |Author_AndreyP| unicode:: Andrey U+0020 Pavlenko .. |Author_AlexS| unicode:: Alexander U+0020 Smorkalov +.. |Author_DomenicoB| unicode:: Domenico U+0020 Daniele U+0020 Bloisi diff --git a/doc/tutorials/video/background_subtraction/background_subtraction.rst b/doc/tutorials/video/background_subtraction/background_subtraction.rst new file mode 100644 index 0000000000..864cca60cb --- /dev/null +++ b/doc/tutorials/video/background_subtraction/background_subtraction.rst @@ -0,0 +1,394 @@ +.. _Background_Subtraction: + +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 + +* Background modeling consists of two main steps: + + #. Background Initialization; + #. Background Update. + + In the first step, an initial model of the background is computed, while in the second step that model is updated in order to adapt to possible changes in the scene. + +* In this tutorial we will learn how to perform BS by using OpenCV. As input, we will use data coming from the publicly available data set `Background Models Challenge (BMC) `_ . + +Goals +====== + +In this tutorial you will learn how to: + + #. Read data from videos by using :video_capture:`VideoCapture <>` or image sequences by using :imread:`imread <>`; + #. Create and update the background model by using :background_subtractor:`BackgroundSubtractor <>` class; + #. Get and show the foreground mask by using :imshow:`imshow <>`; + #. Save the output by using :imwrite:`imwrite <>` to quantitatively evaluate the results. + +Code +===== + +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. + +* Two different methods are used to generate two foreground masks: + #. MOG + #. MOG2 +The results as well as the input data are shown on the screen. + +.. code-block:: cpp + + //opencv + #include + #include + #include + //C + #include + //C++ + #include + #include + + using namespace cv; + using namespace std; + + //global variables + Mat frame; //current frame + Mat fgMaskMOG; //fg mask generated by MOG method + Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method + Ptr pMOG; //MOG Background subtractor + Ptr pMOG2; //MOG2 Background subtractor + int keyboard; + + //function declarations + void help(); + void processVideo(char* videoFilename); + 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