@prev_tutorial{tutorial_stitcher} @next_tutorial{tutorial_meanshift}
| | | | -: | :- | | Original author | Domenico Daniele Bloisi | | Compatibility | OpenCV >= 3.0 |
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.
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.
In this tutorial you will learn how to:
-# Read data from videos or image sequences by using @ref cv::VideoCapture ; -# Create and update the background model by using @ref cv::BackgroundSubtractor class; -# Get and show the foreground mask by using @ref cv::imshow ;
In the following you can find the source code. We will let the user choose to process either a video file or a sequence of images.
We will use @ref cv::BackgroundSubtractorMOG2 in this sample, to generate the foreground mask.
The results as well as the input data are shown on the screen.
Downloadable code: Click here
Code at glance: @include samples/cpp/tutorial_code/video/bg_sub.cpp @end_toggle
Downloadable code: Click here
Code at glance: @include samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java @end_toggle
Downloadable code: Click here
Code at glance: @include samples/python/tutorial_code/video/background_subtraction/bg_sub.py @end_toggle
We discuss the main parts of the code above:
@add_toggle_cpp @snippet samples/cpp/tutorial_code/video/bg_sub.cpp create @end_toggle
@add_toggle_java @snippet samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java create @end_toggle
@add_toggle_python @snippet samples/python/tutorial_code/video/background_subtraction/bg_sub.py create @end_toggle
@add_toggle_cpp @snippet samples/cpp/tutorial_code/video/bg_sub.cpp capture @end_toggle
@add_toggle_java @snippet samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java capture @end_toggle
@add_toggle_python @snippet samples/python/tutorial_code/video/background_subtraction/bg_sub.py capture @end_toggle
apply
method.@add_toggle_cpp @snippet samples/cpp/tutorial_code/video/bg_sub.cpp apply @end_toggle
@add_toggle_java @snippet samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java apply @end_toggle
@add_toggle_python @snippet samples/python/tutorial_code/video/background_subtraction/bg_sub.py apply @end_toggle
@add_toggle_cpp @snippet samples/cpp/tutorial_code/video/bg_sub.cpp display_frame_number @end_toggle
@add_toggle_java @snippet samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java display_frame_number @end_toggle
@add_toggle_python @snippet samples/python/tutorial_code/video/background_subtraction/bg_sub.py display_frame_number @end_toggle
@add_toggle_cpp @snippet samples/cpp/tutorial_code/video/bg_sub.cpp show @end_toggle
@add_toggle_java @snippet samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java show @end_toggle
@add_toggle_python @snippet samples/python/tutorial_code/video/background_subtraction/bg_sub.py show @end_toggle
With the vtest.avi
video, for the following frame:
The output of the program will look as the following for MOG2 method (gray areas are detected shadows):
The output of the program will look as the following for the KNN method (gray areas are detected shadows):