Skip to content

Performing Backdrop Elimination in Python Using OpenCV

Comprehensive Educational Haven: Our platform encompasses a vast array of learning resources, catering to various fields such as computer science, programming, traditional education, professional development, commerce, software tool mastery, competitive exam preparation, and more, thereby...

Embracing Python, we delve into the intricacies of background subtraction utilizing OpenCV, a...
Embracing Python, we delve into the intricacies of background subtraction utilizing OpenCV, a potent library in the realm of computer vision.

Performing Backdrop Elimination in Python Using OpenCV

Background subtraction is a popular computer vision technique used to separate moving objects from static scenes in a video. This method is widely used in various applications such as Pedestrian Tracking, Vehicle Counting, Security Enhancement, Visitor Counting, and more.

In this article, we will focus on three prominent background subtraction algorithms: BackgroundSubtractorMOG, BackgroundSubtractorMOG2, and Geometric Multigrid (GMG).

BackgroundSubtractorMOG

BackgroundSubtractorMOG, also known as Mixture of Gaussians, models each pixel as a mixture of Gaussians. However, it does not have a built-in mechanism specifically for shadow detection. Shadows are treated generally as part of the foreground or background depending on pixel intensity changes and statistics.

BackgroundSubtractorMOG2

BackgroundSubtractorMOG2 is an improved version of BackgroundSubtractorMOG, offering better adaptability to changing lighting and shadow detection. It identifies shadows in the video as regions where pixel values become darker but retain similar chromatic properties. These detected shadows are marked differently to distinguish them from moving objects, making it more robust to illumination changes and reducing false positives in the foreground mask.

Geometric Multigrid (GMG)

Geometric Multigrid (GMG) requires an initial training phase to build a reliable background model and then applies morphological operations to reduce noise. While it improves foreground mask quality and noise reduction, GMG does not include an inherent shadow detection mechanism comparable to MOG2. Shadows may be partially suppressed by post-processing but are not explicitly identified by the algorithm.

The following table summarises the shadow detection handling of these algorithms:

| Algorithm | Shadow Detection Handling | |-------------------------|---------------------------------------------------| | BackgroundSubtractorMOG | No explicit shadow detection | | BackgroundSubtractorMOG2| Built-in shadow detection, marks shadows distinctly| | Geometric Multigrid (GMG) | No inherent shadow detection; uses morphological noise reduction |

In videos where shadows can cause significant false foreground detections, BackgroundSubtractorMOG2 proves especially useful due to its built-in shadow detection mechanism. On the other hand, BackgroundSubtractorMOG and GMG might treat shadows mostly as foreground or suppress them through general post-processing techniques.

This method can be implemented using OpenCV's functions such as cv2.VideoCapture(), cv2.createBackgroundSubtractorMOG2(), and cv2.imshow(). The binary mask of moving objects is returned by the background subtraction, and both original and processed frames are displayed in real-time using cv2.imshow().

References: [1] Bouguet, J., & Lewis, D. (2009). Learning OpenCV: Computer Vision with the OpenCV Library. O'Reilly Media. [2] Stauffer, C., & Grimson, W. (1999). Adaptive Background Mixture Models for Real-time Tracking. Proceedings of the IEEE Computer Society Conference on Computer Vision and Pattern Recognition, 1999, 839–846.

Read also:

Latest