In motion detection, the key metrics are Recall and Precision. Recall tells us how many actual motions were correctly found. Precision tells us how many detected motions were real. We want high recall to catch all movements, and high precision to avoid false alarms. The F1 score balances these two. Accuracy is less useful because most frames have no motion, so a model guessing "no motion" often looks good but is not helpful.
Motion detection basics in Computer Vision - Model Metrics & Evaluation
| Predicted Motion | Predicted No Motion |
|------------------|---------------------|
| True Motion: 80 | False Negative: 20 |
| False Positive: 10| True No Motion: 890 |
Total frames = 1000
Precision = TP / (TP + FP) = 80 / (80 + 10) = 0.89
Recall = TP / (TP + FN) = 80 / (80 + 20) = 0.80
F1 Score = 2 * (0.89 * 0.80) / (0.89 + 0.80) ≈ 0.84
If the motion detector is set to be very sensitive, it finds almost all motions (high recall) but also triggers many false alarms (low precision). This is like a security camera that alerts you too often, causing annoyance.
If it is set to be strict, it only alerts when very sure (high precision) but might miss some motions (low recall). This could miss important events.
Choosing the right balance depends on the use case. For home security, high recall is often more important to not miss intruders. For wildlife monitoring, high precision might be preferred to avoid wasting time on false alerts.
- Good: Precision and recall both above 0.85, meaning most motions are detected and false alarms are low.
- Bad: Precision below 0.5 means many false alarms; recall below 0.5 means many motions missed.
- Accuracy can be misleadingly high (e.g., 95%) if the model mostly predicts no motion because most frames have no motion.
- Accuracy paradox: High accuracy but poor detection because most frames have no motion.
- Data leakage: Using future frames or labels during training can inflate metrics falsely.
- Overfitting: Model works well on training video but fails on new scenes.
- Ignoring class imbalance: Motion frames are rare, so metrics must focus on recall and precision, not just accuracy.
Your motion detection model has 98% accuracy but only 12% recall on motion frames. Is it good for production? Why or why not?
Answer: No, it is not good. The model misses 88% of actual motions (low recall), which defeats the purpose of motion detection. The high accuracy is misleading because most frames have no motion, so guessing "no motion" often looks good but is useless.
Practice
Solution
Step 1: Understand motion detection purpose
Motion detection compares video frames to identify changes that show movement.Step 2: Eliminate unrelated options
Brightness increase, grayscale conversion, and compression are not about detecting motion.Final Answer:
To find changes between video frames indicating movement -> Option BQuick Check:
Motion detection = find changes [OK]
- Confusing motion detection with image enhancement
- Thinking motion detection compresses videos
- Mixing grayscale conversion with motion detection
Solution
Step 1: Recall typical motion detection steps
First convert to grayscale, then blur to reduce noise, find difference between frames, apply threshold to highlight changes, and finally find contours.Step 2: Match the correct sequence
Grayscale, blur, difference, threshold, contours matches this exact order.Final Answer:
Grayscale, blur, difference, threshold, contours -> Option CQuick Check:
Step order = grayscale -> blur -> difference -> threshold -> contours [OK]
- Mixing up the order of blur and difference
- Applying contours before threshold
- Skipping grayscale conversion
thresh?
import cv2
frame1 = cv2.imread('frame1.jpg')
frame2 = cv2.imread('frame2.jpg')
gray1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
diff = cv2.absdiff(gray1, gray2)
thresh = cv2.threshold(diff, 25, 255, cv2.THRESH_BINARY)[1]Solution
Step 1: Understand cv2.threshold output
The function returns a tuple; the second element is the thresholded image, which is a grayscale image with pixels either 0 or max value (255).Step 2: Analyze the code
Sincediffis grayscale, thresholding produces a binary grayscale image, not color or contours.Final Answer:
A grayscale image with pixel values 0 or 255 -> Option AQuick Check:
Threshold output = binary grayscale image [OK]
- Thinking threshold returns contours
- Assuming output is color image
- Confusing threshold value with output image
diff = cv2.absdiff(frame1, frame2) thresh = cv2.threshold(diff, 30, 255, cv2.THRESH_BINARY)[1] contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)What is the likely cause of the error?
Solution
Step 1: Check input types for absdiff
cv2.absdiff expects images of the same type and single channel for motion detection, usually grayscale.Step 2: Identify missing grayscale conversion
Frames are likely color images; missing conversion to grayscale causes error in absdiff.Final Answer:
Frames are color images; absdiff needs grayscale images -> Option DQuick Check:
absdiff input = grayscale images [OK]
- Assuming threshold returns single value only
- Thinking findContours needs color images
- Using invalid flags for findContours
Solution
Step 1: Understand contour filtering
Contours represent detected shapes; filtering by area removes small noise.Step 2: Apply area filter after contours
Ignoring contours smaller than 500 pixels ensures only significant motion is detected.Final Answer:
Filter contours by area and ignore those smaller than 500 pixels -> Option AQuick Check:
Filter contours by area ≥ 500 pixels [OK]
- Confusing threshold value with pixel area
- Applying blur instead of filtering contours
- Converting to color unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
