Adv-Python Unit 2: Questions with Answers
Unit II: Video Processing using OpenCV -> Generated and Prepared By Thiruselvan (ThiruXD)
A. Multiple Choice Questions (MCQs)
Easy Level
1. A video is a sequence of:
a) Pixels
b) Frames
c) Codecs
d) Channels
Answer: b) Frames
2. Which function is used to open a video file or webcam in OpenCV?
a) cv2.VideoOpen()
b) cv2.VideoCapture()
c) cv2.ReadVideo()
d) cv2.Capture()
Answer: b) cv2.VideoCapture()
3. What does ret represent in ret, frame = cap.read()?
a) Resolution of the frame
b) Boolean value indicating if the frame was read successfully
c) Frame number
d) FPS of the video
Answer: b) Boolean value indicating if the frame was read successfully
4. To capture video from the default webcam, we use:
a) cv2.VideoCapture(1)
b) cv2.VideoCapture(0)
c) cv2.VideoCapture(-1)
d) cv2.VideoCapture("webcam")
Answer: b) cv2.VideoCapture(0)
5. Which function is used to save a video in OpenCV?
a) cv2.VideoSave()
b) cv2.VideoWriter()
c) cv2.WriteVideo()
d) cv2.imwrite()
Answer: b) cv2.VideoWriter()
Medium Level
6. FourCC stands for:
a) Four Camera Capture Code
b) Four Character Code
c) Frame Codec Control Code
d) Fast OpenCV Codec Code
Answer: b) Four Character Code
7. Which of the following is commonly used to write an .avi video file?
a) cv2.VideoWriter_fourcc(*'mp4v')
b) cv2.VideoWriter_fourcc(*'XVID')
c) cv2.VideoWriter_fourcc(*'H264')
d) cv2.VideoWriter_fourcc(*'PNG')
Answer: b) cv2.VideoWriter_fourcc(*'XVID')
8. In motion detection using frame differencing, the first step is:
a) Finding contours
b) Converting to HSV
c) Finding absolute difference between two frames
d) Applying Gaussian blur only
Answer: c) Finding absolute difference between two frames
9. Which property is used to get the total number of frames in a video?
a) cv2.CAP_PROP_FPS
b) cv2.CAP_PROP_FRAME_COUNT
c) cv2.CAP_PROP_POS_FRAMES
d) cv2.CAP_PROP_FRAME_WIDTH
Answer: b) cv2.CAP_PROP_FRAME_COUNT
10. Drawing functions (line, rectangle, circle, putText) must be called:
a) Only once before the loop
b) Outside the while loop
c) Inside the while loop on every frame
d) After releasing the video
Answer: c) Inside the while loop on every frame
Hard Level
11. What will happen if you forget to write cap.release() and out.release()?
a) Program runs faster
b) Video file may get corrupted or resources are not freed
c) FPS increases automatically
d) No effect
Answer: b) Video file may get corrupted or resources are not freed
12. Which tracker is generally considered the most accurate among OpenCV’s classical trackers?
a) MOSSE
b) KCF
c) CSRT
d) MIL
Answer: c) CSRT
13. You want to jump to the frame at 10 seconds in a video having 30 FPS. Which command is correct?
a) cap.set(cv2.CAP_PROP_POS_FRAMES, 10)
b) cap.set(cv2.CAP_PROP_POS_FRAMES, 300)
c) cap.set(cv2.CAP_PROP_POS_MSEC, 10)
d) cap.set(cv2.CAP_PROP_FPS, 10)
Answer: b) cap.set(cv2.CAP_PROP_POS_FRAMES, 300)
B. Theory / Short Answer Questions
Easy Level
14. What is a video in terms of computer vision?
Answer:
A video is a sequence of images (called frames) displayed rapidly one after another to create the illusion of continuous motion.
15. What is the full form of FPS? Why is it important?
Answer:
FPS stands for Frames Per Second. It tells how many frames are displayed in one second. Higher FPS gives smoother video.
16. Why do we need to check if not cap.isOpened()?
Answer:
To verify whether the video file or camera was successfully opened. If not checked, the program may crash or behave unexpectedly when trying to read frames.
Medium Level
17. Explain the difference between reading a video from a file and capturing from a webcam.
Answer:
- Video file: We pass the file path to
VideoCapture(). The video has fixed length and ends when all frames are read (retbecomes False). - Webcam: We pass 0 (or camera index). It captures live continuous stream until manually stopped. We usually use
waitKey(1).
18. What is FourCC? Why is it required while writing a video?
Answer:
FourCC (Four Character Code) is a 4-character code that specifies the codec used for compressing the video.
It is required in VideoWriter so that OpenCV knows which codec to use for saving the video properly.
19. Explain the basic principle of motion detection using frame differencing.
Answer:
Two consecutive frames are subtracted to find the difference. Areas where pixels have changed significantly indicate motion. After thresholding and finding contours, bounding boxes are drawn around moving objects.
Hard Level
20. Differentiate between Object Detection and Object Tracking.
Answer:
- Object Detection: Identifies and locates objects in each frame independently (no memory of previous frames).
- Object Tracking: Once an object is selected/detected, it follows that same object across subsequent frames by updating its position.
21. Why do we apply Gaussian Blur and Dilation in motion detection?
Answer:
- Gaussian Blur: Reduces noise so that small unnecessary differences are ignored.
- Dilation: Increases the white region in the threshold image, making contours more complete and easier to detect.
22. What are the main steps involved in using a tracker (e.g., CSRT) in OpenCV?
Answer:
- Create tracker object
- Read the first frame
- Select ROI (Region of Interest)
- Initialize the tracker with the first frame and ROI
- In a loop: read new frame → update tracker → draw bounding box if successful
C. Analytical / Coding / Problem-Solving Questions
Easy Level
23. Write a Python program to read a video file and display it. Stop the video when ‘q’ is pressed.
Answer:
import cv2
cap = cv2.VideoCapture('video.mp4')
while True:
ret, frame = cap.read()
if not ret:
break
cv2.imshow('Video', frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()24. Write code to display live webcam feed with current date and time on it.
Answer:
import cv2
import datetime
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
break
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
cv2.putText(frame, now, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 255), 2)
cv2.imshow('Webcam', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()Medium Level
25. Write a program to record video from webcam and save it as output.avi.
Answer:
import cv2
cap = cv2.VideoCapture(0)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (width, height))
while True:
ret, frame = cap.read()
if not ret:
break
out.write(frame)
cv2.imshow('Recording', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()26. Explain what the following code does and what will be the output video.
cap = cv2.VideoCapture('input.mp4')
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
while True:
ret, frame = cap.read()
if not ret:
break
frame = cv2.resize(frame, (640, 480))
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
out.write(gray) # Is this correct?Answer:
The code tries to save a resized grayscale version of the video.
Problem: VideoWriter is initialized for color frames (3 channels), but grayscale image has only 1 channel. This will cause an error or produce a corrupted video.
Correct way: Convert gray back to BGR before writing → gray_bgr = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
Hard Level
27. Analytical Question:
You are developing a security surveillance system.
a) Which technique will you use to detect any movement in the camera feed?
b) Write the important steps of that technique.
c) How will you avoid detecting very small movements (like noise or flying insects)?
Answer:
a) Frame Differencing based Motion Detection (or Background Subtraction).
b) Steps:
- Capture two consecutive frames
- Find absolute difference
- Convert to grayscale
- Apply Gaussian blur
- Threshold the image
- Dilate the threshold image
- Find contours
- Draw rectangles on large contours
c) By setting a minimum contour area threshold (e.g., if cv2.contourArea(contour) < 900: continue)
28. A video has 30 FPS and total duration of 2 minutes.
a) Calculate total number of frames.
b) Write the code to jump directly to the frame at 1 minute 15 seconds.
Answer:
a) Total frames = 30 FPS × 120 seconds = 3600 frames
b)
cap = cv2.VideoCapture('video.mp4')
frame_number = 30 * 75 # 1 min 15 sec = 75 seconds
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_number)
ret, frame = cap.read()29. Write a complete program for basic object tracking using CSRT tracker. User should select the object using mouse.
Answer:
import cv2
tracker = cv2.TrackerCSRT_create()
cap = cv2.VideoCapture('video.mp4')
ret, frame = cap.read()
bbox = cv2.selectROI("Select Object", frame, False)
tracker.init(frame, bbox)
while True:
ret, frame = cap.read()
if not ret:
break
success, bbox = tracker.update(frame)
if success:
x, y, w, h = [int(v) for v in bbox]
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.putText(frame, "Tracking", (x, y-10),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
else:
cv2.putText(frame, "Lost", (50, 80),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
cv2.imshow("Tracking", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()Quick Exam Tips for Unit II
- Always mention
retand resource release (release()) in coding answers. - Motion Detection = Absolute Difference → Gray → Blur → Threshold → Dilate → Contours
- Tracking needs
selectROI+init+update waitKey(1)→ live camera,waitKey(25)→ video file- FourCC is mandatory when using
VideoWriter