Background Subtraction OpenCV Python Using MOG2 and KNN Read it later

1/5 - (2 votes)

In this blog, we will learn about the basics of background subtraction and how to implement it in OpenCV using Python.

What is Background Subtraction?

Background subtraction is a technique used to separate the foreground from the background in a video stream.

The basic idea is to subtract the background image from the current frame of the video stream to obtain the foreground objects.

The background image can either be a static image or a dynamically updated image that adapts to changes in the scene.

Resulting foreground objects can then be used for further processing, such as object detection or tracking.

Why is Background Subtraction Important?

Background subtraction is an essential step in many computer vision and image processing applications. Here are some of the reasons why it’s important:

  1. Object Detection: Background subtraction is a critical step in object detection, which is used in surveillance systems, traffic monitoring, and other applications.
  2. Motion Analysis: Background subtraction can be used to track motion in a video sequence, which is important in applications such as human-computer interaction, sports analysis, and medical imaging.
  3. Image Segmentation: Background subtraction can be used as a preprocessing step for image segmentation, which is used in computer vision applications such as face recognition, object recognition, and scene understanding.

Types of Background Subtraction

There are several types of background subtraction algorithms, each with its strengths and weaknesses. Some of the most commonly used algorithms are:

  1. Gaussian Mixture-based Background/Foreground Segmentation Algorithm (MOG2)
  2. Adaptive Background Mixture Model (ABM):
  3. Codebook-based Background Subtraction (CBBS)
  4. Approximate Median Filter-based Background Subtraction (AMF)

Background Subtraction using OpenCV Python

In this section, we will demonstrate how to perform background subtraction using OpenCV and Python.

Read Video Stream

We can create an instance of the VideoCapture class and pass the path of the video file or the camera index as an argument. The camera index is usually 0 for the default camera.

First, we need to read the video stream from a camera or a file. OpenCV provides the VideoCapture function for this purpose.

import cv2

cap = cv2.VideoCapture(0) # Read video stream from default camera

Applying Background Subtraction OpenCV Python

Next, we need to apply background subtraction to the video stream.

OpenCV provides several functions for this purpose, such as createBackgroundSubtractorMOG2, createBackgroundSubtractorKNN, etc.

We can choose any of these functions based on our requirements.

# Create a background subtractor object using MOG2 algorithm
fgbg = cv2.createBackgroundSubtractorMOG2()

while True:
    # Read a frame from the video stream
    ret, frame = cap.read()
    
    if not ret:
        break
    
    # Apply background subtraction to the current frame
    fgmask = fgbg.apply(frame)
    
    # Display the foreground mask
    cv2.imshow('Foreground Mask', fgmask)
    
    # Exit if 'q' is pressed
    if cv2.waitKey(1) == ord('q'):
        break

# Release the video stream and destroy all windows
cap.release()
cv2.destroyAllWindows()

In the above code, we create an instance of the MOG2 background subtractor using the cv2.createBackgroundSubtractorMOG2() function. We then read a frame from the video stream using the cap.read() function and apply the background subtraction using the fgbg.apply(frame) function. The resulting foreground mask is displayed using the cv2.imshow() function. Finally, we release the video stream using the cap.release() function and destroy all windows using the cv2.destroyAllWindows() function.

Was This Article Helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *