0
0
Matplotlibdata~20 mins

Color channel handling in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Color Channel Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of color channel extraction
What is the shape and data type of the red channel extracted from this RGB image array?
Matplotlib
import numpy as np
image = np.array([[[255, 0, 0], [0, 255, 0]],
                  [[0, 0, 255], [255, 255, 0]]], dtype=np.uint8)
red_channel = image[:, :, 0]
print(red_channel.shape, red_channel.dtype)
A(2, 2) uint8
B(2, 2, 3) uint8
C(3, 2) int32
D(2, 2) float64
Attempts:
2 left
💡 Hint
Remember that extracting one color channel from an RGB image reduces the last dimension.
data_output
intermediate
1:30remaining
Result of swapping color channels
Given an RGB image array, what is the resulting pixel value after swapping the red and blue channels for the pixel [100, 150, 200]?
Matplotlib
pixel = [100, 150, 200]
swapped_pixel = [pixel[2], pixel[1], pixel[0]]
print(swapped_pixel)
A[150, 100, 200]
B[100, 150, 200]
C[200, 100, 150]
D[200, 150, 100]
Attempts:
2 left
💡 Hint
Swapping red and blue means the first and last values switch places.
visualization
advanced
2:30remaining
Visualizing individual color channels
Which option correctly plots the green channel of an RGB image using matplotlib?
Matplotlib
import numpy as np
import matplotlib.pyplot as plt
image = np.random.randint(0, 256, (5, 5, 3), dtype=np.uint8)
green_channel = image[:, :, 1]
plt.imshow(green_channel, cmap='Greens')
plt.show()
Aplt.imshow(image[:, :, 1])
Bplt.imshow(image[:, :, 1], cmap='Greens')
Cplt.imshow(image[:, :, 1], cmap='Reds')
Dplt.imshow(image[:, :, 0], cmap='Greens')
Attempts:
2 left
💡 Hint
Use the correct channel index and a matching colormap for green.
🔧 Debug
advanced
2:00remaining
Identify the error in color channel normalization
What error will this code raise when normalizing the blue channel of an image?
Matplotlib
import numpy as np
image = np.array([[[10, 20, 30], [40, 50, 60]]], dtype=np.uint8)
blue_channel = image[:, :, 2]
normalized = blue_channel / 255
print(normalized.dtype)
ANo error; dtype is float64
BTypeError: unsupported operand type(s) for /: 'uint8' and 'int'
CValueError: operands could not be broadcast together
DAttributeError: 'numpy.ndarray' object has no attribute 'dtype'
Attempts:
2 left
💡 Hint
Dividing uint8 by int converts to float automatically.
🚀 Application
expert
3:00remaining
Calculate average intensity of red channel in an image
Given a 3x3 RGB image array, what is the average intensity of the red channel?
Matplotlib
import numpy as np
image = np.array([
  [[10, 20, 30], [40, 50, 60], [70, 80, 90]],
  [[15, 25, 35], [45, 55, 65], [75, 85, 95]],
  [[20, 30, 40], [50, 60, 70], [80, 90, 100]]
], dtype=np.uint8)
red_channel = image[:, :, 0]
avg_red = np.mean(red_channel)
print(round(avg_red, 2))
A40.0
B50.0
C45.0
D55.0
Attempts:
2 left
💡 Hint
Sum all red values and divide by 9 pixels.