Adv-Python Unit 1: Questions with Answers
Unit I: Introduction to Computer Vision and Video Processing -> Generated and Prepared By Thiruselvan (ThiruXD)
A. Multiple Choice Questions (MCQs)
Easy Level
1. OpenCV stores color images by default in which color order?
a) RGB
b) BGR
c) HSV
d) Grayscale
Answer: b) BGR
2. The origin (0,0) of an image in OpenCV is located at:
a) Bottom-left
b) Center
c) Top-left
d) Top-right
Answer: c) Top-left
3. Which function is used to read an image in OpenCV?
a) cv2.load()
b) cv2.imread()
c) cv2.read()
d) cv2.open()
Answer: b) cv2.imread()
4. A grayscale image has how many channels?
a) 1
b) 2
c) 3
d) 4
Answer: a) 1
5. Which of the following is the correct way to display an image?
a) cv2.show(img)
b) cv2.imshow("Window", img)
c) cv2.display(img)
d) plt.imshow(img) only
Answer: b) cv2.imshow("Window", img)
Medium Level
6. What does img.shape return for a color image?
a) (width, height)
b) (height, width)
c) (height, width, channels)
d) (channels, height, width)
Answer: c) (height, width, channels)
7. Which color space is most suitable for color-based object segmentation under varying lighting?
a) BGR
b) RGB
c) HSV
d) Grayscale
Answer: c) HSV
8. To convert a BGR image to grayscale, the correct flag is:
a) cv2.COLOR_RGB2GRAY
b) cv2.COLOR_BGR2GRAY
c) cv2.COLOR_GRAY2BGR
d) cv2.COLOR_HSV2GRAY
Answer: b) cv2.COLOR_BGR2GRAY
9. The command img[100:200, 50:150] performs:
a) Resizing
b) Cropping (ROI extraction)
c) Rotation
d) Flipping
Answer: b) Cropping (ROI extraction)
10. Which of the following is not a feature of OpenCV?
a) Real-time image processing
b) Deep learning model training from scratch
c) Video capture and writing
d) Feature detection (ORB, SIFT)
Answer: b) Deep learning model training from scratch
(OpenCV DNN can run pre-trained models, but does not train models from scratch like TensorFlow/PyTorch)
Hard Level
11. An image of shape (480, 640, 3) and dtype uint8 occupies approximately how much memory?
a) 300 KB
b) 900 KB
c) 1.2 MB
d) 2.4 MB
Answer: b) 900 KB
Calculation: 480 × 640 × 3 = 921,600 bytes ≈ 900 KB
12. What will be the output of the following code?
img = np.zeros((300, 400, 3), dtype=np.uint8)
img[100:200, 150:250] = [0, 255, 0]
print(img[150, 200])a) [0, 0, 0]
b) [0, 255, 0]
c) [255, 0, 0]
d) Error
Answer: b) [0, 255, 0]
13. Which statement is true regarding image coordinates in OpenCV?
a) x increases downward, y increases rightward
b) Both x and y increase from bottom-left
c) x increases rightward, y increases downward
d) Origin is at the center of the image
Answer: c) x increases rightward, y increases downward
B. Theory / Short Answer Questions
Easy Level
14. Define Computer Vision. How is it different from Image Processing?
Answer:
Computer Vision is the field of AI that enables machines to interpret and understand visual data from the world.
Image Processing mainly focuses on enhancing or transforming images, while Computer Vision focuses on extracting meaningful information and making decisions from images/videos.
15. What is a pixel?
Answer:
A pixel (Picture Element) is the smallest unit of a digital image. It stores intensity (and color) information.
16. Name any four important applications of Computer Vision.
Answer:
- Face detection & recognition
- Autonomous vehicles
- Medical image analysis
- Industrial quality inspection (Also acceptable: surveillance, AR/VR, OCR, agriculture, etc.)
Medium Level
17. Explain the BGR color model used in OpenCV. Why does OpenCV use BGR instead of RGB?
Answer:
In the BGR model, the three channels are ordered as Blue, Green, Red.
OpenCV uses BGR for historical reasons — early camera manufacturers and the original Intel developers used BGR ordering. Most other libraries (Matplotlib, PIL, TensorFlow) use RGB, so conversion is often required.
18. What information does img.shape, img.dtype, and img.size provide?
Answer:
img.shape→ (height, width, channels)img.dtype→ data type of pixel values (usually uint8)img.size→ total number of elements (height × width × channels)
19. Differentiate between spatial resolution and bit depth of an image.
Answer:
- Spatial resolution refers to the number of pixels (e.g., 1920×1080). Higher spatial resolution means more detail.
- Bit depth refers to the number of bits used to represent each pixel’s intensity (e.g., 8-bit = 256 levels). Higher bit depth means more intensity levels and smoother gradients.
Hard Level
20. Explain the HSV color space and its advantages over BGR/RGB for computer vision tasks.
Answer:
HSV stands for Hue, Saturation, Value.
- Hue represents the pure color (0–179 in OpenCV).
- Saturation represents the purity/intensity of the color.
- Value represents brightness.
Advantages:
- Separates color information (Hue) from lighting conditions (Value).
- Makes color-based segmentation much more robust to changes in illumination compared to BGR/RGB.
21. Why is it important to check whether cv2.imread() returned None? What problems can occur if this check is skipped?
Answer:
If the file path is wrong or the image is corrupted, cv2.imread() returns None.
If we proceed without checking, subsequent operations (like accessing .shape or displaying) will raise errors such as AttributeError: 'NoneType' object has no attribute 'shape', making debugging difficult.
C. Analytical / Programming / Problem-Solving Questions
Easy Level
22. Write a Python program to:
- Read a color image
- Convert it to grayscale
- Display both images
- Save the grayscale image
Answer:
import cv2
img = cv2.imread("image.jpg")
if img is None:
print("Error loading image")
else:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("Original", img)
cv2.imshow("Grayscale", gray)
cv2.imwrite("gray_image.jpg", gray)
cv2.waitKey(0)
cv2.destroyAllWindows()Medium Level
23. An image has dimensions 800 × 600 (width × height).
Write the code to:
a) Crop the central 400 × 300 region
b) Resize the cropped region to 200 × 150
c) Draw a red rectangle around the original crop region on the original image
Answer:
import cv2
img = cv2.imread("image.jpg")
h, w = img.shape[:2]
# Central crop
x1 = (w - 400) // 2
y1 = (h - 300) // 2
cropped = img[y1:y1+300, x1:x1+400]
# Resize
resized = cv2.resize(cropped, (200, 150))
# Draw rectangle on original
cv2.rectangle(img, (x1, y1), (x1+400, y1+300), (0, 0, 255), 2)
cv2.imshow("Original with ROI", img)
cv2.imshow("Resized Crop", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()24. Explain what happens step-by-step in the following code and predict the final color of the center pixel.
img = np.zeros((200, 200, 3), dtype=np.uint8)
img[50:150, 50:150] = [255, 0, 0]
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
print(img[100, 100])Answer:
- Creates a black 200×200 color image.
- Sets a 100×100 square region to Blue (because OpenCV uses BGR → [255,0,0] means Blue).
- Converts the image from BGR to RGB.
- After conversion, the blue region becomes Red in RGB format.
Final output:
[255, 0, 0](Red)
Hard Level
25. Analytical Problem:
You are given a noisy outdoor image captured in varying sunlight. You need to detect red-colored objects robustly.
a) Which color space will you convert the image into and why?
b) What range of values would you typically use for the red color in that space?
c) Write the complete code snippet to create a binary mask for red objects.
Answer:
a) HSV color space — because Hue separates pure color information from intensity (Value), making detection more robust to lighting changes.
b) Red color wraps around 0 in OpenCV HSV:
- Lower red: Hue 0–10
- Upper red: Hue 170–180 Saturation and Value usually kept high (e.g., 100–255).
c)
import cv2
import numpy as np
img = cv2.imread("image.jpg")
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# Lower red
lower1 = np.array([0, 100, 100])
upper1 = np.array([10, 255, 255])
mask1 = cv2.inRange(hsv, lower1, upper1)
# Upper red
lower2 = np.array([170, 100, 100])
upper2 = np.array([180, 255, 255])
mask2 = cv2.inRange(hsv, lower2, upper2)
mask = mask1 | mask2
result = cv2.bitwise_and(img, img, mask=mask)26. An image of shape (1080, 1920, 3) is loaded. You perform the following operations:
- Convert to grayscale
- Resize to half the original dimensions
- Crop the top-left 200×200 region
What will be the shape of the final image? Show calculations.
Answer:
- Original: (1080, 1920, 3)
- After grayscale: (1080, 1920)
- After resize (half): (540, 960)
- After cropping top-left 200×200: (200, 200)
Final shape: (200, 200)
Quick Revision
- Always remember: OpenCV → BGR, NumPy → first index is row (y)
img.shape→ (height, width, channels)- Prefer HSV for color detection
- Always check
if img is None - Cropping is done using NumPy slicing:
img[y1:y2, x1:x2]