Introduction to OpenCV background substration
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 backgorundsubtractor. 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 BackgroundSubtractorMOG algorithm,
an object represents the object of BackgroundSubtractorMOG algorithm and
source_image is the image whose background is to be subtracted from the foreground.
The syntax to implement 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 matches with 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 foreground object.
Examples
Let us discuss examples of OpenCV background substration.
Example #1
OpenCV program in python to perform background subtraction on a given video by implementing 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 are reading the video whose background is to be subtracted using BackgroundSubtractorMOG algorithm. Then we are implementing 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 are displaying 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 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 are reading the video whose background is to be subtracted using BackgroundSubtractorMOG2 algorithm. Then we are implementing 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 are displaying 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 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 are reading the video whose background is to be subtracted using BackgroundSubtractorGMG algorithm. Then we are implementing BackgroundSubtractorGMG algorithm by creating an object of the algorithm. Then we are reading the individual frames from the video. Then we are using background subtractor. apply the () function to remove the background from the frame. Then we are displaying the background-subtracted video as the output on the screen.
Conclusion – OpenCV background substration
In this article, we have learnt the concept of background subtraction by implementing BackgroundSubtractorMOG, BackgroundSubtractorMOG2, and BackgroundSubtractorGMG algorithms with corresponding programming examples and their outputs to demonstrate them.
Recommended Articles
This is a guide to OpenCV background subtraction. Here we discuss the Introduction, syntax, How to work background subtraction in OpenCV? and examples. You may also have a look at the following articles to learn more –
1 Online Course | 4 Hands-on Projects | 12+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses