· Hakan Çelik · OpenCV / Video Analysis · 2 dk okuma

Background Subtraction

Learn background subtraction techniques to detect moving objects in video streams. We cover cv.createBackgroundSubtractorMOG2() and KNN-based background subtraction.

Background Subtraction

Goal

In this chapter,

  • We will familiarize with the background subtraction methods available in OpenCV.

Basics

Background subtraction is a major preprocessing step in many vision-based applications. For example, a customer counter using a static camera, or a traffic analysis system. In all these cases, you first need to extract the person or vehicles alone. Background subtraction achieves this by creating a model of the background.

OpenCV has implemented three such algorithms, among which we will talk about two:

  • MOG2 (Gaussian Mixture-based Background/Foreground Segmentation Algorithm)
  • KNN (K-Nearest Neigbours based Background/Foreground Segmentation)

BackgroundSubtractorMOG2

It is a Gaussian Mixture-based Background/Foreground Segmentation Algorithm. It is based on two papers by Z.Zivkovic, “Improved adaptive Gaussian mixture model for background subtraction” in 2004 and “Efficient Adaptive Density Estimation per Image Pixel for the Task of Background Subtraction” in 2006. One important feature of this algorithm is that it selects the appropriate number of gaussian distribution for each pixel.

import numpy as np
import cv2 as cv

cap = cv.VideoCapture('vtest.avi')

fgbg = cv.createBackgroundSubtractorMOG2()

while True:
    ret, frame = cap.read()

    if not ret:
        break

    fgmask = fgbg.apply(frame)

    cv.imshow('frame', fgmask)

    k = cv.waitKey(30) & 0xff
    if k == 27:
        break

cap.release()
cv.destroyAllWindows()

Shadow Detection

MOG2 algorithm can detect shadows. It is enabled by default. Shadows are marked with value 127 in the mask. You can disable this behavior:

fgbg = cv.createBackgroundSubtractorMOG2(detectShadows=True)

BackgroundSubtractorKNN

It is a K-nearest neighbours based Background/Foreground Segmentation Algorithm.

import numpy as np
import cv2 as cv

cap = cv.VideoCapture('vtest.avi')

fgbg = cv.createBackgroundSubtractorKNN()

while True:
    ret, frame = cap.read()

    if not ret:
        break

    fgmask = fgbg.apply(frame)

    cv.imshow('frame', fgmask)

    k = cv.waitKey(30) & 0xff
    if k == 27:
        break

cap.release()
cv.destroyAllWindows()

Additional Resources

  1. Zivkovic, Z. “Improved adaptive Gaussian mixture model for background subtraction”, 2004.
  2. Zivkovic, Z. and van der Heijden, F. “Efficient adaptive density estimation per image pixel for the task of background subtraction”, Pattern Recognition Letters, 2006.

Source: OpenCV Python Tutorials — Original Documentation

Back to Blog

Related Posts

View All Posts »
How OpenCV-Python Bindings Work

How OpenCV-Python Bindings Work

OpenCV · 3 dk

Learn how OpenCV-Python bindings are generated from C++ headers. We cover CV_EXPORTS_W, CV_WRAP, and other macros, plus the gen2.py generator and hdr_parser.py header parser scripts.

Face Detection using Haar Cascades

Face Detection using Haar Cascades

OpenCV · 3 dk

Learn to use Haar Cascade classifiers in OpenCV for face and eye detection. This tutorial covers the theory behind Haar features, integral images, AdaBoost, and cascade classifiers.

High Dynamic Range (HDR) Imaging

High Dynamic Range (HDR) Imaging

OpenCV · 3 dk

Learn how to generate and display HDR images from an exposure sequence in OpenCV. We cover Debevec, Robertson, and Mertens exposure fusion algorithms with camera response function estimation.

Image Inpainting

Image Inpainting

OpenCV · 2 dk

Learn how to remove small noises, strokes, and damage from old photographs using OpenCV's cv.inpaint(). We cover the Telea and Navier-Stokes inpainting algorithms.