Bird
Raised Fist0
SciPydata~10 mins

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

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
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.

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