2014-11-27 20:39:05 +08:00
|
|
|
How to Use Background Subtraction Methods {#tutorial_background_subtraction}
|
|
|
|
=========================================
|
|
|
|
|
2020-12-08 00:13:54 +08:00
|
|
|
@tableofcontents
|
|
|
|
|
2020-12-05 06:46:00 +08:00
|
|
|
@prev_tutorial{tutorial_stitcher}
|
2020-05-20 06:59:28 +08:00
|
|
|
@next_tutorial{tutorial_meanshift}
|
|
|
|
|
2020-12-05 06:46:00 +08:00
|
|
|
| | |
|
|
|
|
| -: | :- |
|
|
|
|
| Original author | Domenico Daniele Bloisi |
|
|
|
|
| Compatibility | OpenCV >= 3.0 |
|
|
|
|
|
2014-11-27 20:39:05 +08:00
|
|
|
- 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.
|
|
|
|
|
2014-11-28 21:21:28 +08:00
|
|
|
![](images/Background_Subtraction_Tutorial_Scheme.png)
|
2014-11-27 20:39:05 +08:00
|
|
|
|
|
|
|
- Background modeling consists of two main steps:
|
|
|
|
|
2014-11-28 21:21:28 +08:00
|
|
|
-# Background Initialization;
|
|
|
|
-# Background Update.
|
2014-11-27 20:39:05 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2018-10-27 04:17:18 +08:00
|
|
|
- In this tutorial we will learn how to perform BS by using OpenCV.
|
2014-11-27 20:39:05 +08:00
|
|
|
|
|
|
|
Goals
|
|
|
|
-----
|
|
|
|
|
|
|
|
In this tutorial you will learn how to:
|
|
|
|
|
2018-10-27 04:17:18 +08:00
|
|
|
-# Read data from videos or image sequences by using @ref cv::VideoCapture ;
|
2014-11-28 21:21:28 +08:00
|
|
|
-# Create and update the background model by using @ref cv::BackgroundSubtractor class;
|
|
|
|
-# Get and show the foreground mask by using @ref cv::imshow ;
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2020-11-10 20:36:13 +08:00
|
|
|
### Code
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2018-10-27 04:17:18 +08:00
|
|
|
In the following you can find the source code. We will let the user choose to process either a video
|
2014-11-27 20:39:05 +08:00
|
|
|
file or a sequence of images.
|
|
|
|
|
2017-12-31 19:23:02 +08:00
|
|
|
We will use @ref cv::BackgroundSubtractorMOG2 in this sample, to generate the foreground mask.
|
2014-11-27 20:39:05 +08:00
|
|
|
|
|
|
|
The results as well as the input data are shown on the screen.
|
|
|
|
|
2018-10-27 04:17:18 +08:00
|
|
|
@add_toggle_cpp
|
|
|
|
- **Downloadable code**: Click
|
2021-12-22 21:01:26 +08:00
|
|
|
[here](https://github.com/opencv/opencv/tree/4.x/samples/cpp/tutorial_code/video/bg_sub.cpp)
|
2018-10-27 04:17:18 +08:00
|
|
|
|
|
|
|
- **Code at glance:**
|
|
|
|
@include samples/cpp/tutorial_code/video/bg_sub.cpp
|
|
|
|
@end_toggle
|
|
|
|
|
|
|
|
@add_toggle_java
|
|
|
|
- **Downloadable code**: Click
|
2021-12-22 21:01:26 +08:00
|
|
|
[here](https://github.com/opencv/opencv/tree/4.x/samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java)
|
2018-10-27 04:17:18 +08:00
|
|
|
|
|
|
|
- **Code at glance:**
|
|
|
|
@include samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java
|
|
|
|
@end_toggle
|
|
|
|
|
|
|
|
@add_toggle_python
|
|
|
|
- **Downloadable code**: Click
|
2021-12-22 21:01:26 +08:00
|
|
|
[here](https://github.com/opencv/opencv/tree/4.x/samples/python/tutorial_code/video/background_subtraction/bg_sub.py)
|
2018-10-27 04:17:18 +08:00
|
|
|
|
|
|
|
- **Code at glance:**
|
|
|
|
@include samples/python/tutorial_code/video/background_subtraction/bg_sub.py
|
|
|
|
@end_toggle
|
2014-11-27 20:39:05 +08:00
|
|
|
|
|
|
|
Explanation
|
|
|
|
-----------
|
|
|
|
|
2018-10-27 04:17:18 +08:00
|
|
|
We discuss the main parts of the code above:
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2018-10-27 04:17:18 +08:00
|
|
|
- A @ref cv::BackgroundSubtractor object will be used to generate the foreground mask. In this
|
2014-11-27 20:39:05 +08:00
|
|
|
example, default parameters are used, but it is also possible to declare specific parameters in
|
|
|
|
the create function.
|
2018-10-27 04:17:18 +08:00
|
|
|
|
|
|
|
@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
|
2014-11-27 20:39:05 +08:00
|
|
|
you want to change the learning rate used for updating the background model, it is possible to
|
2018-10-27 04:17:18 +08:00
|
|
|
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
|
2014-11-27 20:39:05 +08:00
|
|
|
the top left corner of the current frame. A white rectangle is used to highlight the black
|
|
|
|
colored frame number.
|
2018-10-27 04:17:18 +08:00
|
|
|
|
|
|
|
@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
|
2014-11-27 20:39:05 +08:00
|
|
|
|
|
|
|
Results
|
|
|
|
-------
|
|
|
|
|
2018-10-27 04:17:18 +08:00
|
|
|
- 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):
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2018-10-27 04:17:18 +08:00
|
|
|
![](images/Background_Subtraction_Tutorial_result_KNN.jpg)
|
2014-11-27 20:39:05 +08:00
|
|
|
|
|
|
|
References
|
|
|
|
----------
|
|
|
|
|
2018-10-27 04:17:18 +08:00
|
|
|
- [Background Models Challenge (BMC) website](https://web.archive.org/web/20140418093037/http://bmc.univ-bpclermont.fr/)
|
2014-11-28 21:21:28 +08:00
|
|
|
- A Benchmark Dataset for Foreground/Background Extraction @cite vacavant2013benchmark
|