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