OpenCV Video Background Subtraction (MOG, MOG2)
In the field of computer vision, background subtraction is a commonly used technique for extracting foreground objects from a video sequence.
The core idea of background subtraction is to model the background and then compare the current frame with the background model, thereby separating the foreground objects.
OpenCV provides a variety of background subtraction algorithms, among which MOG (Mixture of Gaussians) and MOG2 are the two most commonly used methods.
Basic concepts of background subtraction
Background subtraction is a technique used for video analysis, mainly to detect moving objects in a video. Its basic flow is as follows:
- Background modeling: Build a background model by analyzing multiple frames in the video sequence.
- Foreground detection: Compare the current frame with the background model and find regions that differ significantly from the background; these regions are the foreground objects.
- Background update: Over time, the background may change (such as lighting changes, movement of background objects, etc.), so the background model must be updated continuously.
MOG (Mixture of Gaussians) Algorithm
Principle
The MOG algorithm is a background subtraction method based on the Gaussian Mixture Model (GMM). Its core idea is to use multiple Gaussian distributions to model the pixel values in the background. The value of each pixel is regarded as a random variable whose distribution is composed of multiple Gaussian distributions. In this way, MOG can handle complex changes in the background, such as lighting changes and shadows.
Algorithm steps
- Initialization: Initialize multiple Gaussian distributions for each pixel.
- Model update: For each frame, update the Gaussian distribution parameters (mean, variance, weight) of each pixel.
- Foreground detection: Compare the current frame's pixel values with the Gaussian distributions in the background model; if a pixel value does not fall within any Gaussian distribution, mark it as foreground.
Implementation in OpenCV
In OpenCV, the MOG algorithm can be created through the cv2.bgsegm.createBackgroundSubtractorMOG() function to create a background subtractor. The following is a simple example code:
Example
import
cv2
Create a MOG background subtractor
mog
=
cv2.
bgsegm
.
createBackgroundSubtractorMOG
(
)
Read the video
cap
=
cv2.
VideoCapture
(
'video.mp4'
)
while
True
:
ret
,
frame
=
cap.
read
(
)
if
not
ret:
break
Apply background subtraction
fg_mask
=
mog.
apply
(
frame
)
Display Results
cv2.
imshow
(
'Frame'
,
frame
)
cv2.
imshow
(
'FG Mask'
,
fg_mask
)
if
cv2.
waitKey
(
30
)
&
0xFF
==
ord
(
'q'
)
:
break
cap.
release
(
)
cv2.
destroyAllWindows
(
)
MOG2 (Mixture of Gaussians Version 2) Algorithm
Principle
MOG2 is an improved version of MOG. The main difference is that it can automatically select the number of Gaussian distributions and adapt better to background changes. By dynamically adjusting the number and parameters of Gaussian distributions, MOG2 can model the background more accurately, thereby improving the accuracy of foreground detection.
Algorithm steps
- Initialization: Initialize multiple Gaussian distributions for each pixel.
- Model update: For each frame, update the Gaussian distribution parameters of each pixel, and increase or decrease the number of Gaussian distributions as needed.
- Foreground detection: Compare the current frame's pixel values with the Gaussian distributions in the background model; if a pixel value does not fall within any Gaussian distribution, mark it as foreground.
Implementation in OpenCV
In OpenCV, the MOG2 algorithm can be created through the cv2.createBackgroundSubtractorMOG2() function to create a background subtractor. The following is a simple example code:
Example
import
cv2
Create a MOG2 background subtractor
mog2
=
cv2.
createBackgroundSubtractorMOG2
(
)
Read the video
cap
=
cv2.
VideoCapture
(
'video.mp4'
)
while
True
:
ret
,
frame
=
cap.
read
(
)
if
not
ret:
break
Apply background subtraction
fg_mask
=
mog2.
apply
(
frame
)
Display Results
cv2.
imshow
(
'Frame'
,
frame
)
cv2.
imshow
(
'FG Mask'
,
fg_mask
)
if
cv2.
waitKey
(
30
)
&
0xFF
==
ord
(
'q'
)
:
break
cap.
release
(
)
cv2.
destroyAllWindows
(
)
Applications of background subtraction
-
Video Surveillance: Used to detect moving targets in surveillance video, such as pedestrians and vehicles.
-
Motion analysis: Used to analyze the motion trajectories and behavior of targets in the video.
-
Human-computer interaction: Used to detect the user's gestures or face to achieve human-computer interaction.
The following is a complete MOG2 background subtraction example code:
Example
import
cv2
Read the video
cap
=
cv2.
VideoCapture
(
"path/to/video.mp4"
)
Create a MOG2 background subtractor
fgbg
=
cv2.
createBackgroundSubtractorMOG2
(
)
while
True
:
ret
,
frame
=
cap.
read
(
)
if
not
ret:
break
Apply the background subtractor
fgmask
=
fgbg.
apply
(
frame
)
Display Results
cv2.
imshow
(
"MOG2 Background Subtraction"
,
fgmask
)
Press the 'q' key to exit
if
cv2.
waitKey
(
30
)
&
0xFF
==
ord
(
'q'
)
:
break
Dispose resources
cap.
release
(
)
cv2.
destroyAllWindows
(
)
Comparison of MOG and MOG2
Background subtraction is an important technique in video analysis, and MOG and MOG2 are two commonly used background subtraction algorithms in OpenCV.
The MOG algorithm models the background with a fixed number of Gaussian distributions, suitable for scenarios with little background change, while the MOG2 algorithm, by dynamically adjusting the number and parameters of Gaussian distributions, can adapt better to background changes, suitable for scenarios with more background changes.
| Feature | MOG | MOG2 |
|---|---|---|
| Number of Gaussian distributions | Fixed | Dynamic adjustment |
| Background update speed | Slower | Faster |
| Ability to adapt to background changes | Weaker | Strong |
| Computational complexity | Lower | Higher |
| Applicable scenarios | Scenarios with little background change | Scenarios with more background change |