OpenCV  4.5.4
Open Source Computer Vision
How to Use Background Subtraction Methods

Prev Tutorial: High level stitching API (Stitcher class)

Next Tutorial: Meanshift and Camshift

Original author Domenico Daniele Bloisi
Compatibility OpenCV >= 3.0

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:

Results

References