0
0
SciPydata~10 mins

Connected component labeling in SciPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Connected component labeling
Input: Binary Image
Scan Image Pixels
Check Neighbors for Connectivity
Assign/Update Labels
Merge Equivalent Labels
Output: Labeled Image with Components
The process scans a binary image, checks pixel neighbors to find connected regions, assigns labels to these regions, merges equivalent labels, and outputs the labeled image.
Execution Sample
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,1]])

labeled_array, num_features = label(binary_image)
This code labels connected components in a binary image and counts how many distinct components exist.
Execution Table
StepPixel PositionPixel ValueNeighbors CheckedLabel AssignedCurrent Label Map Snapshot
1(0,0)1None (first pixel)1[[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
2(0,1)0N/A (background)0[[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
3(0,2)0N/A (background)0[[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
4(0,3)1Neighbors: (0,2)=0, (1,3)=02[[1,0,0,2],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
5(1,0)1Neighbors: (0,0)=11[[1,0,0,2],[1,0,0,0],[0,0,0,0],[0,0,0,0]]
6(1,1)1Neighbors: (1,0)=1, (0,1)=01[[1,0,0,2],[1,1,0,0],[0,0,0,0],[0,0,0,0]]
7(1,2)0N/A (background)0[[1,0,0,2],[1,1,0,0],[0,0,0,0],[0,0,0,0]]
8(1,3)0N/A (background)0[[1,0,0,2],[1,1,0,0],[0,0,0,0],[0,0,0,0]]
9(2,0)0N/A (background)0[[1,0,0,2],[1,1,0,0],[0,0,0,0],[0,0,0,0]]
10(2,1)0N/A (background)0[[1,0,0,2],[1,1,0,0],[0,0,0,0],[0,0,0,0]]
11(2,2)1Neighbors: (1,2)=0, (2,1)=03[[1,0,0,2],[1,1,0,0],[0,0,3,0],[0,0,0,0]]
12(2,3)1Neighbors: (2,2)=3, (1,3)=03[[1,0,0,2],[1,1,0,0],[0,0,3,3],[0,0,0,0]]
13(3,0)0N/A (background)0[[1,0,0,2],[1,1,0,0],[0,0,3,3],[0,0,0,0]]
14(3,1)0N/A (background)0[[1,0,0,2],[1,1,0,0],[0,0,3,3],[0,0,0,0]]
15(3,2)1Neighbors: (2,2)=3, (3,1)=03[[1,0,0,2],[1,1,0,0],[0,0,3,3],[0,0,3,0]]
16(3,3)1Neighbors: (3,2)=3, (2,3)=33[[1,0,0,2],[1,1,0,0],[0,0,3,3],[0,0,3,3]]
17EndN/AN/AN/AFinal labeled array with 3 components
💡 All pixels scanned; connected components labeled and counted.
Variable Tracker
VariableStartAfter 1After 4After 6After 11After 16Final
labeled_array[all zeros][[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]][[1,0,0,2],[0,0,0,0],[0,0,0,0],[0,0,0,0]][[1,0,0,2],[1,1,0,0],[0,0,0,0],[0,0,0,0]][[1,0,0,2],[1,1,0,0],[0,0,3,0],[0,0,0,0]][[1,0,0,2],[1,1,0,0],[0,0,3,3],[0,0,3,3]][[1,0,0,2],[1,1,0,0],[0,0,3,3],[0,0,3,3]]
num_features0122333
Key Moments - 3 Insights
Why does the pixel at (0,3) get a different label than the pixel at (0,0) even though both are 1?
Because they are not connected through neighboring pixels. The execution_table rows 1 and 4 show that (0,0) is labeled 1 first, then (0,3) is labeled 2 since its neighbors are background (0).
How does the algorithm decide to merge labels if two connected regions are found separately?
The scipy label function merges equivalent labels during scanning when neighbors with different labels are connected. In this example, no merges occur because connected regions are distinct, as seen in the labeled_array snapshots.
Why are some pixels assigned label 0?
Label 0 means background pixels (value 0 in the binary image). They are not part of any connected component, as shown in multiple execution_table rows where pixel value is 0 and label assigned is 0.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 6. What label is assigned to pixel (1,1)?
A2
B3
C1
D0
💡 Hint
Check the 'Label Assigned' column at step 6 in the execution_table.
At which step does the algorithm assign label 3 for the first time?
AStep 11
BStep 4
CStep 16
DStep 1
💡 Hint
Look at the 'Label Assigned' column and find when label 3 appears first in the execution_table.
If the pixel at (0,3) was connected to (0,0), how would the number of features change?
AIt would increase to 4
BIt would decrease to 2
CIt would remain 3
DIt would become 1
💡 Hint
Refer to the variable_tracker for num_features and consider merging connected components.
Concept Snapshot
Connected component labeling:
- Input: binary image (0=background, 1=foreground)
- Scan pixels, check neighbors for connectivity
- Assign labels to connected pixels
- Merge equivalent labels
- Output: labeled image and count of components
Full Transcript
Connected component labeling scans a binary image pixel by pixel. For each foreground pixel (value 1), it checks neighbors to see if they belong to an existing component. If yes, it assigns the same label; if no, it assigns a new label. Background pixels (value 0) get label 0. The algorithm merges labels if connected regions are found separately. The output is a labeled image where each connected component has a unique label and the total number of components is counted.