Bird
Raised Fist0
Computer Visionml~20 mins

Motion detection basics in Computer Vision - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Motion Detection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the main purpose of background subtraction in motion detection?

Background subtraction is a common technique in motion detection. What does it primarily help to achieve?

AIt isolates moving objects by removing the static background from video frames.
BIt enhances the color saturation of moving objects in the video.
CIt increases the frame rate of the video to detect faster motion.
DIt compresses the video to reduce storage space.
Attempts:
2 left
💡 Hint

Think about how motion detection separates moving parts from the rest.

❓ Predict Output
intermediate
2:00remaining
What is the output of this motion detection code snippet?

Consider this Python code using OpenCV for simple motion detection:

import cv2
cap = cv2.VideoCapture('video.mp4')
ret, frame1 = cap.read()
ret, frame2 = cap.read()
diff = cv2.absdiff(frame1, frame2)
gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 25, 255, cv2.THRESH_BINARY)
count = cv2.countNonZero(thresh)
print(count)

What does the printed count represent?

AThe total number of pixels in the video frame.
BThe average brightness of the current frame.
CThe number of pixels that changed significantly between two consecutive frames.
DThe number of frames processed so far.
Attempts:
2 left
💡 Hint

Look at what cv2.countNonZero counts after thresholding the difference.

❓ Model Choice
advanced
2:00remaining
Which model is best suited for detecting motion in a video with changing lighting conditions?

You want to detect motion in a video where lighting changes frequently, like clouds passing over the sun. Which approach is best?

ABackground subtraction using a Gaussian Mixture Model (GMM).
BSimple frame differencing between consecutive frames.
CUsing a fixed threshold on grayscale pixel differences.
DApplying edge detection on each frame without background modeling.
Attempts:
2 left
💡 Hint

Consider models that adapt to gradual background changes.

❓ Metrics
advanced
2:00remaining
Which metric best evaluates the accuracy of a motion detection algorithm?

You have ground truth masks showing where motion occurs and your algorithm's predicted masks. Which metric best measures how well your algorithm detects motion?

ANumber of detected moving objects.
BIntersection over Union (IoU) between predicted and ground truth motion areas.
CFrame rate of the video processing.
DMean Squared Error (MSE) between pixel intensities.
Attempts:
2 left
💡 Hint

Think about how to compare predicted and actual motion regions.

🔧 Debug
expert
3:00remaining
Why does this motion detection code fail to detect motion correctly?

Review this Python code snippet for motion detection:

import cv2
cap = cv2.VideoCapture('video.mp4')
ret, frame1 = cap.read()
while True:
    ret, frame2 = cap.read()
    diff = cv2.absdiff(frame1, frame2)
    gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
    _, thresh = cv2.threshold(gray, 30, 255, cv2.THRESH_BINARY)
    count = cv2.countNonZero(thresh)
    if count > 5000:
        print('Motion detected')
    frame1 = frame2
    if cv2.waitKey(30) & 0xFF == 27:
        break
cap.release()

Why might this code miss some motion events?

AThe code does not convert frames to grayscale before differencing.
BThe threshold value is too low, causing false positives.
CIt updates <code>frame1</code> incorrectly inside the loop.
DIt does not handle the case when <code>ret</code> is False, causing errors on last frames.
Attempts:
2 left
💡 Hint

Check what happens when the video ends or frame reading fails.

Practice

(1/5)
1. What is the main goal of motion detection in computer vision?
easy
A. To increase the brightness of images
B. To find changes between video frames indicating movement
C. To convert color images to grayscale
D. To compress video files for storage

Solution

  1. Step 1: Understand motion detection purpose

    Motion detection compares video frames to identify changes that show movement.
  2. Step 2: Eliminate unrelated options

    Brightness increase, grayscale conversion, and compression are not about detecting motion.
  3. Final Answer:

    To find changes between video frames indicating movement -> Option B
  4. Quick Check:

    Motion detection = find changes [OK]
Hint: Motion detection = spotting changes between frames [OK]
Common Mistakes:
  • Confusing motion detection with image enhancement
  • Thinking motion detection compresses videos
  • Mixing grayscale conversion with motion detection
2. Which of the following is the correct order of steps in basic motion detection?
easy
A. Blur, grayscale, difference, threshold, contours
B. Difference, blur, grayscale, contours, threshold
C. Grayscale, blur, difference, threshold, contours
D. Contours, threshold, difference, blur, grayscale

Solution

  1. 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.
  2. Step 2: Match the correct sequence

    Grayscale, blur, difference, threshold, contours matches this exact order.
  3. Final Answer:

    Grayscale, blur, difference, threshold, contours -> Option C
  4. Quick Check:

    Step order = grayscale -> blur -> difference -> threshold -> contours [OK]
Hint: Remember: grayscale first, then blur, difference, threshold, contours [OK]
Common Mistakes:
  • Mixing up the order of blur and difference
  • Applying contours before threshold
  • Skipping grayscale conversion
3. Given the following Python code snippet for motion detection, what will be the output type of 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]
medium
A. A grayscale image with pixel values 0 or 255
B. A color image with three channels
C. A single integer threshold value
D. A list of contour points

Solution

  1. 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).
  2. Step 2: Analyze the code

    Since diff is grayscale, thresholding produces a binary grayscale image, not color or contours.
  3. Final Answer:

    A grayscale image with pixel values 0 or 255 -> Option A
  4. Quick Check:

    Threshold output = binary grayscale image [OK]
Hint: cv2.threshold returns binary grayscale image, not color or contours [OK]
Common Mistakes:
  • Thinking threshold returns contours
  • Assuming output is color image
  • Confusing threshold value with output image
4. You wrote this code for motion detection but it raises an error:
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?
medium
A. cv2.RETR_EXTERNAL is an invalid flag
B. Threshold function returns only one value, not a tuple
C. findContours requires color images, not binary
D. Frames are color images; absdiff needs grayscale images

Solution

  1. Step 1: Check input types for absdiff

    cv2.absdiff expects images of the same type and single channel for motion detection, usually grayscale.
  2. Step 2: Identify missing grayscale conversion

    Frames are likely color images; missing conversion to grayscale causes error in absdiff.
  3. Final Answer:

    Frames are color images; absdiff needs grayscale images -> Option D
  4. Quick Check:

    absdiff input = grayscale images [OK]
Hint: Convert frames to grayscale before absdiff to avoid errors [OK]
Common Mistakes:
  • Assuming threshold returns single value only
  • Thinking findContours needs color images
  • Using invalid flags for findContours
5. You want to detect motion only when a moving object covers at least 500 pixels in the frame. Which step should you add after finding contours to achieve this?
hard
A. Filter contours by area and ignore those smaller than 500 pixels
B. Apply a stronger blur to the grayscale image
C. Increase the threshold value to 500
D. Convert the frame to color before contour detection

Solution

  1. Step 1: Understand contour filtering

    Contours represent detected shapes; filtering by area removes small noise.
  2. Step 2: Apply area filter after contours

    Ignoring contours smaller than 500 pixels ensures only significant motion is detected.
  3. Final Answer:

    Filter contours by area and ignore those smaller than 500 pixels -> Option A
  4. Quick Check:

    Filter contours by area ≥ 500 pixels [OK]
Hint: Filter contours by size to detect significant motion only [OK]
Common Mistakes:
  • Confusing threshold value with pixel area
  • Applying blur instead of filtering contours
  • Converting to color unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard