OpenCV Video Background Subtraction (MOG, MOG2)

In the field of computer vision, background subtraction is a commonly used technique for extracting foreground objects from video sequences.

The core idea of background subtraction is to model the background, and then compare the current frame with the background model to separate out foreground objects.

OpenCV provides multiple background subtraction algorithms, among which MOG (Mixture of Gaussians) and MOG2 are the two most commonly used methods.

Basic Concepts of Background Subtraction

Background subtraction is a technique used for video analysis, mainly for detecting moving objects in videos. Its basic process is as follows:

  1. Background modeling: By analyzing multiple frames in a video sequence, establish a background model.
  2. Foreground detection: Compare the current frame with the background model to identify regions with significant differences from the background; these regions are the foreground objects.
  3. Background update: Over time, the background may change (such as lighting changes, movement of background objects, etc.), so the background model needs to be continuously updated.

MOG (Mixture of Gaussians) Algorithm

Principle

The MOG algorithm is a background subtraction method based on the Gaussian Mixture Model (GMM). Its core idea is to use multiple Gaussian distributions to model pixel values in the background. Each pixel value is regarded as a random variable, whose distribution is composed of multiple Gaussian distributions. In this way, MOG can handle complex changes in the background, such as lighting changes and shadows.

Algorithm Steps

  1. Initialization: Initialize multiple Gaussian distributions for each pixel.
  2. Model update: For each frame, update the Gaussian distribution parameters (mean, variance, weight) for each pixel.
  3. Foreground detection: Compare the current frame's pixel values with the Gaussian distributions in the background model. If a pixel value does not fall within the range of any Gaussian distribution, it is marked as foreground.

Implementation in OpenCV

In OpenCV, the MOG algorithm can be created using thecv2.bgsegm.createBackgroundSubtractorMOG()function to create a background subtractor. The following is a simple example code:

Example

import cv2

# Create MOG background subtractor
mog = cv2.bgsegm.createBackgroundSubtractorMOG()

# Read video
cap = cv2.VideoCapture('video.mp4')

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

    # Apply background subtraction
    fg_mask = mog.apply(frame)

    # Display result
    cv2.imshow('Frame', frame)
    cv2.imshow('FG Mask', fg_mask)

    if cv2.waitKey(30) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

MOG2 (Mixture of Gaussians Version 2) Algorithm

Principle

MOG2 is an improved version of MOG. The main difference is that it can automatically select the number of Gaussian distributions and better adapt to background changes. By dynamically adjusting the number and parameters of Gaussian distributions, MOG2 can model the background more accurately, thereby improving the accuracy of foreground detection.

Algorithm Steps

  1. Initialization: Initialize multiple Gaussian distributions for each pixel.
  2. Model update: For each frame, update the Gaussian distribution parameters for each pixel, and increase or decrease the number of Gaussian distributions as needed.
  3. Foreground detection: Compare the current frame's pixel values with the Gaussian distributions in the background model. If a pixel value does not fall within the range of any Gaussian distribution, it is marked as foreground.

Implementation in OpenCV

In OpenCV, the MOG2 algorithm can be created using thecv2.createBackgroundSubtractorMOG2()function to create a background subtractor. The following is a simple example code:

Example

import cv2

# Create MOG2 background subtractor
mog2 = cv2.createBackgroundSubtractorMOG2()

# Read video
cap = cv2.VideoCapture('video.mp4')

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

    # Apply background subtraction
    fg_mask = mog2.apply(frame)

    # Display result
    cv2.imshow('Frame', frame)
    cv2.imshow('FG Mask', fg_mask)

    if cv2.waitKey(30) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Applications of Background Subtraction

  • Video surveillance:Used to detect moving targets in surveillance video, such as pedestrians, vehicles, etc.

  • Motion analysis:Used to analyze the motion trajectories and behaviors of targets in video.

  • Human-computer interaction:Used to detect user gestures or faces to achieve human-computer interaction.

The following is a complete MOG2 background subtraction example code:

Example

import cv2

# Read video
cap = cv2.VideoCapture("path/to/video.mp4")

# Create MOG2 background subtractor
fgbg = cv2.createBackgroundSubtractorMOG2()

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

    # Apply background subtractor
    fgmask = fgbg.apply(frame)

    # Display result
    cv2.imshow("MOG2 Background Subtraction", fgmask)

    # Press 'q' to exit
    if cv2.waitKey(30) & 0xFF == ord('q'):
        break

# Release resources
cap.release()
cv2.destroyAllWindows()

Comparison of MOG and MOG2

Background subtraction is an important technique in video analysis, and MOG and MOG2 are two commonly used background subtraction algorithms in OpenCV.

The MOG algorithm uses a fixed number of Gaussian distributions to model the background, making it suitable for scenes with fewer background changes, while the MOG2 algorithm dynamically adjusts the number and parameters of Gaussian distributions, allowing it to better adapt to background changes and making it suitable for scenes with more background changes.

Feature MOG MOG2
Number of Gaussian distributions Fixed Dynamically adjusted
Background update speed Slower Faster
Ability to adapt to background changes Weaker Stronger
Computational complexity Lower Higher
Applicable scenarios Scenes with fewer background changes Scenes with more background changes
Other extensions