Bird
Raised Fist0
SciPydata~10 mins

Connected component labeling in SciPy - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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().

Practice

(1/5)
1. What is the main purpose of connected component labeling in image processing?
easy
A. To enhance image contrast
B. To convert color images to grayscale
C. To identify and label groups of connected pixels in binary images
D. To compress image file size

Solution

  1. Step 1: Understand connected component labeling

    It finds groups of connected pixels in binary images and assigns unique labels to each group.
  2. Step 2: Compare with other image tasks

    Other options like grayscale conversion, contrast enhancement, and compression do not involve labeling connected pixels.
  3. Final Answer:

    To identify and label groups of connected pixels in binary images -> Option C
  4. Quick Check:

    Connected component labeling = identify connected pixel groups [OK]
Hint: Remember: labeling means assigning unique IDs to connected pixels [OK]
Common Mistakes:
  • Confusing labeling with color conversion
  • Thinking it compresses images
  • Mixing it up with image enhancement
2. Which of the following is the correct way to import the connected component labeling function from scipy.ndimage?
easy
A. import scipy.ndimage.label
B. from scipy.ndimage import label
C. from scipy import label
D. import label from scipy.ndimage

Solution

  1. Step 1: Recall correct import syntax in Python

    The correct syntax to import a function is 'from module import function'.
  2. Step 2: Match with scipy.ndimage.label

    The function 'label' is inside 'scipy.ndimage', so 'from scipy.ndimage import label' is correct.
  3. Final Answer:

    from scipy.ndimage import label -> Option B
  4. Quick Check:

    Correct import syntax = from module import function [OK]
Hint: Use 'from module import function' to import specific functions [OK]
Common Mistakes:
  • Using 'import scipy.ndimage.label' which is invalid
  • Trying 'from scipy import label' which misses submodule
  • Incorrect 'import label from scipy.ndimage' syntax
3. Given the following code, what will be the output of num_features?
import numpy as np
from scipy.ndimage import label

array = np.array([[1, 0, 0, 1],
                  [1, 1, 0, 0],
                  [0, 0, 1, 1],
                  [0, 0, 1, 0]])

labeled_array, num_features = label(array)
print(num_features)
medium
A. 1
B. 3
C. 4
D. 2

Solution

  1. Step 1: Identify connected groups in the array

    There are two groups of connected 1s: one cluster at top-left (positions (0,0),(1,0),(1,1)) and one cluster at bottom-right (positions (0,3),(2,2),(2,3),(3,2)).
  2. Step 2: Count the connected components

    Counting these groups gives 2 connected components.
  3. Final Answer:

    2 -> Option D
  4. Quick Check:

    Count connected 1s groups = 2 [OK]
Hint: Count distinct connected 1s clusters in the array [OK]
Common Mistakes:
  • Counting isolated 1s as separate components incorrectly
  • Ignoring connectivity rules
  • Misreading array layout
4. What is wrong with the following code snippet for connected component labeling?
import numpy as np
from scipy.ndimage import label

binary_image = np.array([[1, 1, 0],
                         [0, 1, 0],
                         [1, 0, 1]])

labeled, num = label(binary_image, structure=1)
print(num)
medium
A. The 'structure' parameter should be an array, not an integer
B. The input array must be float type, not integer
C. The 'label' function does not accept a 'structure' parameter
D. The print statement syntax is incorrect

Solution

  1. Step 1: Check 'structure' parameter type

    The 'structure' parameter expects an array defining connectivity, not a single integer.
  2. Step 2: Identify correct usage

    Passing 'structure=1' is invalid; it should be an array like np.ones((3,3)) for full connectivity.
  3. Final Answer:

    The 'structure' parameter should be an array, not an integer -> Option A
  4. Quick Check:

    'structure' must be array, not int [OK]
Hint: Remember: 'structure' needs an array defining connectivity [OK]
Common Mistakes:
  • Passing integer instead of array for 'structure'
  • Assuming 'label' lacks 'structure' parameter
  • Confusing data types of input array
5. You have a binary image with noise: small isolated pixels scattered randomly. How can you use connected component labeling with scipy to remove noise by keeping only components larger than 2 pixels?
hard
A. Label components, count sizes, then remove components with size ≤ 2
B. Apply Gaussian blur before labeling to remove noise
C. Use label function with parameter 'min_size=3' to filter components
D. Invert the image and label the background instead

Solution

  1. Step 1: Label connected components in the binary image

    Use scipy.ndimage.label to assign unique labels to each connected group of pixels.
  2. Step 2: Count the size of each labeled component and filter

    Calculate the size of each component and remove those with size less than or equal to 2 pixels to eliminate noise.
  3. Final Answer:

    Label components, count sizes, then remove components with size ≤ 2 -> Option A
  4. Quick Check:

    Filter small components after labeling to remove noise [OK]
Hint: Label, count sizes, remove small components to clean noise [OK]
Common Mistakes:
  • Expecting label() to filter by size automatically
  • Using image blur instead of component filtering
  • Inverting image does not remove noise directly