0
0
NumPydata~20 mins

Image as array concept in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Image Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the shape of the image array?
Given a color image loaded as a NumPy array with shape (480, 640, 3), what does the shape represent?
NumPy
import numpy as np
image = np.zeros((480, 640, 3))
print(image.shape)
A(480, 640, 3) means 480 pixels width, 640 pixels height, and 3 color channels (RGB)
B(480, 640, 3) means 480 pixels height, 640 pixels width, and 3 color channels (RGB)
C(480, 640, 3) means 3 pixels height, 480 pixels width, and 640 color channels
D(480, 640, 3) means 3 pixels width, 640 pixels height, and 480 color channels
Attempts:
2 left
💡 Hint
Remember the shape order is (height, width, channels) for images.
data_output
intermediate
1:30remaining
What is the pixel value at position (2, 3)?
Consider this grayscale image array:
import numpy as np
image = np.array([[10, 20, 30, 40],
                  [50, 60, 70, 80],
                  [90, 100, 110, 120]])
print(image[2, 3])
What is the output?
NumPy
import numpy as np
image = np.array([[10, 20, 30, 40],
                  [50, 60, 70, 80],
                  [90, 100, 110, 120]])
print(image[2, 3])
A120
B110
C80
D90
Attempts:
2 left
💡 Hint
Remember indexing is zero-based and rows come first.
visualization
advanced
2:30remaining
Visualize a grayscale image from a 2D array
Which code snippet correctly displays a 5x5 grayscale image using matplotlib from a NumPy array?
NumPy
import numpy as np
import matplotlib.pyplot as plt
image = np.array([[0, 50, 100, 150, 200],
                  [10, 60, 110, 160, 210],
                  [20, 70, 120, 170, 220],
                  [30, 80, 130, 180, 230],
                  [40, 90, 140, 190, 240]])
plt.imshow(image, cmap='gray')
plt.show()
Aplt.imshow(image, cmap='jet') followed by plt.show()
Bplt.imshow(image) without cmap argument followed by plt.show()
Cplt.imshow(image, cmap='gray') followed by plt.show()
Dplt.plot(image) followed by plt.show()
Attempts:
2 left
💡 Hint
Use the grayscale colormap to show intensity properly.
🔧 Debug
advanced
2:00remaining
Why does this code raise an error?
What error does this code raise and why?
import numpy as np
image = np.zeros((100, 100, 3))
print(image[50, 50])
print(image[50, 50, 3])
NumPy
import numpy as np
image = np.zeros((100, 100, 3))
print(image[50, 50])
print(image[50, 50, 3])
ANo error, prints pixel values
BTypeError: too many indices for array
CValueError: invalid index
DIndexError: index 3 is out of bounds for axis 2 with size 3
Attempts:
2 left
💡 Hint
Remember Python indexing starts at 0 and max index is size-1.
🚀 Application
expert
3:00remaining
Calculate average color of an RGB image
Given a NumPy array representing an RGB image of shape (100, 100, 3), which code correctly calculates the average color across all pixels?
NumPy
import numpy as np
image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
Aavg_color = image.mean(axis=(0,1))
Bavg_color = image.mean(axis=2)
Cavg_color = image.mean(axis=0)
Davg_color = image.mean()
Attempts:
2 left
💡 Hint
Average color means average over height and width, keep channels separate.