0
0
SciPydata~20 mins

Connected component labeling in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Connected Components Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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))
A(array([[1, 0, 0, 1], [1, 1, 0, 0], [0, 0, 2, 2], [0, 0, 2, 0]]), 2)
B(array([[1, 0, 0, 2], [1, 1, 0, 0], [0, 0, 3, 3], [0, 0, 3, 0]]), 3)
C(array([[1, 0, 0, 2], [1, 1, 0, 0], [0, 0, 2, 2], [0, 0, 2, 0]]), 2)
D(array([[1, 0, 0, 2], [1, 1, 0, 0], [0, 0, 3, 3], [0, 0, 4, 0]]), 4)
Attempts:
2 left
💡 Hint
Look at how connected pixels with value 1 are grouped. Each group gets a unique label starting from 1.
data_output
intermediate
1: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)
A2
B1
C3
D0
Attempts:
2 left
💡 Hint
Check if the two 1s at positions (0,0,0) and (0,0,1) are connected.
visualization
advanced
2: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?
A
plt.imshow(binary_image, cmap='nipy_spectral')
plt.colorbar()
plt.show()
B
plt.imshow(labeled_array, cmap='gray')
plt.colorbar()
plt.show()
C
plt.imshow(labeled_array, cmap='nipy_spectral')
plt.colorbar()
plt.show()
D
plt.imshow(binary_image, cmap='gray')
plt.colorbar()
plt.show()
Attempts:
2 left
💡 Hint
Use the labeled array and a color map that shows distinct colors for different labels.
🔧 Debug
advanced
1: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)
ARuntimeWarning: Non-binary values ignored
BValueError: The input array must be 2D
CNo error, output is (array([[1, 0], [0, 0]]), 1)
DTypeError: Input array must be binary or boolean
Attempts:
2 left
💡 Hint
Check the input array values. Are they binary (0 or 1)?
🚀 Application
expert
2: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)
A1
B2
C4
D3
Attempts:
2 left
💡 Hint
Connectivity=1 means only vertical and horizontal neighbors are connected, not diagonals.