Connected component labeling helps find groups of connected pixels in images or data. It tells which parts belong together.
Connected component labeling in SciPy
Start learning this pattern below
Jump into concepts and practice - no test required
from scipy.ndimage import label labeled_array, num_features = label(input_array, structure=None)
input_array is a binary or boolean array where connected regions are labeled.
structure defines connectivity (e.g., 4-connectivity or 8-connectivity in 2D). If None, uses 8-connectivity in 2D.
import numpy as np from scipy.ndimage import label input_array = np.array([[1, 0, 0], [1, 1, 0], [0, 0, 1]]) labeled_array, num_features = label(input_array) print(labeled_array) print(num_features)
import numpy as np from scipy.ndimage import label input_array = np.zeros((3,3), dtype=int) labeled_array, num_features = label(input_array) print(labeled_array) print(num_features)
import numpy as np from scipy.ndimage import label input_array = np.array([[1]]) labeled_array, num_features = label(input_array) print(labeled_array) print(num_features)
import numpy as np from scipy.ndimage import label input_array = np.array([[1, 1, 0], [0, 1, 1], [0, 0, 1]]) structure = np.array([[1,1,1], [1,1,1], [1,1,1]]) # 8-connectivity labeled_array, num_features = label(input_array, structure=structure) print(labeled_array) print(num_features)
This program creates a 5x5 grid with three groups of connected 1s. It labels each group with a unique number and prints the results.
import numpy as np from scipy.ndimage import label # Create a 5x5 binary array with three connected components input_array = np.array([ [1, 1, 0, 0, 0], [1, 1, 0, 1, 1], [0, 0, 0, 1, 1], [0, 1, 0, 0, 0], [0, 1, 1, 0, 0] ]) print("Input array:") print(input_array) # Label connected components with default connectivity labeled_array, num_features = label(input_array) print("\nLabeled array:") print(labeled_array) print(f"\nNumber of connected components: {num_features}")
Time complexity is roughly O(n), where n is the number of elements in the array.
Space complexity depends on the size of the input array and the labeled output array.
Common mistake: forgetting to use a binary array (only 0 and 1) as input.
Use connected component labeling when you want to identify and count distinct groups in data. For simple counting without location, other methods might be faster.
Connected component labeling finds groups of connected pixels in binary data.
It assigns a unique label to each connected group.
Useful for image analysis, clustering, and region detection.
Practice
Solution
Step 1: Understand connected component labeling
It finds groups of connected pixels in binary images and assigns unique labels to each group.Step 2: Compare with other image tasks
Other options like grayscale conversion, contrast enhancement, and compression do not involve labeling connected pixels.Final Answer:
To identify and label groups of connected pixels in binary images -> Option CQuick Check:
Connected component labeling = identify connected pixel groups [OK]
- Confusing labeling with color conversion
- Thinking it compresses images
- Mixing it up with image enhancement
Solution
Step 1: Recall correct import syntax in Python
The correct syntax to import a function is 'from module import function'.Step 2: Match with scipy.ndimage.label
The function 'label' is inside 'scipy.ndimage', so 'from scipy.ndimage import label' is correct.Final Answer:
from scipy.ndimage import label -> Option BQuick Check:
Correct import syntax = from module import function [OK]
- Using 'import scipy.ndimage.label' which is invalid
- Trying 'from scipy import label' which misses submodule
- Incorrect 'import label from scipy.ndimage' syntax
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)Solution
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)).Step 2: Count the connected components
Counting these groups gives 2 connected components.Final Answer:
2 -> Option DQuick Check:
Count connected 1s groups = 2 [OK]
- Counting isolated 1s as separate components incorrectly
- Ignoring connectivity rules
- Misreading array layout
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)Solution
Step 1: Check 'structure' parameter type
The 'structure' parameter expects an array defining connectivity, not a single integer.Step 2: Identify correct usage
Passing 'structure=1' is invalid; it should be an array like np.ones((3,3)) for full connectivity.Final Answer:
The 'structure' parameter should be an array, not an integer -> Option AQuick Check:
'structure' must be array, not int [OK]
- Passing integer instead of array for 'structure'
- Assuming 'label' lacks 'structure' parameter
- Confusing data types of input array
Solution
Step 1: Label connected components in the binary image
Use scipy.ndimage.label to assign unique labels to each connected group of pixels.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.Final Answer:
Label components, count sizes, then remove components with size ≤ 2 -> Option AQuick Check:
Filter small components after labeling to remove noise [OK]
- Expecting label() to filter by size automatically
- Using image blur instead of component filtering
- Inverting image does not remove noise directly
