Blame view

3rdparty/opencv-4.5.4/doc/tutorials/others/background_subtraction.markdown 5.98 KB
f4334277   Hu Chunming   提交3rdparty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
  How to Use Background Subtraction Methods {#tutorial_background_subtraction}
  =========================================
  
  @tableofcontents
  
  @prev_tutorial{tutorial_stitcher}
  @next_tutorial{tutorial_meanshift}
  
  |    |    |
  | -: | :- |
  | Original author | Domenico Daniele Bloisi |
  | Compatibility | OpenCV >= 3.0 |
  
  -   Background subtraction (BS) is a common and widely used technique for generating a foreground
      mask (namely, a binary image containing the pixels belonging to moving objects in the scene) by
      using static cameras.
  -   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.
  
      ![](images/Background_Subtraction_Tutorial_Scheme.png)
  
  -   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.
  
  Goals
  -----
  
  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 ;
  
  ### Code
  
  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.
  
  @add_toggle_cpp
  -   **Downloadable code**: Click
      [here](https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/video/bg_sub.cpp)
  
  -   **Code at glance:**
      @include samples/cpp/tutorial_code/video/bg_sub.cpp
  @end_toggle
  
  @add_toggle_java
  -   **Downloadable code**: Click
      [here](https://github.com/opencv/opencv/tree/master/samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java)
  
  -   **Code at glance:**
      @include samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java
  @end_toggle
  
  @add_toggle_python
  -   **Downloadable code**: Click
      [here](https://github.com/opencv/opencv/tree/master/samples/python/tutorial_code/video/background_subtraction/bg_sub.py)
  
  -   **Code at glance:**
      @include samples/python/tutorial_code/video/background_subtraction/bg_sub.py
  @end_toggle
  
  Explanation
  -----------
  
  We discuss the main parts of the code above:
  
  -   A @ref cv::BackgroundSubtractor object will be used to generate the foreground mask. In this
      example, default parameters are used, but it is also possible to declare specific parameters in
      the create function.
  
  @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
  
  -   A @ref cv::VideoCapture object is used to read the input video or input images sequence.
  
  @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
  
  -   Every frame is used both for calculating the foreground mask and for updating the background. If
      you want to change the learning rate used for updating the background model, it is possible to
      set a specific learning rate by passing a parameter to the `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
  
  -   The current frame number can be extracted from the @ref cv::VideoCapture object and stamped in
      the top left corner of the current frame. A white rectangle is used to highlight the black
      colored frame number.
  
  @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
  
  -   We are ready to show the current input frame and the results.
  
  @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
  
  Results
  -------
  
  -   With the `vtest.avi` video, for the following frame:
  
      ![](images/Background_Subtraction_Tutorial_frame.jpg)
  
      The output of the program will look as the following for MOG2 method (gray areas are detected shadows):
  
      ![](images/Background_Subtraction_Tutorial_result_MOG2.jpg)
  
      The output of the program will look as the following for the KNN method (gray areas are detected shadows):
  
      ![](images/Background_Subtraction_Tutorial_result_KNN.jpg)
  
  References
  ----------
  
  -   [Background Models Challenge (BMC) website](https://web.archive.org/web/20140418093037/http://bmc.univ-bpclermont.fr/)
  -   A Benchmark Dataset for Foreground/Background Extraction @cite vacavant2013benchmark