Complete the code to read a video frame using OpenCV.
ret, frame = cap.[1]()The read() method reads the next frame from the video capture object.
Complete the code to convert a frame to grayscale for motion detection.
gray = cv2.[1](frame, cv2.COLOR_BGR2GRAY)The cvtColor() function converts images between color spaces, here from BGR to grayscale.
Fix the error in the code to compute the absolute difference between two frames.
diff = cv2.[1](frame1, frame2)The correct OpenCV function to compute absolute difference is absdiff() all lowercase.
Fill both blanks to threshold the difference image and find contours.
ret, thresh = cv2.[1](diff, 25, 255, cv2.[2]) contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
threshold() applies a fixed threshold, and THRESH_BINARY is the thresholding type to create a binary image.
Fill all three blanks to draw rectangles around detected motion areas.
for contour in contours: if cv2.contourArea(contour) > [1]: (x, y, w, h) = cv2.[2](contour) cv2.[3](frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
We filter small contours with area > 500, get bounding rectangles with boundingRect(), and draw rectangles with rectangle().