0
0
Matplotlibdata~10 mins

Color channel handling in Matplotlib - Interactive Code Practice

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

Complete the code to extract the red channel from the image array.

Matplotlib
red_channel = image[:, :, [1]]
Drag options to blanks, or click blank then click option'
A1
B0
C2
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 or 2 instead of 0 for the red channel index.
Confusing channel order with BGR instead of RGB.
2fill in blank
medium

Complete the code to create a grayscale image by averaging the RGB channels.

Matplotlib
gray_image = image.mean(axis=[1])
Drag options to blanks, or click blank then click option'
A-1
B1
C0
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Averaging over axis 0 or 1 which are spatial dimensions.
Using -1 which also works but is less explicit.
3fill in blank
hard

Fix the error in the code to swap the red and blue channels of the image.

Matplotlib
swapped_image = image[:, :, [[1], 1, 0]]
Drag options to blanks, or click blank then click option'
A2
B3
C1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or 1 incorrectly in the first position.
Including an invalid index like 3.
4fill in blank
hard

Fill both blanks to create a new image with only the green channel and zeros for red and blue.

Matplotlib
new_image = np.zeros_like(image)
new_image[:, :, [1]] = image[:, :, [2]]
Drag options to blanks, or click blank then click option'
A1
B0
C2
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using different indices for source and destination channels.
Using indices for red or blue instead of green.
5fill in blank
hard

Fill all three blanks to normalize each color channel separately to the range 0-1.

Matplotlib
normalized = np.empty_like(image, dtype=float)
for i in range(3):
    channel = image[:, :, [1]]
    min_val = channel.min()
    max_val = channel.max()
    normalized[:, :, [2]] = (channel - min_val) / (max_val - min_val)

result = normalized[:, :, [3]]
Drag options to blanks, or click blank then click option'
Ai
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using fixed indices like 0 instead of the loop variable.
Mixing indices between source and destination channels.