Bird
Raised Fist0
Computer Visionml~20 mins

Displaying images (cv2.imshow, matplotlib) 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 Display Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What does this OpenCV code display?
Consider the following Python code using OpenCV to display an image. What will the window show when this code runs?
Computer Vision
import cv2
import numpy as np

img = np.zeros((100, 100, 3), dtype=np.uint8)
img[25:75, 25:75] = [0, 255, 0]  # green square in the center
cv2.imshow('Test Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
AA white window with a red square in the center
BA white window with a green square in the center
CA black window with a red square in the center
DA black window with a green square in the center
Attempts:
2 left
💡 Hint
Remember that np.zeros creates a black image and the color is in BGR format.
Predict Output
intermediate
2:00remaining
What color does matplotlib show for this image array?
Given this code using matplotlib to display an image, what color will the square appear?
Computer Vision
import matplotlib.pyplot as plt
import numpy as np

img = np.zeros((100, 100, 3), dtype=np.uint8)
img[25:75, 25:75] = [0, 0, 255]  # red square in BGR
plt.imshow(img)
plt.show()
AA black window with a blue square in the center
BA black window with a green square in the center
CA black window with a red square in the center
DA black window with a yellow square in the center
Attempts:
2 left
💡 Hint
Matplotlib expects RGB format, but the array is in BGR.
Model Choice
advanced
2:00remaining
Choosing the right method to display images in a real-time video feed
You want to display frames from a live webcam feed with minimal delay and allow keyboard interaction to stop the feed. Which method is best?
AUse cv2.imshow with cv2.waitKey inside a loop
BUse print() to output pixel values to the console
CSave each frame as an image file and open it externally
DUse matplotlib.pyplot.imshow inside a loop with plt.show()
Attempts:
2 left
💡 Hint
Consider speed and ability to handle keyboard events.
Metrics
advanced
2:00remaining
Understanding the effect of cv2.waitKey argument on image display
What happens if you call cv2.waitKey(0) versus cv2.waitKey(1) after cv2.imshow?
ABoth wait indefinitely for a key press
Bcv2.waitKey(0) waits 0 ms; cv2.waitKey(1) waits indefinitely
Ccv2.waitKey(0) waits indefinitely for a key press; cv2.waitKey(1) waits 1 ms and continues
DBoth wait 1 ms and then continue
Attempts:
2 left
💡 Hint
Think about how waitKey controls the pause duration.
🔧 Debug
expert
3:00remaining
Why does this matplotlib image display show a distorted color?
You run this code to display an image loaded by OpenCV, but the colors look wrong. What is the cause?
Computer Vision
import cv2
import matplotlib.pyplot as plt

img = cv2.imread('image.jpg')
plt.imshow(img)
plt.show()
AMatplotlib cannot display images loaded from OpenCV
BOpenCV loads images in BGR format but matplotlib expects RGB, causing color distortion
CThe image file is corrupted, causing wrong colors
Dcv2.imread returns grayscale images by default, causing color issues
Attempts:
2 left
💡 Hint
Check the color channel order between OpenCV and matplotlib.

Practice

(1/5)
1. What is the main purpose of using cv2.imshow in computer vision?
easy
A. To resize an image
B. To save an image to disk
C. To convert an image from BGR to RGB
D. To open a window that displays an image

Solution

  1. Step 1: Understand the function of cv2.imshow

    cv2.imshow is used to open a new window that shows the image you provide.
  2. Step 2: Differentiate from other functions

    Saving images uses cv2.imwrite, color conversion uses cv2.cvtColor, and resizing uses cv2.resize.
  3. Final Answer:

    To open a window that displays an image -> Option D
  4. Quick Check:

    cv2.imshow shows images in a window [OK]
Hint: cv2.imshow always opens a window to show images [OK]
Common Mistakes:
  • Confusing cv2.imshow with saving or converting images
  • Forgetting that cv2.imshow opens a separate window
  • Thinking cv2.imshow changes image data
2. Which of the following is the correct sequence to display an image using OpenCV in Python?
easy
A. cv2.imshow(), cv2.waitKey(), cv2.destroyAllWindows()
B. cv2.waitKey(), cv2.imshow(), cv2.destroyAllWindows()
C. cv2.destroyAllWindows(), cv2.imshow(), cv2.waitKey()
D. cv2.imshow(), cv2.destroyAllWindows(), cv2.waitKey()

Solution

  1. Step 1: Recall the correct order of OpenCV display functions

    First, cv2.imshow() opens the image window, then cv2.waitKey() waits for a key press, and finally cv2.destroyAllWindows() closes the window.
  2. Step 2: Check the options order

    Only cv2.imshow(), cv2.waitKey(), cv2.destroyAllWindows() follows this correct sequence.
  3. Final Answer:

    cv2.imshow(), cv2.waitKey(), cv2.destroyAllWindows() -> Option A
  4. Quick Check:

    Display, wait, then close windows [OK]
Hint: Always call waitKey after imshow before destroying windows [OK]
Common Mistakes:
  • Calling destroyAllWindows before waitKey
  • Not calling waitKey causing window to close immediately
  • Mixing order of functions
3. What will be the output of this code snippet?
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('image.jpg')
plt.imshow(img)
plt.show()
medium
A. The image displays with correct colors
B. The image displays but colors look incorrect (blue and red swapped)
C. The code throws an error because plt.imshow cannot display images
D. The image window opens but closes immediately

Solution

  1. Step 1: Understand color format difference

    OpenCV loads images in BGR format, but matplotlib expects RGB format.
  2. Step 2: Effect on plt.imshow

    Displaying BGR image directly with plt.imshow causes colors to appear swapped, especially red and blue.
  3. Final Answer:

    The image displays but colors look incorrect (blue and red swapped) -> Option B
  4. Quick Check:

    OpenCV BGR images show wrong colors in matplotlib [OK]
Hint: Convert BGR to RGB before plt.imshow to fix colors [OK]
Common Mistakes:
  • Assuming plt.imshow shows correct colors without conversion
  • Confusing BGR and RGB formats
  • Expecting plt.imshow to throw error on BGR images
4. You wrote this code to display an image but the window closes immediately. What is the error?
import cv2
img = cv2.imread('photo.png')
cv2.imshow('Photo', img)
cv2.destroyAllWindows()
medium
A. Missing cv2.waitKey() call after cv2.imshow()
B. cv2.destroyAllWindows() should be before cv2.imshow()
C. cv2.imread() cannot read PNG files
D. Window name 'Photo' is invalid

Solution

  1. Step 1: Identify missing waitKey()

    Without cv2.waitKey(), the window opens and closes immediately because the program does not wait for a key press.
  2. Step 2: Confirm other options are incorrect

    Destroying windows before showing is wrong, cv2.imread supports PNG, and window names can be any string.
  3. Final Answer:

    Missing cv2.waitKey() call after cv2.imshow() -> Option A
  4. Quick Check:

    Always call waitKey to pause window [OK]
Hint: Always add cv2.waitKey() after imshow to keep window open [OK]
Common Mistakes:
  • Forgetting waitKey causes window to close instantly
  • Thinking destroyAllWindows controls window display timing
  • Assuming cv2.imread can't read PNG images
5. You want to display an image inside a Jupyter notebook using matplotlib with correct colors. Which code snippet is correct?
hard
A. import cv2 import matplotlib.pyplot as plt img = cv2.imread('img.jpg') plt.imshow(img) plt.show()
B. import matplotlib.pyplot as plt img = cv2.imread('img.jpg') plt.imshow(img) plt.show()
C. import cv2 import matplotlib.pyplot as plt img = cv2.imread('img.jpg') img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) plt.imshow(img_rgb) plt.show()
D. import cv2 img = cv2.imread('img.jpg') cv2.imshow('Image', img) cv2.waitKey(0) cv2.destroyAllWindows()

Solution

  1. Step 1: Understand color format for matplotlib display

    OpenCV loads images in BGR format, but matplotlib expects RGB, so conversion is needed.
  2. Step 2: Check each option

    import cv2 import matplotlib.pyplot as plt img = cv2.imread('img.jpg') plt.imshow(img) plt.show() misses color conversion, so colors will be wrong. import cv2 import matplotlib.pyplot as plt img = cv2.imread('img.jpg') img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) plt.imshow(img_rgb) plt.show() converts BGR to RGB correctly before display. import matplotlib.pyplot as plt img = cv2.imread('img.jpg') plt.imshow(img) plt.show() misses cv2 import (NameError) and color conversion. import cv2 img = cv2.imread('img.jpg') cv2.imshow('Image', img) cv2.waitKey(0) cv2.destroyAllWindows() uses cv2.imshow which opens a separate window, not inside notebook.
  3. Final Answer:

    correctly converts BGR to RGB and displays image inside notebook -> Option C
  4. Quick Check:

    Convert BGR to RGB before matplotlib display [OK]
Hint: Convert BGR to RGB before plt.imshow for correct colors [OK]
Common Mistakes:
  • Skipping color conversion causing wrong colors
  • Using cv2.imshow inside notebooks expecting inline display
  • Assuming plt.imread always works for all image types