OpenCV  4.2.0
Open Source Computer Vision
How to Use Background Subtraction Methods
  • 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.

  • Background modeling consists of two main steps:

    1. Background Initialization;
    2. 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:

  1. Read data from videos or image sequences by using cv::VideoCapture ;
  2. Create and update the background model by using cv::BackgroundSubtractor class;
  3. Get and show the foreground mask by using 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 cv::BackgroundSubtractorMOG2 in this sample, to generate the foreground mask.

The results as well as the input data are shown on the screen.

Explanation

We discuss the main parts of the code above:

  • A 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.
  • A cv::VideoCapture object is used to read the input video or input images sequence.
  • 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.
  • The current frame number can be extracted from the 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.
  • We are ready to show the current input frame and the results.

Results

  • With the vtest.avi video, for the following frame:

    The output of the program will look as the following for MOG2 method (gray areas are detected shadows):

    The output of the program will look as the following for the KNN method (gray areas are detected shadows):

References

cv::FONT_HERSHEY_SIMPLEX
normal size sans-serif font
Definition: imgproc.hpp:814
cv::String
std::string String
Definition: cvstd.hpp:150
cv::Point_< int >
cv::rectangle
void rectangle(InputOutputArray img, Rect rec, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
cv::BackgroundSubtractor::apply
virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1)=0
Computes a foreground mask.
cv::samples::findFile
cv::String findFile(const cv::String &relative_path, bool required=true, bool silentMode=false)
Try to find requested data file.
cv::VideoCapture
Class for video capturing from video files, image sequences or cameras.
Definition: videoio.hpp:608
cv::waitKey
int waitKey(int delay=0)
Waits for a pressed key.
cv::CAP_PROP_POS_FRAMES
0-based index of the frame to be decoded/captured next.
Definition: videoio.hpp:133
highgui.hpp
cv::Scalar_< double >
cv::rectangle
void rectangle(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a simple, thick, or filled up-right rectangle.
cv::createBackgroundSubtractorKNN
Ptr< BackgroundSubtractorKNN > createBackgroundSubtractorKNN(int history=500, double dist2Threshold=400.0, bool detectShadows=true)
Creates KNN Background Subtractor.
cv::createBackgroundSubtractorMOG2
Ptr< BackgroundSubtractorMOG2 > createBackgroundSubtractorMOG2(int history=500, double varThreshold=16, bool detectShadows=true)
Creates MOG2 Background Subtractor.
imgcodecs.hpp
cv::dnn::print
static void print(const MatShape &shape, const String &name="")
Definition: shape_utils.hpp:188
video.hpp
cv::Ptr
std::shared_ptr< _Tp > Ptr
Definition: cvstd_wrapper.hpp:23
cv::imshow
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
cv::Scalar
Scalar_< double > Scalar
Definition: types.hpp:669
cv::putText
void putText(InputOutputArray img, const String &text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=LINE_8, bool bottomLeftOrigin=false)
Draws a text string.
cv::Point
Point2i Point
Definition: types.hpp:194
cv::Mat
n-dimensional dense array class
Definition: mat.hpp:791
cv::imshow
void imshow(const String &winname, const ogl::Texture2D &tex)
Displays OpenGL 2D texture in the specified window.
cv::samples::findFileOrKeep
cv::String findFileOrKeep(const cv::String &relative_path, bool silentMode=false)
Definition: utility.hpp:1164
cv::CommandLineParser
Designed for command line parsing.
Definition: utility.hpp:796
cv
"black box" representation of the file storage associated with a file on disk.
Definition: affine.hpp:51
imgproc.hpp
videoio.hpp