Bird
0
0

Identify the error in this motion detection code snippet:

medium📝 Debug Q6 of 15
Raspberry Pi - Camera Module
Identify the error in this motion detection code snippet:
import cv2
cap = cv2.VideoCapture(0)
ret, frame1 = cap.read()
ret, frame2 = cap.read()
diff = cv2.absdiff(frame1, frame2)
gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
ANo error, code is correct
Bcv2.absdiff requires grayscale images, not color
Ccv2.cvtColor should use COLOR_RGB2GRAY instead of COLOR_BGR2GRAY
Dcap.read() should be called only once
Step-by-Step Solution
Solution:
  1. Step 1: Check cv2.absdiff input

    It accepts color images and returns a color difference image.
  2. Step 2: cv2.cvtColor usage

    Converting BGR to grayscale is standard since OpenCV reads images in BGR format.
  3. Step 3: cap.read() usage

    Calling cap.read() twice to get two frames is correct for motion detection.
  4. Final Answer:

    No error, code is correct -> Option A
  5. Quick Check:

    OpenCV uses BGR by default [OK]
Quick Trick: OpenCV images are BGR by default, so COLOR_BGR2GRAY is correct [OK]
Common Mistakes:
MISTAKES
  • Assuming OpenCV uses RGB instead of BGR
  • Thinking absdiff only works on grayscale images
  • Believing cap.read() should be called once for motion detection

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes