mirror of https://github.com/opencv/opencv.git
Open Source Computer Vision Library
https://opencv.org/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
175 lines
6.0 KiB
175 lines
6.0 KiB
10 years ago
|
How to Use Background Subtraction Methods {#tutorial_background_subtraction}
|
||
|
=========================================
|
||
|
|
||
4 years ago
|
@prev_tutorial{tutorial_stitcher}
|
||
5 years ago
|
@next_tutorial{tutorial_meanshift}
|
||
|
|
||
4 years ago
|
| | |
|
||
|
| -: | :- |
|
||
|
| Original author | Domenico Daniele Bloisi |
|
||
|
| Compatibility | OpenCV >= 3.0 |
|
||
|
|
||
10 years ago
|
- 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.
|
||
|
|
||
10 years ago
|
![](images/Background_Subtraction_Tutorial_Scheme.png)
|
||
10 years ago
|
|
||
|
- Background modeling consists of two main steps:
|
||
|
|
||
10 years ago
|
-# Background Initialization;
|
||
|
-# Background Update.
|
||
10 years ago
|
|
||
|
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.
|
||
|
|
||
6 years ago
|
- In this tutorial we will learn how to perform BS by using OpenCV.
|
||
10 years ago
|
|
||
|
Goals
|
||
|
-----
|
||
|
|
||
|
In this tutorial you will learn how to:
|
||
|
|
||
6 years ago
|
-# Read data from videos or image sequences by using @ref cv::VideoCapture ;
|
||
10 years ago
|
-# Create and update the background model by using @ref cv::BackgroundSubtractor class;
|
||
|
-# Get and show the foreground mask by using @ref cv::imshow ;
|
||
10 years ago
|
|
||
4 years ago
|
### Code
|
||
10 years ago
|
|
||
6 years ago
|
In the following you can find the source code. We will let the user choose to process either a video
|
||
10 years ago
|
file or a sequence of images.
|
||
|
|
||
7 years ago
|
We will use @ref cv::BackgroundSubtractorMOG2 in this sample, to generate the foreground mask.
|
||
10 years ago
|
|
||
|
The results as well as the input data are shown on the screen.
|
||
|
|
||
6 years ago
|
@add_toggle_cpp
|
||
|
- **Downloadable code**: Click
|
||
6 years ago
|
[here](https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/video/bg_sub.cpp)
|
||
6 years ago
|
|
||
|
- **Code at glance:**
|
||
|
@include samples/cpp/tutorial_code/video/bg_sub.cpp
|
||
|
@end_toggle
|
||
|
|
||
|
@add_toggle_java
|
||
|
- **Downloadable code**: Click
|
||
6 years ago
|
[here](https://github.com/opencv/opencv/tree/master/samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java)
|
||
6 years ago
|
|
||
|
- **Code at glance:**
|
||
|
@include samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java
|
||
|
@end_toggle
|
||
|
|
||
|
@add_toggle_python
|
||
|
- **Downloadable code**: Click
|
||
6 years ago
|
[here](https://github.com/opencv/opencv/tree/master/samples/python/tutorial_code/video/background_subtraction/bg_sub.py)
|
||
6 years ago
|
|
||
|
- **Code at glance:**
|
||
|
@include samples/python/tutorial_code/video/background_subtraction/bg_sub.py
|
||
|
@end_toggle
|
||
10 years ago
|
|
||
|
Explanation
|
||
|
-----------
|
||
|
|
||
6 years ago
|
We discuss the main parts of the code above:
|
||
10 years ago
|
|
||
6 years ago
|
- A @ref cv::BackgroundSubtractor object will be used to generate the foreground mask. In this
|
||
10 years ago
|
example, default parameters are used, but it is also possible to declare specific parameters in
|
||
|
the create function.
|
||
6 years ago
|
|
||
|
@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
|
||
10 years ago
|
you want to change the learning rate used for updating the background model, it is possible to
|
||
6 years ago
|
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
|
||
10 years ago
|
the top left corner of the current frame. A white rectangle is used to highlight the black
|
||
|
colored frame number.
|
||
6 years ago
|
|
||
|
@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
|
||
10 years ago
|
|
||
|
Results
|
||
|
-------
|
||
|
|
||
6 years ago
|
- 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):
|
||
10 years ago
|
|
||
6 years ago
|
![](images/Background_Subtraction_Tutorial_result_KNN.jpg)
|
||
10 years ago
|
|
||
|
References
|
||
|
----------
|
||
|
|
||
6 years ago
|
- [Background Models Challenge (BMC) website](https://web.archive.org/web/20140418093037/http://bmc.univ-bpclermont.fr/)
|
||
10 years ago
|
- A Benchmark Dataset for Foreground/Background Extraction @cite vacavant2013benchmark
|