Bird
Raised Fist0
Computer Visionml~20 mins

First image processing program 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
🎖️
Image Processing 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 image processing code?
Consider this Python code that loads an image, converts it to grayscale, and prints the shape of the resulting image array. What will be printed?
Computer Vision
import cv2
image = cv2.imread('sample.jpg')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
print(gray_image.shape)
A(width, height)
B(height, width, 3)
CError: cv2.cvtColor requires 3-channel input
D(height, width)
Attempts:
2 left
💡 Hint
Grayscale images have only one channel, so the shape has two dimensions.
Model Choice
intermediate
2:00remaining
Which model is best for simple image classification?
You want to classify images of cats and dogs with a small dataset. Which model is the best choice?
AA decision tree using image file names as features
BA simple logistic regression model using raw pixel values
CA pretrained convolutional neural network with transfer learning
DA deep convolutional neural network with many layers trained from scratch
Attempts:
2 left
💡 Hint
Using pretrained models helps when data is limited.
Hyperparameter
advanced
2:00remaining
Which hyperparameter change will most reduce overfitting in image classification?
You trained a CNN that performs well on training images but poorly on new images. Which hyperparameter adjustment will most likely reduce overfitting?
AAdd dropout layers with a dropout rate of 0.5
BIncrease the number of convolutional layers
CDecrease the batch size to 1
DRemove data augmentation
Attempts:
2 left
💡 Hint
Dropout randomly disables neurons during training to prevent memorization.
Metrics
advanced
2:00remaining
Which metric is best to evaluate image segmentation quality?
You built a model to segment objects in images. Which metric best measures how well the predicted segments match the true segments?
AAccuracy
BIntersection over Union (IoU)
CMean Squared Error
DPrecision
Attempts:
2 left
💡 Hint
This metric compares the overlap between predicted and true segments.
🔧 Debug
expert
2:00remaining
What error does this image processing code raise?
This code tries to resize an image but raises an error. What is the error?
Computer Vision
import cv2
image = cv2.imread('nonexistent.jpg')
resized = cv2.resize(image, (100, 100))
Acv2.error: OpenCV(…) error: (-215:Assertion failed) !ssize.empty() in function 'resize'
BFileNotFoundError: No such file or directory: 'nonexistent.jpg'
CTypeError: 'NoneType' object is not subscriptable
DValueError: invalid literal for int() with base 10
Attempts:
2 left
💡 Hint
cv2.imread returns None if the file is missing, causing resize to fail.

Practice

(1/5)
1. What does the OpenCV function imread do in an image processing program?
easy
A. It displays an image on the screen.
B. It reads an image file and loads it into the program.
C. It converts an image from color to grayscale.
D. It saves an image to a file.

Solution

  1. Step 1: Understand the purpose of imread

    The function imread is used to load an image from a file into the program's memory.
  2. Step 2: Differentiate from other functions

    Functions like imshow display images, and cvtColor changes image colors, so they do not read files.
  3. Final Answer:

    It reads an image file and loads it into the program. -> Option B
  4. Quick Check:

    imread = load image [OK]
Hint: imread always loads images from files [OK]
Common Mistakes:
  • Confusing imread with imshow
  • Thinking imread changes image colors
  • Assuming imread saves images
2. Which of the following is the correct syntax to display an image stored in variable img using OpenCV?
easy
A. cv2.display(img)
B. cv2.showimage(img)
C. cv2.show('Window', img)
D. cv2.imshow('Window', img)

Solution

  1. Step 1: Recall the OpenCV display function

    The correct function to show an image is cv2.imshow, which takes a window name and the image variable.
  2. Step 2: Check the syntax of options

    Only cv2.imshow('Window', img) uses cv2.imshow with correct parameters: a string window name and the image.
  3. Final Answer:

    cv2.imshow('Window', img) -> Option D
  4. Quick Check:

    imshow = show image [OK]
Hint: imshow needs a window name and image [OK]
Common Mistakes:
  • Using non-existent functions like display or showimage
  • Forgetting the window name argument
  • Swapping argument order
3. What will be the output of this code snippet?
import cv2
img = cv2.imread('photo.jpg')
print(img.shape)
medium
A. It prints the image pixel values.
B. It raises an error because shape is not valid.
C. It prints the dimensions of the image as (height, width, channels).
D. It prints the file size of 'photo.jpg'.

Solution

  1. Step 1: Understand what img.shape returns

    In OpenCV, img.shape gives the dimensions of the image as a tuple: (height, width, number of color channels).
  2. Step 2: Differentiate from other outputs

    It does not print pixel values or file size, and shape is a valid attribute for images loaded by imread.
  3. Final Answer:

    It prints the dimensions of the image as (height, width, channels). -> Option C
  4. Quick Check:

    img.shape = image size [OK]
Hint: shape shows image size and channels [OK]
Common Mistakes:
  • Expecting pixel data instead of shape
  • Thinking shape is a method, not attribute
  • Confusing file size with image dimensions
4. Identify the error in this code snippet:
import cv2
img = cv2.imread('image.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('Gray Image')
cv2.waitKey(0)
cv2.destroyAllWindows()
medium
A. Missing the image argument in cv2.imshow function.
B. cv2.cvtColor cannot convert color images.
C. cv2.waitKey requires an argument of 1, not 0.
D. cv2.destroyAllWindows should be called before imshow.

Solution

  1. Step 1: Check the usage of cv2.imshow

    The function cv2.imshow requires two arguments: a window name and the image to display. Here, the image argument is missing.
  2. Step 2: Verify other function calls

    cv2.cvtColor correctly converts color images, waitKey(0) waits indefinitely, and destroyAllWindows is correctly placed after showing images.
  3. Final Answer:

    Missing the image argument in cv2.imshow function. -> Option A
  4. Quick Check:

    imshow needs image argument [OK]
Hint: imshow always needs image to show [OK]
Common Mistakes:
  • Forgetting the image argument in imshow
  • Misunderstanding waitKey argument
  • Calling destroyAllWindows too early
5. You want to write a program that reads an image, converts it to grayscale, and then saves the grayscale image. Which sequence of OpenCV functions is correct?
hard
A. cv2.imread() -> cv2.cvtColor() -> cv2.imwrite()
B. cv2.imshow() -> cv2.cvtColor() -> cv2.imwrite()
C. cv2.imread() -> cv2.imshow() -> cv2.cvtColor()
D. cv2.imwrite() -> cv2.imread() -> cv2.cvtColor()

Solution

  1. Step 1: Understand the task steps

    The program must first read the image, then convert it to grayscale, and finally save the new image.
  2. Step 2: Match functions to steps

    cv2.imread() reads the image, cv2.cvtColor() converts color spaces, and cv2.imwrite() saves the image to a file.
  3. Final Answer:

    cv2.imread() -> cv2.cvtColor() -> cv2.imwrite() -> Option A
  4. Quick Check:

    Read -> Convert -> Save = imread, cvtColor, imwrite [OK]
Hint: Read first, convert second, save last [OK]
Common Mistakes:
  • Trying to save before reading
  • Showing image before converting
  • Mixing order of functions