Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to load an image using OpenCV.
Intro to Computing
import cv2 image = cv2.[1]('image.jpg')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using imshow instead of imread
Using imwrite which saves images
Using resize which changes image size
✗ Incorrect
The imread function loads an image from a file into memory.
2fill in blank
mediumComplete the code to convert a color image to grayscale using OpenCV.
Intro to Computing
gray_image = cv2.cvtColor(image, cv2.[1]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using COLOR_BGR2RGB which changes color space but not to grayscale
Using COLOR_GRAY2BGR which converts grayscale to color
Using COLOR_RGB2BGR which swaps color channels
✗ Incorrect
COLOR_BGR2GRAY converts a color image from BGR to grayscale.
3fill in blank
hardFix the error in the code to display an image using OpenCV.
Intro to Computing
cv2.[1]('Image Window', image) cv2.waitKey(0) cv2.destroyAllWindows()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using imread which loads images
Using imwrite which saves images
Using resize which changes image size
✗ Incorrect
The imshow function displays an image in a window.
4fill in blank
hardFill both blanks to create a binary threshold on a grayscale image.
Intro to Computing
_, binary_image = cv2.threshold(gray_image, [1], [2], cv2.THRESH_BINARY)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 as threshold which makes all pixels black
Using 128 as max value which is not standard
Swapping threshold and max value
✗ Incorrect
The threshold value is 127 and the max value for the binary image is 255.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps image filenames to their grayscale shapes.
Intro to Computing
shapes = {filename: cv2.imread(filename, [1]).shape[:[2]] for filename in files if cv2.imread(filename, [3]) is not None} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using IMREAD_COLOR which loads 3 channels
Using 3 for slicing shape which is for color images
Using different flags for loading images in comprehension
✗ Incorrect
Images are loaded in grayscale mode (IMREAD_GRAYSCALE), so shape has 2 dimensions (height, width).