Scipy - Connected Component Labeling



Connected Component Labeling in SciPy refers to a technique often used in image processing and computer vision to identify and label connected regions or components in binary or gray-scale images.

This labeling helps in analyzing and processing distinct regions of an image such as identifying separate objects, blobs or clusters.

Key Concepts

Following are the key concepts that we need to know before proceeding with the Connected Component Labeling in Image segmentation −

  • Binary Image: Connected component labeling typically starts with a binary image where pixels are either foreground as 1 or True or background 0 or False. Gray scale images may need thresholding to convert them to binary.
  • Connectivity: Pixels are considered connected based on a predefined connectivity rule −
    • 4-connectivity: A pixel is connected to its immediate neighbors such as top, bottom, left, right.
    • 8-connectivity: A pixel is connected to its neighbors diagonally as well such as top-left, top-right, bottom-left, bottom-right.
  • Labels: Each distinct connected region in the image is assigned a unique label.

How SciPy Handles Connected Component Labeling?

In SciPy library, we have a scipy.ndimage.label() function which is used to perform connected component labeling.

Syntax

Following is the syntax of the function scipy.ndimage.label() to perform Connected Component Labeling −

scipy.ndimage.label(input, structure=None, output=None)

Following are the parameters of the function scipy.ndimage.label()

  • input: The binary input array which is a NumPy array.
  • structure(optional): Defines the connectivity of the elements i.e., pixels or voxels.
  • output (optional): A pre-allocated array where the labeled output will be stored.

Basic 4-connectivity

4-connectivity refers to a method of defining how pixels in a 2D grid are considered "connected" to each other. In 4-connectivity, a pixel is connected to its immediate horizontal and vertical neighbors i.e., top, bottom, left and right.

import numpy as np
from scipy.ndimage import label

# Input binary image
binary_image = np.array([[0, 1, 1, 0],
                         [1, 1, 0, 0],
                         [0, 0, 1, 1],
                         [0, 1, 1, 0]])

# Perform labeling with default (4-connectivity)
labeled_array, num_features = label(binary_image)

print("Labeled Array:")
print(labeled_array)
print("Number of Features:", num_features)

Following is the output of the function scipy.ndimage.label() which is used to perform 4-Connectivity −

Labeled Array:
[[0 1 1 0]
 [1 1 0 0]
 [0 0 2 2]
 [0 2 2 0]]
Number of Features: 2

Using 8-Connectivity

In 8-connectivity pixels are considered connected if they share an edge or a corner with the current pixel. This includes all 8 neighbors in a 2D grid such as top, bottom, left, right and the four diagonal neighbors.

import numpy as np
from scipy.ndimage import label

# Input binary image
binary_image = np.array([[0, 1, 0, 0],
                         [1, 1, 0, 0],
                         [0, 0, 1, 1],
                         [0, 0, 1, 0]])

# Define an 8-connectivity structure
structure = np.array([[1, 1, 1],
                      [1, 1, 1],
                      [1, 1, 1]])

# Perform connected component labeling
labeled_array, num_features = label(binary_image, structure=structure)

print("Labeled Array:")
print(labeled_array)
print("Number of Features:", num_features)

Following is the output of the function scipy.ndimage.label() which is used to perform 4-Connectivity −

Labeled Array:
[[0 1 0 0]
 [1 1 0 0]
 [0 0 1 1]
 [0 0 1 0]]
Number of Features: 1

4-Connectivity Vs 8-Connectivity

The primary difference between 4-connectivity and 8-connectivity lies in how they define the neighborhood of a pixel in an image and determine which pixels are considered "connected" to each other.

Aspect 4-Connectivity 8-Connectivity
Connected Neighbors Only horizontal and vertical neighbors (top, bottom, left, right). Includes horizontal, vertical, and diagonal neighbors (top-left, top-right, bottom-left, bottom-right).
Total Number of Neighbors 4 (top, bottom, left, right) 8 (including diagonal neighbors)
Connectivity Behavior Only pixels sharing edges (not corners) are connected. Pixels sharing edges or corners are connected.
Resulting Regions May result in more disconnected components, as diagonal connections are ignored. May result in fewer disconnected components, as diagonal connections are considered.
Advertisements