Background Subtraction - iffatAGheyas/computer-vision-handbook GitHub Wiki
🎩 Background Subtraction
Background subtraction is the process of separating moving foreground objects from a static background in a video.
“What’s new or different in this frame compared to what’s usually there?”
🧠 What Is Background Subtraction?
It’s often used in:
- 🚗 Traffic monitoring (detect vehicles)
- 🚶 Security cameras (detect people)
- 🎯 Motion tracking (track movement without full object recognition)
⚙️ How It Works (Conceptually)
- Build a background model over time from the video.
- Compare each new frame to that model.
- Difference = moving foreground (e.g. a person walking in front of a static wall).
✅ Built-in Background Subtraction Algorithms in OpenCV
OpenCV provides ready-to-use models:
Method | Description |
---|---|
cv2.createBackgroundSubtractorMOG2() |
Gaussian mixture model; handles shadows |
cv2.createBackgroundSubtractorKNN() |
Uses k-Nearest Neighbours for segmentation |
baby.mp4
🧪 Example Code: Background Subtraction on import cv2
import matplotlib.pyplot as plt
# Open the video
cap = cv2.VideoCapture("baby.mp4")
# Create background subtractor
backSub = cv2.createBackgroundSubtractorMOG2()
# Step 1: Warm up the model with initial frames
for _ in range(30): # feed first 30 frames into the model
ret, frame = cap.read()
if not ret:
break
backSub.apply(frame) # update model, don’t use mask yet
# Step 2: Seek to the target frame (frame 40)
cap.set(cv2.CAP_PROP_POS_FRAMES, 40)
ret, frame = cap.read()
if not ret:
print("❌ Couldn’t read target frame")
cap.release()
exit()
# Step 3: Apply background subtraction on this frame
fg_mask = backSub.apply(frame)
# Step 4: Show result
plt.figure(figsize=(10, 4))
plt.subplot(1, 2, 1)
plt.imshow(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
plt.title("Original Frame")
plt.axis("off")
plt.subplot(1, 2, 2)
plt.imshow(fg_mask, cmap="gray")
plt.title("Foreground Mask (MOG2)")
plt.axis("off")
plt.tight_layout()
plt.show()
cap.release()
📋 What This Code Does
Step | Purpose |
---|---|
1 | Feeds 30 initial frames to learn the “background” |
2 | Seeks to a later frame with potential motion |
3 | Applies apply() — now it knows what’s “background” |
4 | Displays the result — moving objects appear as white |