Updated May 26, 2023
Introduction to OpenCV Background Subtraction
The process of removing the background from a given image and displaying only the foreground objects is called background subtraction in OpenCV, and to perform the operation of background subtraction, we make use of three algorithms, namely BackgroundSubtractorMOG, BackgroundSubtractorMOG2, and BackgroundSubtractorGMG and in order to implement any of these algorithms on a given image, we have to create the corresponding object of the algorithm and then make use of backgroundsubtractor.apply() function on the image to subtract the background from the foreground in a given image and backgroundsubtractor.apply() function returns an image with only the foreground objects in the given image.
The syntax to implement the BackgroundSubtractorMOG algorithm to perform background subtraction in OpenCV is as follows:
object = bgsegm.createBackgroundSubtractorMOG()
background_subtracted_image = object.apply(source_image)
where bgsegm.createBackgroundSubtractorMOG() is the implementation of the BackgroundSubtractorMOG algorithm,
an object represents the object of the BackgroundSubtractorMOG algorithm, and
source_image is the image whose background is to be subtracted from the foreground.
The syntax to implement the BackgroundSubtractorMOG2 algorithm to perform background subtraction in OpenCV is as follows:
object1 = createBackgroundSubtractorMOG2()
background_subtracted_image = object1.apply(source_image)
where createBackgroundSubtractorMOG2() is the implementation of BackgroundSubtractorMOG2 algorithm,
object1 represents the object of the BackgroundSubtractorMOG2 algorithm and
source_image is the image whose background is to be subtracted from the foreground.
The syntax to implement the BackgroundSubtractorGMG algorithm to perform background subtraction in OpenCV is as follows:
object2 = bgsegm.createBackgroundSubtractorGMG()
background_subtracted_image = object2.apply(source_image)
where createBackgroundSubtractorGMG() is the implementation of BackgroundSubtractorGMG2 algorithm,
object2 represents the object of the BackgroundSubtractorGMG algorithm and
source_image is the image whose background is to be subtracted from the foreground.
How to work background subtraction in OpenCV?
Working of background subtraction in OpenCV is as follows:
The process of separating the background from the foreground objects is called Background subtraction in OpenCV.
Three algorithms that can be used to perform background subtraction in OpenCV are BackgroundSubtractorMOG algorithm, BackgroundSubtractorMOG2 algorithm, and BackgroundSubtractorGMG algorithm.
The algorithms work by looping through all the pixels in a given image.
If the pixels in the image match the threshold associated with the algorithms, then they are classified as match components; otherwise, they are classified as non-match components.
The non-matching components are declared as background in the image by the algorithm and is removed from the image.
To implement any of these algorithms to perform background subtraction of a given image, the object of the corresponding algorithm must be created.
Then using the object of the corresponding algorithm, apply() function must be used.
The backgroundsubtractor,apply() function removes the background from the image and returns the image with only the foreground object.
Examples
Let us discuss examples of OpenCV Background Subtraction.
Example #1
OpenCV program in Python to perform background subtraction on a given video by implementing the BackgroundSubtractorMOG algorithm and then displaying the background-subtracted images as the output on the screen:
#importing the required module
import cv2
#reading the video whose background is to be subtracted using BackgroundSubtractorMOG algorithm
videoread = cv2.VideoCapture('C:/Users/admin/Desktop/images/sports.mp4')
#implementing BackgroundSubtractorMOG algorithm by creating an object of the algorithm
object1 = cv2.bgsegm.createBackgroundSubtractorMOG();
while(1):
#reading the individual frames from the video
ret, imageframe = videoread.read()
#using backgroundsubtractor.apply() function to remove the background from the frame
bg_subtracted_video = object1.apply(imageframe);
#displaying the background subtracted video as the output on the screen
cv2.imshow('BackgroundSubtractorMOG', bg_subtracted_video);
cv2.waitKey(0)
videoread.release()
cv2.destroyAllWindows()
The output of the given program is shown in the snapshot below:
In the above program, we are importing the required modules. Then we read the video whose background is to be subtracted using the BackgroundSubtractorMOG algorithm. Then we are implementing the BackgroundSubtractorMOG algorithm by creating an object of the algorithm. Then we are reading the individual frames from the video. Then we are using backgroundsubtractor.apply() function to remove the background from the frame. Then we display the background-subtracted video as the output on the screen.
Example #2
OpenCV program in Python to perform background subtraction on a given video by implementing the BackgroundSubtractorMOG2 algorithm and then displaying the background-subtracted images as the output on the screen:
#importing the required module
import cv2
#reading the video whose background is to be subtracted using BackgroundSubtractorMOG2 algorithm
videoread = cv2.VideoCapture('C:/Users/admin/Desktop/images/sports.mp4')
#implementing BackgroundSubtractorMOG2 algorithm by creating an object of the algorithm
object1 = cv2.createBackgroundSubtractorMOG2();
while(1):
#reading the individual frames from the video
ret, imageframe = videoread.read()
#using backgroundsubtractor.apply() function to remove the background from the frame
bg_subtracted_video = object1.apply(imageframe);
#displaying the background subtracted video as the output on the screen
cv2.imshow('BackgroundSubtractorMOG2', bg_subtracted_video);
cv2.waitKey(0)
videoread.release()
cv2.destroyAllWindows()
The output of the given program is shown in the snapshot below:
In the above program, we are importing the required modules. Then we read the video whose background is to be subtracted using the BackgroundSubtractorMOG2 algorithm. Then we implement the BackgroundSubtractorMOG2 algorithm by creating an object of the algorithm. Then we are reading the individual frames from the video. Then we are using backgroundsubtractor.apply() function to remove the background from the frame. Then we display the background-subtracted video as the output on the screen.
Example #3
OpenCV program in Python to perform background subtraction on a given video by implementing the BackgroundSubtractorGMG algorithm and then displaying the background-subtracted images as the output on the screen:
#importing the required module
import cv2
#reading the video whose background is to be subtracted using BackgroundSubtractorGMG algorithm
videoread = cv2.VideoCapture('C:/Users/admin/Desktop/images/sports.mp4')
#implementing BackgroundSubtractorGMG algorithm by creating an object of the algorithm
object1 = cv2.bgsegm.createBackgroundSubtractorGMG();
while(1):
#reading the individual frames from the video
ret, imageframe = videoread.read()
#using backgroundsubtractor.apply() function to remove the background from the frame
bg_subtracted_video = object1.apply(imageframe);
#displaying the background subtracted video as the output on the screen
cv2.imshow('BackgroundSubtractorGMG', bg_subtracted_video);
cv2.waitKey(0)
videoread.release()
cv2.destroyAllWindows()
The output of the given program is shown in the snapshot below:
In the above program, we are importing the required modules. Then we read the video whose background is to be subtracted using the BackgroundSubtractorGMG algorithm. Then we implement the BackgroundSubtractorGMG algorithm by creating an object of the algorithm. Then we are reading the individual frames from the video. Then we use using background subtractor. apply the () function to remove the background from the frame. Then we display the background-subtracted video as the output on the screen.
Conclusion
In this article, we have learned the concept of Background Subtraction by implementing BackgroundSubtractorMOG, BackgroundSubtractorMOG2, and BackgroundSubtractorGMG algorithms with corresponding programming examples and their outputs to demonstrate them.
Recommended Articles
We hope that this EDUCBA information on “OpenCV Background Subtraction” was beneficial to you. You can view EDUCBA’s recommended articles for more information.