Today we are going to look at how you can remove the background from an image or video frame. The method is known as background subtraction in OpenCV Python.

Let’s Code it!

Background Subtraction OpenCV Python Algorithm

Import Packages

import cv2 as cv

Computer Vision and NumPy packages are very important for working in OpenCV Python.

import numpy as np

If you feel uncomfortable in OpenCV Python basics have a look at OpenCV Basic Operations and Image Manipulation in OpenCV.

Capture Video for Background Subtraction OpenCV

cap = cv.VideoCapture(‘./img/vtest.avi’)

VideoCapture() is an OpenCV function in Python, It takes one parameter i.e the source of the video. 0 for Front Camera, 1 for Rear Camera, and for recorded video provide the Absolute or Relative Path.

MOG2 Background Subtraction OpenCV Python

There are two functions in OpenCV for subtraction namely MOG2 and KNN.

fgbg = cv.createBackgroundSubtractorMOG2(detectShadows=False)

If you do not want to detect shadows and remove them from the image, pass the argument detectShadows as False.

KNN Background Subtraction OpenCV Python

fgbg = cv.createBackgroundSubtractorKNN(detectShadows=False)

This is another algorithm for background subtraction, known as KNN. It also takes detect shadows argument as True or False. This OpenCV function will initialize background subtraction.

Reading Frames

ret, frame =

Now read every frame from the video source. If retention is True it means the function successfully reads the frame from the video source.

Applying Background Subtraction in OpenCV Python

fgmask = fgbg.apply(frame)

In MOG2 and KNN background subtraction methods/steps we had created an instance of the background subtraction and the instance was named fgbg.

Now, we will use apply() function in every frame of the video to remove the background. The apply() function takes one parameter as an argument, i.e The source image/frame from which the background has to be removed.

Showing Frames on window

The background removed image “fgmask” is then shown using the cv.imshow() function.

The Subtracted frames are shown using a while loop that is true when the program is able to read the video. So we need some logic to end the program otherwise the program will be stuck in an infinite loop.

One way to end the infinite loop is to bind the Keyboard key to the program that will break the loop.

In this case, we are binding the Keyboard key ‘q’s to end the loop, and after that, all the created windows will be destroyed and the memory occupied will be released.

Note: Release the captured video that is read in the beginning, otherwise the machine’s memory will be full and the process will not work properly.

Background Subtraction OpenCV Python Code

This is the full code of the background subtraction function created in OpenCV Python.

def bs():
    cap = cv.VideoCapture('./img/vtest.avi')

    # fgbg = cv.createBackgroundSubtractorMOG2(detectShadows=False)
    fgbg = cv.createBackgroundSubtractorKNN(detectShadows=False)

    while cap.isOpened():
        ret, frame =

        if frame is None:

        fgmask = fgbg.apply(frame)

        cv.imshow('Frame', frame)
        cv.imshow('FG Mask', fgmask)
        if cv.waitKey(30) & 0xFF == ord('q'):


Background Subtraction GitHub

Get the Full Source Code at GitHub.

OpenCV Documentation

Look at the OpenCV Documentation for more knowledge of the functions.

About Author
0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Scroll to Top