Bird
Raised Fist0
Computer Visionml~20 mins

Python CV ecosystem (OpenCV, PIL, torchvision) in Computer Vision - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Python CV Ecosystem Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this OpenCV image shape code?
Given an image loaded with OpenCV, what will be the output of the shape attribute?
Computer Vision
import cv2
img = cv2.imread('sample.jpg')
print(img.shape)
A(height, width, channels) tuple representing image dimensions
B(width, height, channels) tuple representing image dimensions
CA single integer representing total pixels
DRaises an error because shape attribute does not exist
Attempts:
2 left
💡 Hint
OpenCV loads images as NumPy arrays with shape (rows, columns, channels).
Model Choice
intermediate
2:00remaining
Which torchvision model is best for image classification on ImageNet?
You want to use a pretrained model from torchvision for classifying images into 1000 classes. Which model is designed specifically for this?
Atorchvision.models.segmentation.fcn_resnet50(pretrained=True)
Btorchvision.models.resnet50(pretrained=True)
Ctorchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
Dtorchvision.models.video.r3d_18(pretrained=True)
Attempts:
2 left
💡 Hint
Image classification models output class probabilities for fixed classes.
Metrics
advanced
2:00remaining
How to correctly compute accuracy for a torchvision classification model?
You have model outputs as logits and true labels as integers. Which code snippet correctly computes accuracy?
A
preds = outputs.argmax(dim=1)
accuracy = (preds == labels).sum().item()
B
preds = outputs.max(dim=0)
accuracy = (preds == labels).float().mean().item()
C
preds = outputs.argmax(dim=1)
accuracy = (preds == labels).float().mean().item()
D
preds = outputs.argmax(dim=1)
accuracy = (preds == labels).float().sum().item() / len(labels)
Attempts:
2 left
💡 Hint
Accuracy is the fraction of correct predictions over total samples.
🔧 Debug
advanced
2:00remaining
Why does this PIL image conversion code raise an error?
Code snippet: from PIL import Image img = Image.open('photo.png') img = img.convert('HSV')
Computer Vision
from PIL import Image
img = Image.open('photo.png')
img = img.convert('HSV')
ANo error, image converts successfully
BFileNotFoundError because 'photo.png' does not exist
CValueError because 'HSV' is not a supported mode in PIL
DTypeError because convert expects an integer
Attempts:
2 left
💡 Hint
PIL supports specific modes like 'RGB', 'L', 'RGBA'.
🧠 Conceptual
expert
2:00remaining
What is the main difference between OpenCV and PIL in image processing?
Choose the statement that best describes a key difference between OpenCV and PIL libraries.
APIL is designed for real-time computer vision, OpenCV is not
BPIL supports video processing, OpenCV does not
COpenCV cannot read PNG images, PIL can
DOpenCV uses BGR color order by default, PIL uses RGB
Attempts:
2 left
💡 Hint
Think about default color channel order in each library.

Practice

(1/5)
1. Which Python library is best known for fast image and video processing tasks?
easy
A. PIL (Pillow)
B. OpenCV
C. torchvision
D. matplotlib

Solution

  1. Step 1: Understand library purposes

    OpenCV is designed for fast image and video processing, widely used in computer vision.
  2. Step 2: Compare with other libraries

    PIL is mainly for image editing, torchvision is for ML image datasets, matplotlib is for plotting.
  3. Final Answer:

    OpenCV -> Option B
  4. Quick Check:

    Fast image/video processing = OpenCV [OK]
Hint: OpenCV = fast image/video tasks, PIL = editing, torchvision = ML prep [OK]
Common Mistakes:
  • Confusing PIL as the fastest for video processing
  • Thinking torchvision handles video processing
  • Assuming matplotlib is for image processing
2. Which of the following is the correct way to read an image using OpenCV in Python?
easy
A. img = cv2.imread('image.jpg')
B. img = Image.open('image.jpg')
C. img = torchvision.io.read_image('image.jpg')
D. img = plt.imread('image.jpg')

Solution

  1. Step 1: Identify OpenCV image reading syntax

    OpenCV uses cv2.imread() to load images from files.
  2. Step 2: Differentiate from other libraries

    PIL uses Image.open(), torchvision uses torchvision.io.read_image(), matplotlib uses plt.imread().
  3. Final Answer:

    img = cv2.imread('image.jpg') -> Option A
  4. Quick Check:

    OpenCV image read = cv2.imread() [OK]
Hint: OpenCV reads images with cv2.imread() [OK]
Common Mistakes:
  • Using Image.open() which is from PIL, not OpenCV
  • Using plt.imread() which is for plotting, not OpenCV
  • Confusing torchvision's read_image with OpenCV
3. What will be the shape and color format of the image loaded by this OpenCV code?
import cv2
img = cv2.imread('image.jpg')
print(img.shape)
medium
A. (height, width, 3) with RGB color order
B. (width, height, 3) with BGR color order
C. (width, height, 3) with RGB color order
D. (height, width, 3) with BGR color order

Solution

  1. Step 1: Understand OpenCV image shape

    OpenCV loads images as NumPy arrays with shape (height, width, channels).
  2. Step 2: Know OpenCV color format

    OpenCV uses BGR color order by default, not RGB.
  3. Final Answer:

    (height, width, 3) with BGR color order -> Option D
  4. Quick Check:

    OpenCV shape = (H, W, 3), color = BGR [OK]
Hint: OpenCV images: shape (H,W,3), color BGR [OK]
Common Mistakes:
  • Assuming RGB color order instead of BGR
  • Swapping width and height in shape
  • Thinking OpenCV loads grayscale by default
4. This code tries to convert a PIL image to a NumPy array for OpenCV processing but causes an error:
from PIL import Image
import numpy as np
img_pil = Image.open('image.jpg')
img_cv = np.array(img_pil)

What is the likely cause and fix?
medium
A. PIL image must be converted to grayscale first
B. Color channels are in wrong order; convert RGB to BGR after np.array()
C. Use img_pil.convert('RGB') before np.array() to ensure 3 channels
D. No error; code works fine as is

Solution

  1. Step 1: Identify PIL image mode issue

    PIL images may not be in RGB mode by default; could be 'P' or 'L' mode causing np.array to have unexpected shape.
  2. Step 2: Fix by converting to RGB mode

    Use img_pil.convert('RGB') to ensure 3 color channels before converting to NumPy array.
  3. Final Answer:

    Use img_pil.convert('RGB') before np.array() to ensure 3 channels -> Option C
  4. Quick Check:

    PIL to NumPy needs RGB mode [OK]
Hint: Convert PIL image to RGB before np.array() [OK]
Common Mistakes:
  • Assuming np.array always works without convert()
  • Ignoring color channel order differences
  • Trying to convert to grayscale unnecessarily
5. You want to prepare an image for a PyTorch model using torchvision transforms. Which sequence correctly converts a PIL image to a tensor normalized for pretrained models?
hard
A. Use torchvision.transforms.ToTensor() then torchvision.transforms.Normalize(mean, std)
B. Use cv2.imread() then convert to tensor manually
C. Use PIL.Image.open() then convert to NumPy array and normalize manually
D. Use torchvision.transforms.Normalize() only on PIL image

Solution

  1. Step 1: Understand torchvision transform pipeline

    To prepare images for PyTorch models, convert PIL image to tensor with ToTensor(), which scales pixels to [0,1].
  2. Step 2: Normalize tensor with mean and std

    Use Normalize() with pretrained model's mean and std to standardize input.
  3. Final Answer:

    Use torchvision.transforms.ToTensor() then torchvision.transforms.Normalize(mean, std) -> Option A
  4. Quick Check:

    ToTensor + Normalize = correct PyTorch prep [OK]
Hint: Use ToTensor() then Normalize() for PyTorch image prep [OK]
Common Mistakes:
  • Trying to normalize PIL images directly
  • Using OpenCV images without conversion
  • Skipping normalization step