Bird
Raised Fist0
Computer Visionml~20 mins

Reading images (cv2.imread) 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 Reading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
What is the shape of the image read by cv2.imread?
Given the code below, what will be the shape of the variable img if the image is a 640x480 pixel color image?
Computer Vision
import cv2
img = cv2.imread('sample.jpg')
print(img.shape)
A(640, 480)
B(640, 480, 3)
C(480, 640)
D(480, 640, 3)
Attempts:
2 left
💡 Hint
Remember that OpenCV reads images as height x width x channels.
🧠 Conceptual
intermediate
1:00remaining
What does cv2.imread return if the image file does not exist?
If you try to read an image file that does not exist using cv2.imread('missing.jpg'), what will be the value of the returned variable?
ANone
BAn empty numpy array with shape (0,0,3)
CRaises a FileNotFoundError
DAn array filled with zeros of shape (1,1,3)
Attempts:
2 left
💡 Hint
Check what OpenCV returns when it cannot find the file.
Model Choice
advanced
1:30remaining
Which flag reads an image in grayscale using cv2.imread?
You want to read an image as grayscale using cv2.imread. Which flag should you use?
Acv2.IMREAD_GRAYSCALE
Bcv2.IMREAD_COLOR
Ccv2.IMREAD_UNCHANGED
Dcv2.IMREAD_ANYDEPTH
Attempts:
2 left
💡 Hint
Grayscale means one channel, black and white image.
🔧 Debug
advanced
1:30remaining
Why does this code raise an AttributeError?
Consider the code below:
import cv2
img = cv2.imread('image.jpg')
print(img.size())
Why does this code raise an AttributeError?
Computer Vision
import cv2
img = cv2.imread('image.jpg')
print(img.size())
Acv2 module does not have imread function
Bcv2.imread returns None, so None has no size() method
C'size' is an attribute, not a method, so calling img.size() causes AttributeError
DThe image file is corrupted, causing AttributeError
Attempts:
2 left
💡 Hint
Check if size is a method or attribute of numpy arrays.
Metrics
expert
2:00remaining
What is the number of channels in an image read with cv2.IMREAD_UNCHANGED?
If you read a PNG image with transparency using cv2.imread('image.png', cv2.IMREAD_UNCHANGED), how many channels will the resulting image array have?
A3 channels (Blue, Green, Red)
B4 channels (Blue, Green, Red, Alpha)
C1 channel (Grayscale)
DDepends on the image, could be 1 or 3
Attempts:
2 left
💡 Hint
IMREAD_UNCHANGED preserves the alpha channel if present.

Practice

(1/5)
1. What does the function cv2.imread() do in computer vision?
easy
A. Loads an image from a file into a format that can be processed
B. Saves an image to a file
C. Displays an image on the screen
D. Converts an image to grayscale

Solution

  1. Step 1: Understand the purpose of cv2.imread()

    The function cv2.imread() is designed to read image files and load them into memory as arrays for further processing.
  2. Step 2: Differentiate from other functions

    Functions like cv2.imwrite() save images, and cv2.imshow() display images, so they are not the correct answer.
  3. Final Answer:

    Loads an image from a file into a format that can be processed -> Option A
  4. Quick Check:

    cv2.imread() loads images [OK]
Hint: Remember: imread means 'image read' from file [OK]
Common Mistakes:
  • Confusing imread with imwrite (saving images)
  • Thinking imread displays images
  • Assuming imread converts image color
2. Which of the following is the correct way to read an image in grayscale using OpenCV?
easy
A. cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
B. cv2.imread('image.jpg', cv2.IMREAD_COLOR)
C. cv2.imread('image.jpg', cv2.IMREAD_UNCHANGED)
D. cv2.imread('image.jpg')

Solution

  1. Step 1: Identify the flag for grayscale reading

    OpenCV uses cv2.IMREAD_GRAYSCALE to load images in grayscale mode.
  2. Step 2: Compare with other flags

    cv2.IMREAD_COLOR loads color images, cv2.IMREAD_UNCHANGED loads with alpha channel, and no flag defaults to color.
  3. Final Answer:

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

    Grayscale flag = cv2.IMREAD_GRAYSCALE [OK]
Hint: Use cv2.IMREAD_GRAYSCALE flag to read grayscale images [OK]
Common Mistakes:
  • Using default flag which loads color image
  • Confusing IMREAD_COLOR with grayscale
  • Using IMREAD_UNCHANGED when grayscale is needed
3. What will be the output type of the variable img after running this code?
import cv2
img = cv2.imread('sample.png')
medium
A. None, because cv2.imread does not return anything
B. A string containing the image file path
C. A list of image file names
D. A NumPy array representing the image pixels

Solution

  1. Step 1: Understand cv2.imread return type

    cv2.imread loads the image and returns it as a NumPy array containing pixel values.
  2. Step 2: Eliminate other options

    It does not return a string, list, or None. It always returns an array or None if loading fails.
  3. Final Answer:

    A NumPy array representing the image pixels -> Option D
  4. Quick Check:

    cv2.imread returns NumPy array [OK]
Hint: cv2.imread returns image as NumPy array [OK]
Common Mistakes:
  • Thinking it returns file path string
  • Assuming it returns None always
  • Confusing with list of files
4. Consider this code snippet:
import cv2
img = cv2.imread('nonexistent.jpg')
print(img.shape)

What error will occur and why?
medium
A. FileNotFoundError because the file does not exist
B. AttributeError because img is None and None has no attribute 'shape'
C. SyntaxError due to wrong function usage
D. No error, prints image shape

Solution

  1. Step 1: Understand cv2.imread behavior on missing files

    If the file does not exist, cv2.imread returns None instead of an image array.
  2. Step 2: Analyze the print statement

    Trying to access img.shape when img is None causes an AttributeError because NoneType has no attribute 'shape'.
  3. Final Answer:

    AttributeError because img is None and None has no attribute 'shape' -> Option B
  4. Quick Check:

    Missing file -> img=None -> AttributeError on .shape [OK]
Hint: Check if img is None before accessing attributes [OK]
Common Mistakes:
  • Expecting FileNotFoundError instead of None return
  • Ignoring None check before using img
  • Assuming no error occurs
5. You want to load an image with transparency (alpha channel) preserved using OpenCV. Which code snippet correctly does this?
hard
A. img = cv2.imread('image.png', cv2.IMREAD_COLOR)
B. img = cv2.imread('image.png', cv2.IMREAD_GRAYSCALE)
C. img = cv2.imread('image.png', cv2.IMREAD_UNCHANGED)
D. img = cv2.imread('image.png')

Solution

  1. Step 1: Identify flag for loading alpha channel

    cv2.IMREAD_UNCHANGED loads the image as-is, including the alpha transparency channel if present.
  2. Step 2: Compare with other flags

    IMREAD_COLOR ignores alpha, IMREAD_GRAYSCALE loads single channel, and default is color without alpha.
  3. Final Answer:

    img = cv2.imread('image.png', cv2.IMREAD_UNCHANGED) -> Option C
  4. Quick Check:

    Use IMREAD_UNCHANGED to keep transparency [OK]
Hint: Use cv2.IMREAD_UNCHANGED to keep alpha channel [OK]
Common Mistakes:
  • Using IMREAD_COLOR which drops alpha channel
  • Assuming default read keeps transparency
  • Using grayscale flag by mistake