0
0
Computer Visionml~10 mins

Displaying images (cv2.imshow, matplotlib) in Computer Vision - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to display an image using OpenCV.

Computer Vision
import cv2
img = cv2.imread('image.jpg')
cv2.[1]('Image Window', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Drag options to blanks, or click blank then click option'
Aimread
Bimwrite
Cimshow
DwaitKey
Attempts:
3 left
💡 Hint
Common Mistakes
Using cv2.imread instead of cv2.imshow to display the image.
Forgetting to call cv2.waitKey to keep the window open.
2fill in blank
medium

Complete the code to display an image using matplotlib.

Computer Vision
import matplotlib.pyplot as plt
import cv2
img = cv2.imread('image.jpg')
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2[1]RGB)
plt.imshow(img_rgb)
plt.show()
Drag options to blanks, or click blank then click option'
A0
B3
C1
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Not converting BGR to RGB before displaying with matplotlib, causing wrong colors.
Using cv2.COLOR_RGB2BGR instead of cv2.COLOR_BGR2RGB.
3fill in blank
hard

Fix the error in the code to properly display the image using OpenCV.

Computer Vision
import cv2
img = cv2.imread('image.jpg')
cv2.imshow('Window', img)
cv2.[1]()
Drag options to blanks, or click blank then click option'
AwaitKey(0)
BwaitKey
CdestroyAllWindows
Dimwrite
Attempts:
3 left
💡 Hint
Common Mistakes
Calling cv2.waitKey without parentheses or argument.
Calling cv2.destroyAllWindows before waitKey.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps image filenames to their grayscale images.

Computer Vision
import cv2
filenames = ['img1.jpg', 'img2.jpg', 'img3.jpg']
grayscale_images = {filename: cv2.[1](filename, [2]) for filename in filenames}
Drag options to blanks, or click blank then click option'
Aimread
Bimwrite
CCOLOR_BGR2GRAY
DIMREAD_GRAYSCALE
Attempts:
3 left
💡 Hint
Common Mistakes
Using imwrite instead of imread to read images.
Using COLOR_BGR2GRAY as a flag in imread, which is incorrect.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps image filenames to their RGB images using OpenCV and matplotlib.

Computer Vision
import cv2
filenames = ['img1.jpg', 'img2.jpg']
images_rgb = {filename: cv2.cvtColor(cv2.[1](filename), cv2.[2]) for filename in filenames if filename.endswith('.jpg') and filename != [3]
Drag options to blanks, or click blank then click option'
Aimread
BCOLOR_BGR2RGB
C'img2.jpg'
D'img1.jpg'
Attempts:
3 left
💡 Hint
Common Mistakes
Using COLOR_RGB2BGR instead of COLOR_BGR2RGB.
Not excluding the correct filename in the condition.