Bird
0
0

After executing the following code, what will be the shape of blue_channel?

medium📝 Predict Output Q4 of 15
Matplotlib - Image Display
After executing the following code, what will be the shape of blue_channel?
import numpy as np
img = np.random.rand(200, 100, 3)
blue_channel = img[:, :, 2]
A(100, 200)
B(200, 100, 1)
C(3, 200, 100)
D(200, 100)
Step-by-Step Solution
Solution:
  1. Step 1: Understand the shape of img

    The image array img has shape (200, 100, 3), where 200 is height, 100 is width, and 3 is the color channels (RGB).
  2. Step 2: Extracting the blue channel

    Using img[:, :, 2] selects all rows and columns but only the third channel (index 2), which corresponds to blue.
  3. Step 3: Resulting shape

    This extraction removes the last dimension, resulting in a 2D array with shape (200, 100).
  4. Final Answer:

    (200, 100) -> Option D
  5. Quick Check:

    Channel extraction reduces dimension by one [OK]
Quick Trick: Extracting one channel removes last dimension [OK]
Common Mistakes:
  • Assuming the channel dimension remains after extraction
  • Confusing height and width dimensions
  • Using wrong channel index

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes