1 How to Use Background Subtraction Methods {#tutorial_background_subtraction}
2 =========================================
4 @next_tutorial{tutorial_meanshift}
6 - Background subtraction (BS) is a common and widely used technique for generating a foreground
7 mask (namely, a binary image containing the pixels belonging to moving objects in the scene) by
9 - As the name suggests, BS calculates the foreground mask performing a subtraction between the
10 current frame and a background model, containing the static part of the scene or, more in
11 general, everything that can be considered as background given the characteristics of the
14 ![](images/Background_Subtraction_Tutorial_Scheme.png)
16 - Background modeling consists of two main steps:
18 -# Background Initialization;
21 In the first step, an initial model of the background is computed, while in the second step that
22 model is updated in order to adapt to possible changes in the scene.
24 - In this tutorial we will learn how to perform BS by using OpenCV.
29 In this tutorial you will learn how to:
31 -# Read data from videos or image sequences by using @ref cv::VideoCapture ;
32 -# Create and update the background model by using @ref cv::BackgroundSubtractor class;
33 -# Get and show the foreground mask by using @ref cv::imshow ;
38 In the following you can find the source code. We will let the user choose to process either a video
39 file or a sequence of images.
41 We will use @ref cv::BackgroundSubtractorMOG2 in this sample, to generate the foreground mask.
43 The results as well as the input data are shown on the screen.
46 - **Downloadable code**: Click
47 [here](https://github.com/opencv/opencv/tree/3.4/samples/cpp/tutorial_code/video/bg_sub.cpp)
50 @include samples/cpp/tutorial_code/video/bg_sub.cpp
54 - **Downloadable code**: Click
55 [here](https://github.com/opencv/opencv/tree/3.4/samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java)
58 @include samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java
62 - **Downloadable code**: Click
63 [here](https://github.com/opencv/opencv/tree/3.4/samples/python/tutorial_code/video/background_subtraction/bg_sub.py)
66 @include samples/python/tutorial_code/video/background_subtraction/bg_sub.py
72 We discuss the main parts of the code above:
74 - A @ref cv::BackgroundSubtractor object will be used to generate the foreground mask. In this
75 example, default parameters are used, but it is also possible to declare specific parameters in
79 @snippet samples/cpp/tutorial_code/video/bg_sub.cpp create
83 @snippet samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java create
87 @snippet samples/python/tutorial_code/video/background_subtraction/bg_sub.py create
90 - A @ref cv::VideoCapture object is used to read the input video or input images sequence.
93 @snippet samples/cpp/tutorial_code/video/bg_sub.cpp capture
97 @snippet samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java capture
101 @snippet samples/python/tutorial_code/video/background_subtraction/bg_sub.py capture
104 - Every frame is used both for calculating the foreground mask and for updating the background. If
105 you want to change the learning rate used for updating the background model, it is possible to
106 set a specific learning rate by passing a parameter to the `apply` method.
109 @snippet samples/cpp/tutorial_code/video/bg_sub.cpp apply
113 @snippet samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java apply
117 @snippet samples/python/tutorial_code/video/background_subtraction/bg_sub.py apply
120 - The current frame number can be extracted from the @ref cv::VideoCapture object and stamped in
121 the top left corner of the current frame. A white rectangle is used to highlight the black
122 colored frame number.
125 @snippet samples/cpp/tutorial_code/video/bg_sub.cpp display_frame_number
129 @snippet samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java display_frame_number
133 @snippet samples/python/tutorial_code/video/background_subtraction/bg_sub.py display_frame_number
136 - We are ready to show the current input frame and the results.
139 @snippet samples/cpp/tutorial_code/video/bg_sub.cpp show
143 @snippet samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java show
147 @snippet samples/python/tutorial_code/video/background_subtraction/bg_sub.py show
153 - With the `vtest.avi` video, for the following frame:
155 ![](images/Background_Subtraction_Tutorial_frame.jpg)
157 The output of the program will look as the following for MOG2 method (gray areas are detected shadows):
159 ![](images/Background_Subtraction_Tutorial_result_MOG2.jpg)
161 The output of the program will look as the following for the KNN method (gray areas are detected shadows):
163 ![](images/Background_Subtraction_Tutorial_result_KNN.jpg)
168 - [Background Models Challenge (BMC) website](https://web.archive.org/web/20140418093037/http://bmc.univ-bpclermont.fr/)
169 - A Benchmark Dataset for Foreground/Background Extraction @cite vacavant2013benchmark