1 How to Use Background Subtraction Methods {#tutorial_background_subtraction}
2 =========================================
4 - Background subtraction (BS) is a common and widely used technique for generating a foreground
5 mask (namely, a binary image containing the pixels belonging to moving objects in the scene) by
7 - As the name suggests, BS calculates the foreground mask performing a subtraction between the
8 current frame and a background model, containing the static part of the scene or, more in
9 general, everything that can be considered as background given the characteristics of the
12 ![](images/Background_Subtraction_Tutorial_Scheme.png)
14 - Background modeling consists of two main steps:
16 -# Background Initialization;
19 In the first step, an initial model of the background is computed, while in the second step that
20 model is updated in order to adapt to possible changes in the scene.
22 - In this tutorial we will learn how to perform BS by using OpenCV.
27 In this tutorial you will learn how to:
29 -# Read data from videos or image sequences by using @ref cv::VideoCapture ;
30 -# Create and update the background model by using @ref cv::BackgroundSubtractor class;
31 -# Get and show the foreground mask by using @ref cv::imshow ;
36 In the following you can find the source code. We will let the user choose to process either a video
37 file or a sequence of images.
39 We will use @ref cv::BackgroundSubtractorMOG2 in this sample, to generate the foreground mask.
41 The results as well as the input data are shown on the screen.
44 - **Downloadable code**: Click
45 [here](https://github.com/opencv/opencv/tree/3.4/samples/cpp/tutorial_code/video/bg_sub.cpp)
48 @include samples/cpp/tutorial_code/video/bg_sub.cpp
52 - **Downloadable code**: Click
53 [here](https://github.com/opencv/opencv/tree/3.4/samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java)
56 @include samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java
60 - **Downloadable code**: Click
61 [here](https://github.com/opencv/opencv/tree/3.4/samples/python/tutorial_code/video/background_subtraction/bg_sub.py)
64 @include samples/python/tutorial_code/video/background_subtraction/bg_sub.py
70 We discuss the main parts of the code above:
72 - A @ref cv::BackgroundSubtractor object will be used to generate the foreground mask. In this
73 example, default parameters are used, but it is also possible to declare specific parameters in
77 @snippet samples/cpp/tutorial_code/video/bg_sub.cpp create
81 @snippet samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java create
85 @snippet samples/python/tutorial_code/video/background_subtraction/bg_sub.py create
88 - A @ref cv::VideoCapture object is used to read the input video or input images sequence.
91 @snippet samples/cpp/tutorial_code/video/bg_sub.cpp capture
95 @snippet samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java capture
99 @snippet samples/python/tutorial_code/video/background_subtraction/bg_sub.py capture
102 - Every frame is used both for calculating the foreground mask and for updating the background. If
103 you want to change the learning rate used for updating the background model, it is possible to
104 set a specific learning rate by passing a parameter to the `apply` method.
107 @snippet samples/cpp/tutorial_code/video/bg_sub.cpp apply
111 @snippet samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java apply
115 @snippet samples/python/tutorial_code/video/background_subtraction/bg_sub.py apply
118 - The current frame number can be extracted from the @ref cv::VideoCapture object and stamped in
119 the top left corner of the current frame. A white rectangle is used to highlight the black
120 colored frame number.
123 @snippet samples/cpp/tutorial_code/video/bg_sub.cpp display_frame_number
127 @snippet samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java display_frame_number
131 @snippet samples/python/tutorial_code/video/background_subtraction/bg_sub.py display_frame_number
134 - We are ready to show the current input frame and the results.
137 @snippet samples/cpp/tutorial_code/video/bg_sub.cpp show
141 @snippet samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java show
145 @snippet samples/python/tutorial_code/video/background_subtraction/bg_sub.py show
151 - With the `vtest.avi` video, for the following frame:
153 ![](images/Background_Subtraction_Tutorial_frame.jpg)
155 The output of the program will look as the following for MOG2 method (gray areas are detected shadows):
157 ![](images/Background_Subtraction_Tutorial_result_MOG2.jpg)
159 The output of the program will look as the following for the KNN method (gray areas are detected shadows):
161 ![](images/Background_Subtraction_Tutorial_result_KNN.jpg)
166 - [Background Models Challenge (BMC) website](https://web.archive.org/web/20140418093037/http://bmc.univ-bpclermont.fr/)
167 - A Benchmark Dataset for Foreground/Background Extraction @cite vacavant2013benchmark