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 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 stablity even when there is change in luminosity and better identification capablity of shadows in the frames.Geometric multigrid: It makes uses of statiistical method and per pixel bayesin segmentation algorithm.
# Python code for Background subtraction using OpenCV import numpy as np import cv2 cap = cv2.VideoCapture('/home/sourabh/Downloads/people-walking.mp4') fgbg = cv2.createBackgroundSubtractorMOG2() while(1): ret, frame = cap.read() fgmask = fgbg.apply(frame) cv2.imshow('fgmask', fgmask) cv2.imshow('frame',frame ) k = cv2.waitKey(30) & 0xff if k == 27: break cap.release() cv2.destroyAllWindows() |
Original video frame:

Background subtracted video frame:

Thus, we saw an application of background subtraction algorithm detecting motions, life in video frames.
Recommended Posts:
- Python OpenCV - Background Subtraction
- Background subtraction - OpenCV
- Arithmetic Operations on Images using OpenCV | Set-1 (Addition and Subtraction)
- Project Idea | Motion detection using Background Subtraction Techniques
- Background Subtraction in an Image using Concept of Running Average
- OpenCV - Facial Landmarks and Face Detection using dlib and OpenCV
- Transition from OpenCV 2 to OpenCV 3.x
- Addition and Subtraction on TimeDelta objects using Pandas - Python
- Perform addition and subtraction using CherryPy
- Python | Subtraction of dictionaries
- Python | How to get Subtraction of tuples
- Python | Mutual tuple subtraction in list
- Python | Nth tuple index Subtraction by K
- Python program for addition and subtraction of complex numbers
- Python | Nested Tuples Subtraction
- Python | Create video using multiple images using OpenCV
- Image resizing using Seam carving using OpenCV in Python
- Cartooning an Image using OpenCV - Python
- Real-Time Edge Detection using OpenCV in Python | Canny edge detection method
- OpenCV Python Program to analyze an image using Histogram
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : pravesh25pandey

