Python OpenCV - Background Subtraction
Last Updated :
03 Jan, 2023
Background Subtraction is one of the major Image Processing tasks. It is used in various Image Processing applications like Image Segmentation, Object Detection, etc. OpenCV provides us 3 types of Background Subtraction algorithms:-
- BackgroundSubtractorMOG
- BackgroundSubtractorMOG2
- BackgroundSubtractorGMG
Normally, we can perform background Subtraction using matrix subtraction, i.e, just subtracting the static frame from the video. But this has a lot of drawbacks. It is a very less efficient algorithm for Background subtraction because it does not update itself. This problem is being handled by the Background Subtraction algorithms provided by OpenCV.
Using BackgroundSubtractorMOG
To use BackgroundSubtractorMOG we can use
cv2.bgsegm.createBackgroundSubtractorMOG()
Then we can apply it using the "apply" method on each frame of the video. Consider the below example for a better understanding of the topic.
Example:
Python3 1==
import numpy as np
import cv2
cap = cv2.VideoCapture('sample.mp4')
# initializing subtractor
fgbg = cv2.bgsegm.createBackgroundSubtractorMOG()
while(1):
ret, frame = cap.read()
# applying on each frame
fgmask = fgbg.apply(frame)
cv2.imshow('frame', fgmask)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
Output :
Using BackgroundSubtractorMOG2
In the previous subtractor worked fairly well but in real-world situations, there is also a presence of shadows. In BackgroundSubtractorMOG2, we can also detect shadows and in the output of the following code, it's clearly seen. To apply BackgroundSubtractorMOG2, use
cv2.createBackgroundSubtractorMOG2()
Example:
Python3 1==
import numpy as np
import cv2
cap = cv2.VideoCapture('sample.mp4')
# initializing subtractor
fgbg = cv2.createBackgroundSubtractorMOG2()
while(1):
ret, frame = cap.read()
# applying on each frame
fgmask = fgbg.apply(frame)
cv2.imshow('frame', fgmask)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
Output :
Using BackgroundSubtractorGMG
This algorithm combines statistical background image estimation and per-pixel Bayesian segmentation. It employs a probabilistic foreground segmentation algorithm that identifies possible foreground objects using Bayesian inference. To use BackgroundSubtractorGMG, use
cv2.bgsegm.createBackgroundSubtractorGMG()
Note: We will get a black window during first few frames.
Python3 1==
import numpy as np
import cv2
cap = cv2.VideoCapture('sample.mp4')
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
# initializing subtractor
fgbg = cv2.bgsegm.createBackgroundSubtractorGMG()
while(1):
ret, frame = cap.read()
# applying on each frame
fgmask = fgbg.apply(frame)
fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel)
cv2.imshow('frame', fgmask)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
Output :
Similar Reads
Python | Background subtraction using OpenCV Background Subtraction has several use cases in everyday life, It is being used for object segmentation, security enhancement, pedestrian tracking, counting the number of visitors, number of vehicles in traffic etc. It is able to learn and identify the foreground mask.As the name suggests, it is abl
2 min read
Background subtraction - OpenCV Background subtraction is a way of eliminating the background from image. To achieve this we extract the moving foreground from the static background. Background Subtraction has several use cases in everyday life, It is being used for object segmentation, security enhancement, pedestrian tracking, c
2 min read
How to subtract two images using Python-OpenCV ? In this article, we are going to see how to subtract two images using OpenCV in Python. Now, before getting into the topic we shall discuss some of the use cases of Arithmetic operations. Arithmetic operations like addition and subtraction can help us to make images brighter or darker. Specifically,
3 min read
OpenCV Tutorial in Python OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning library. It allows us to process images and videos, detect objects, faces and even handwriting. This tutorial will guide us through image and video processing from the basics to advanced topics using
5 min read
Getting Started with Python OpenCV Computer Vision is one of the techniques from which we can understand images and videos and can extract information from them. It is a subset of artificial intelligence that collects information from digital images or videos. Python OpenCV is the most popular computer vision library. By using it, o
15+ min read
Python OpenCV - startWindowThread() Function This article will discuss how to use the python OpenCV startWindowThread() function. Do you want to display images and videos using a simplified interface through an OpenCV code? Then, you must check out the OpenCV startWindowsThread() function, which lets you use the high GUI windows, i.e., a simpl
3 min read