0
0
Computer Visionml~20 mins

Displaying images (cv2.imshow, matplotlib) in Computer Vision - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.