0
0
SciPydata~5 mins

Connected component labeling in SciPy

Choose your learning style9 modes available
Introduction

Connected component labeling helps find groups of connected pixels in images or data. It tells which parts belong together.

Finding separate objects in a black and white image, like counting coins on a table.
Detecting clusters in a grid of data, such as patches of forest in satellite images.
Grouping connected regions in medical images, like tumors or organs.
Identifying connected areas in network graphs or maps.
Syntax
SciPy
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.

Examples
Basic example with a small 2D array. Finds connected groups of 1s.
SciPy
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)
Edge case: empty array with no connected components.
SciPy
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)
Edge case: single element array with one connected component.
SciPy
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)
Using 8-connectivity to connect diagonally touching pixels.
SciPy
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)
Sample Program

This program creates a 5x5 grid with three groups of connected 1s. It labels each group with a unique number and prints the results.

SciPy
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}")
OutputSuccess
Important Notes

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.

Summary

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.