Challenge - 5 Problems
Connected Components Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of connected components labeling on a binary image
What is the output of the following code that labels connected components in a binary image using
scipy.ndimage.label?SciPy
import numpy as np from scipy.ndimage import label binary_image = np.array([ [1, 0, 0, 1], [1, 1, 0, 0], [0, 0, 1, 1], [0, 0, 1, 0] ]) labeled_array, num_features = label(binary_image) print((labeled_array, num_features))
Attempts:
2 left
💡 Hint
Look at how connected pixels with value 1 are grouped. Each group gets a unique label starting from 1.
✗ Incorrect
The function
label assigns a unique integer to each connected group of 1s. Here, there are three groups: top-left corner, top-right corner, and bottom-right corner. So, the labeled array shows 1, 2, and 3 for these groups, and the total number of features is 3.❓ data_output
intermediate1:30remaining
Number of connected components in a 3D array
Given the following 3D binary array, how many connected components does
scipy.ndimage.label find?SciPy
import numpy as np from scipy.ndimage import label array_3d = np.zeros((3, 3, 3), dtype=int) array_3d[0, 0, 0] = 1 array_3d[0, 0, 1] = 1 array_3d[2, 2, 2] = 1 labeled, num = label(array_3d) print(num)
Attempts:
2 left
💡 Hint
Check if the two 1s at positions (0,0,0) and (0,0,1) are connected.
✗ Incorrect
The two 1s at (0,0,0) and (0,0,1) are adjacent and form one connected component. The single 1 at (2,2,2) is isolated, so total connected components are 2.
❓ visualization
advanced2:30remaining
Visualizing labeled connected components
Which option shows the correct matplotlib code to visualize the labeled connected components of a 2D binary image with distinct colors for each component?
SciPy
import numpy as np from scipy.ndimage import label import matplotlib.pyplot as plt binary_image = np.array([ [1, 0, 1, 1], [1, 1, 0, 0], [0, 0, 1, 1], [0, 0, 1, 0] ]) labeled_array, num_features = label(binary_image) # Which code below correctly plots labeled_array with distinct colors?
Attempts:
2 left
💡 Hint
Use the labeled array and a color map that shows distinct colors for different labels.
✗ Incorrect
The labeled array contains integer labels for each connected component. Using a color map like 'nipy_spectral' shows each label in a different color. Using the binary image or grayscale color map won't distinguish components clearly.
🔧 Debug
advanced1:30remaining
Identify the error in connected components labeling code
What error will the following code raise when trying to label connected components?
SciPy
import numpy as np from scipy.ndimage import label image = np.array([[1, 2], [3, 4]]) labeled, num = label(image) print(num)
Attempts:
2 left
💡 Hint
Check the input array values. Are they binary (0 or 1)?
✗ Incorrect
The label function expects a binary or boolean array. Here, the input has values other than 0 or 1, so it raises a RuntimeWarning and treats non-zero values as 1.
🚀 Application
expert2:30remaining
Counting connected components with custom connectivity
Using
scipy.ndimage.label, how many connected components are found in the following binary image if connectivity=1 (only direct neighbors up, down, left, right) is used?SciPy
import numpy as np from scipy.ndimage import label binary_image = np.array([ [1, 0, 1, 1], [1, 1, 0, 0], [0, 0, 1, 1], [0, 0, 1, 0] ]) structure = np.array([ [0,1,0], [1,1,1], [0,1,0] ]) labeled_array, num_features = label(binary_image, structure=structure) print(num_features)
Attempts:
2 left
💡 Hint
Connectivity=1 means only vertical and horizontal neighbors are connected, not diagonals.
✗ Incorrect
With the given structure, diagonal neighbors are not connected. The image has three distinct connected groups under this connectivity rule.