0
0
SciPydata~10 mins

Connected component labeling in SciPy - Interactive Code Practice

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

Complete the code to import the connected components function from scipy.ndimage.

SciPy
from scipy.ndimage import [1]
Drag options to blanks, or click blank then click option'
Alabel
Bconnect_components
Ccomponent_label
Dconnected_label
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent function name like 'connect_components'.
Confusing the function with other similar names.
2fill in blank
medium

Complete the code to create a binary 2D numpy array named 'image' with a 3x3 block of ones in the center.

SciPy
import numpy as np
image = np.zeros((7, 7), dtype=int)
image[[1]] = 1
Drag options to blanks, or click blank then click option'
A2:5, 2:5
B1:4, 1:4
C3:6, 3:6
D0:3, 0:3
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing slices that are too small or too large.
Off-by-one errors in slicing.
3fill in blank
hard

Fix the error in the code to label connected components in 'image' using 4-connectivity.

SciPy
labeled_array, num_features = label(image, [1]=1)
Drag options to blanks, or click blank then click option'
Aconnect
Bconnectivity
Cconnectivity_structure
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect parameter names like 'connect' or 'connectivity_structure'.
Omitting the parameter and getting default connectivity.
4fill in blank
hard

Fill both blanks to create a structuring element for 8-connectivity and label the image accordingly.

SciPy
structure = np.array([[[1], [2], [1]],
                      [[2], 1, [2]],
                      [[1], [2], [1]]])
labeled_array, num_features = label(image, structure=structure)
Drag options to blanks, or click blank then click option'
A1
B0
C2
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0s for corners which breaks 8-connectivity.
Using values other than 0 or 1.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each label to the count of pixels in that component, excluding background (label 0).

SciPy
counts = {label: (labeled_array == label).[1]() for label in range(1, [2] + [3])}
Drag options to blanks, or click blank then click option'
Asum
Bnum_features
C1
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Including label 0 which is background.
Using incorrect range limits.
Using a non-existent method like count().