Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
cv2.imshow is used to display an image in a window.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
OpenCV loads images in BGR format, so we convert from BGR to RGB using cv2.COLOR_BGR2RGB.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling cv2.waitKey without parentheses or argument.
Calling cv2.destroyAllWindows before waitKey.
✗ Incorrect
cv2.waitKey(0) waits for a key press to keep the window open until you press a key.
4fill in blank
hardFill 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'
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.
✗ Incorrect
cv2.imread with flag cv2.IMREAD_GRAYSCALE loads images in grayscale mode.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using COLOR_RGB2BGR instead of COLOR_BGR2RGB.
Not excluding the correct filename in the condition.
✗ Incorrect
We read images with imread, convert from BGR to RGB, and exclude 'img2.jpg' from the dictionary.