2e480f55853af2e46ad0f7b9dca725d7d34ab234
[platform/upstream/opencv.git] / doc / tutorials / video / background_subtraction / background_subtraction.markdown
1 How to Use Background Subtraction Methods {#tutorial_background_subtraction}
2 =========================================
3
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
6     using static cameras.
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
10     observed scene.
11
12     ![](images/Background_Subtraction_Tutorial_Scheme.png)
13
14 -   Background modeling consists of two main steps:
15
16     -#  Background Initialization;
17     -#  Background Update.
18
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.
21
22 -   In this tutorial we will learn how to perform BS by using OpenCV.
23
24 Goals
25 -----
26
27 In this tutorial you will learn how to:
28
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 ;
32
33 Code
34 ----
35
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.
38
39 We will use @ref cv::BackgroundSubtractorMOG2 in this sample, to generate the foreground mask.
40
41 The results as well as the input data are shown on the screen.
42
43 @add_toggle_cpp
44 -   **Downloadable code**: Click
45     [here](https://github.com/opencv/opencv/tree/3.4/samples/cpp/tutorial_code/video/bg_sub.cpp)
46
47 -   **Code at glance:**
48     @include samples/cpp/tutorial_code/video/bg_sub.cpp
49 @end_toggle
50
51 @add_toggle_java
52 -   **Downloadable code**: Click
53     [here](https://github.com/opencv/opencv/tree/3.4/samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java)
54
55 -   **Code at glance:**
56     @include samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java
57 @end_toggle
58
59 @add_toggle_python
60 -   **Downloadable code**: Click
61     [here](https://github.com/opencv/opencv/tree/3.4/samples/python/tutorial_code/video/background_subtraction/bg_sub.py)
62
63 -   **Code at glance:**
64     @include samples/python/tutorial_code/video/background_subtraction/bg_sub.py
65 @end_toggle
66
67 Explanation
68 -----------
69
70 We discuss the main parts of the code above:
71
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
74     the create function.
75
76 @add_toggle_cpp
77 @snippet samples/cpp/tutorial_code/video/bg_sub.cpp create
78 @end_toggle
79
80 @add_toggle_java
81 @snippet samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java create
82 @end_toggle
83
84 @add_toggle_python
85 @snippet samples/python/tutorial_code/video/background_subtraction/bg_sub.py create
86 @end_toggle
87
88 -   A @ref cv::VideoCapture object is used to read the input video or input images sequence.
89
90 @add_toggle_cpp
91 @snippet samples/cpp/tutorial_code/video/bg_sub.cpp capture
92 @end_toggle
93
94 @add_toggle_java
95 @snippet samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java capture
96 @end_toggle
97
98 @add_toggle_python
99 @snippet samples/python/tutorial_code/video/background_subtraction/bg_sub.py capture
100 @end_toggle
101
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.
105
106 @add_toggle_cpp
107 @snippet samples/cpp/tutorial_code/video/bg_sub.cpp apply
108 @end_toggle
109
110 @add_toggle_java
111 @snippet samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java apply
112 @end_toggle
113
114 @add_toggle_python
115 @snippet samples/python/tutorial_code/video/background_subtraction/bg_sub.py apply
116 @end_toggle
117
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.
121
122 @add_toggle_cpp
123 @snippet samples/cpp/tutorial_code/video/bg_sub.cpp display_frame_number
124 @end_toggle
125
126 @add_toggle_java
127 @snippet samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java display_frame_number
128 @end_toggle
129
130 @add_toggle_python
131 @snippet samples/python/tutorial_code/video/background_subtraction/bg_sub.py display_frame_number
132 @end_toggle
133
134 -   We are ready to show the current input frame and the results.
135
136 @add_toggle_cpp
137 @snippet samples/cpp/tutorial_code/video/bg_sub.cpp show
138 @end_toggle
139
140 @add_toggle_java
141 @snippet samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java show
142 @end_toggle
143
144 @add_toggle_python
145 @snippet samples/python/tutorial_code/video/background_subtraction/bg_sub.py show
146 @end_toggle
147
148 Results
149 -------
150
151 -   With the `vtest.avi` video, for the following frame:
152
153     ![](images/Background_Subtraction_Tutorial_frame.jpg)
154
155     The output of the program will look as the following for MOG2 method (gray areas are detected shadows):
156
157     ![](images/Background_Subtraction_Tutorial_result_MOG2.jpg)
158
159     The output of the program will look as the following for the KNN method (gray areas are detected shadows):
160
161     ![](images/Background_Subtraction_Tutorial_result_KNN.jpg)
162
163 References
164 ----------
165
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