0
0
Computer Visionml~10 mins

Image as numerical data (pixels, channels) 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 load an image as a numerical array using PIL.

Computer Vision
from PIL import Image
import numpy as np
image = Image.open('photo.jpg')
numpy_array = np.array([1])
Drag options to blanks, or click blank then click option'
Aimage
BImage
Cnp
Dphoto.jpg
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the module name instead of the image object.
Passing the filename string instead of the image object.
2fill in blank
medium

Complete the code to get the shape of the image array (height, width, channels).

Computer Vision
height, width, channels = numpy_array[1]
Drag options to blanks, or click blank then click option'
A.size
B.shape
C.dtype
D.ndim
Attempts:
3 left
💡 Hint
Common Mistakes
Using .size which gives total number of elements.
Using .dtype which gives data type.
3fill in blank
hard

Fix the error in the code to convert the image to grayscale by averaging color channels.

Computer Vision
grayscale = numpy_array.mean(axis=[1])
Drag options to blanks, or click blank then click option'
A2
B1
C0
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Averaging over axis 0 or 1 which are height or width.
Using negative axis incorrectly.
4fill in blank
hard

Fill both blanks to create a dictionary mapping pixel coordinates to RGB tuples for pixels with red value > 100.

Computer Vision
pixel_dict = [1]( ((x, y), tuple(numpy_array[x, y])) for x in range(numpy_array.shape[0]) for y in range(numpy_array.shape[1]) if numpy_array[x, y, 0] [2] 100 )
Drag options to blanks, or click blank then click option'
Adict
Blist
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using list instead of dict.
Using < instead of > for the red value condition.
5fill in blank
hard

Fill all three blanks to normalize pixel values to range 0-1 and get the shape of the normalized array.

Computer Vision
normalized = numpy_array[1] / 255.0
shape = normalized[2]
channels = shape[3]
Drag options to blanks, or click blank then click option'
A.astype(float)
B.shape
C[2]
D.astype(int)
Attempts:
3 left
💡 Hint
Common Mistakes
Dividing integers without converting to float.
Using .astype(int) which loses decimal precision.
Accessing wrong index for channels.