Today we are going to look at how you can remove the background from an image or video frames . The method is known as background subtraction in OpenCV Python.
Background Subtraction:
📌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 able to subtract or eliminate the background portion in an image. Its output is a binary segmented image which essentially gives information about the non-stationary objects in the image. There lies a problem in this concept of finding a non-stationary portion, as the shadow of the moving object can be moving and sometimes being classified in the foreground.
The popular Background subtraction algorithms are:
📌BackgroundSubtractorMOG : It is a Gaussian mixture based background segmentation algorithm.
📌BackgroundSubtractorMOG2: It uses the same concept but the major advantage that it provides is in terms of stability even when there is a change in luminosity and better identification capability of shadows in the frames.
Library Used:
📌cv2
📌NumPy
Functions Used:
📌VideoCapture(‘"’)
📌createBackgroundSubtractorMOG2()
📌read()
📌apply()
1)VideoCapture(‘"’)
📌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.
2)createBackgroundSubtractorMOG2()
📌It is a Gaussian mixture based background segmentation algorithm. the major advantage that it provides is in terms of stability even when there is a change in luminosity and better identification capability of shadows in the frames.
⚠️If you do not want to detect shadows and remove it from the image, pass the argument detect shadows as False.
3) read()
📌To read every frame from the video source.
4)apply()
📌The 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.
Source Code & Link:
Link - https://drive.google.com/drive/folders/1mXZKzuactzvl0BDulNTgExtE9zuNlHPb
Subscribe: https://www.youtube.com/channel/UCNs6a3HlrbYw7dSUEXk9W3A
#python #opencv