![]() |
VOOZH | about |
Background subtraction is technique in computer vision for detecting and isolating moving objects within video sequences. It plays an important role in applications like video surveillance, traffic monitoring, gesture recognition and automatic scene analysis, where distinguishing dynamic foreground elements from a static or slowly changing background is required.
OpenCV provides robust and widely used background subtraction methods, most notably:
Let's see the implementation of Background Subtraction using OpenCV,
Click here to download the used sample video.
Step 1: Install and Import the Required Libraries.
Step 2: Upload and Prepare Video
Upload a sample video file and open it for frame-by-frame processing.
Step 3: Background Subtraction Using MOG
We use the MOG algorithm to model each pixel as a mixture of Gaussians, distinguishing moving objects (foreground) from the background. The resulting binary mask shows detected motion in white. We show only the first 30 frames for brevity.
Output:
Step 4: Background Subtraction Using MOG2
MOG2 includes shadow detection (shadows might appear in gray), making it more robust against lighting changes and moving objects. Like before, moving regions appear as lighter areas in the mask.
Output:
Step 5: Background Subtraction Using GMG
GMG requires an initial training phase (about 120 frames) before outputting valid masks. Once trained, moving objects are highlighted. A morphological open operation reduces noise and improves mask clarity.
Output: