
- SciPy - Home
- SciPy - Introduction
- SciPy - Environment Setup
- SciPy - Basic Functionality
- SciPy - Relationship with NumPy
- SciPy Clusters
- SciPy - Clusters
- SciPy - Hierarchical Clustering
- SciPy - K-means Clustering
- SciPy - Distance Metrics
- SciPy Constants
- SciPy - Constants
- SciPy - Mathematical Constants
- SciPy - Physical Constants
- SciPy - Unit Conversion
- SciPy - Astronomical Constants
- SciPy - Fourier Transforms
- SciPy - FFTpack
- SciPy - Discrete Fourier Transform (DFT)
- SciPy - Fast Fourier Transform (FFT)
- SciPy Integration Equations
- SciPy - Integrate Module
- SciPy - Single Integration
- SciPy - Double Integration
- SciPy - Triple Integration
- SciPy - Multiple Integration
- SciPy Differential Equations
- SciPy - Differential Equations
- SciPy - Integration of Stochastic Differential Equations
- SciPy - Integration of Ordinary Differential Equations
- SciPy - Discontinuous Functions
- SciPy - Oscillatory Functions
- SciPy - Partial Differential Equations
- SciPy Interpolation
- SciPy - Interpolate
- SciPy - Linear 1-D Interpolation
- SciPy - Polynomial 1-D Interpolation
- SciPy - Spline 1-D Interpolation
- SciPy - Grid Data Multi-Dimensional Interpolation
- SciPy - RBF Multi-Dimensional Interpolation
- SciPy - Polynomial & Spline Interpolation
- SciPy Curve Fitting
- SciPy - Curve Fitting
- SciPy - Linear Curve Fitting
- SciPy - Non-Linear Curve Fitting
- SciPy - Input & Output
- SciPy - Input & Output
- SciPy - Reading & Writing Files
- SciPy - Working with Different File Formats
- SciPy - Efficient Data Storage with HDF5
- SciPy - Data Serialization
- SciPy Linear Algebra
- SciPy - Linalg
- SciPy - Matrix Creation & Basic Operations
- SciPy - Matrix LU Decomposition
- SciPy - Matrix QU Decomposition
- SciPy - Singular Value Decomposition
- SciPy - Cholesky Decomposition
- SciPy - Solving Linear Systems
- SciPy - Eigenvalues & Eigenvectors
- SciPy Image Processing
- SciPy - Ndimage
- SciPy - Reading & Writing Images
- SciPy - Image Transformation
- SciPy - Filtering & Edge Detection
- SciPy - Top Hat Filters
- SciPy - Morphological Filters
- SciPy - Low Pass Filters
- SciPy - High Pass Filters
- SciPy - Bilateral Filter
- SciPy - Median Filter
- SciPy - Non - Linear Filters in Image Processing
- SciPy - High Boost Filter
- SciPy - Laplacian Filter
- SciPy - Morphological Operations
- SciPy - Image Segmentation
- SciPy - Thresholding in Image Segmentation
- SciPy - Region-Based Segmentation
- SciPy - Connected Component Labeling
- SciPy Optimize
- SciPy - Optimize
- SciPy - Special Matrices & Functions
- SciPy - Unconstrained Optimization
- SciPy - Constrained Optimization
- SciPy - Matrix Norms
- SciPy - Sparse Matrix
- SciPy - Frobenius Norm
- SciPy - Spectral Norm
- SciPy Condition Numbers
- SciPy - Condition Numbers
- SciPy - Linear Least Squares
- SciPy - Non-Linear Least Squares
- SciPy - Finding Roots of Scalar Functions
- SciPy - Finding Roots of Multivariate Functions
- SciPy - Signal Processing
- SciPy - Signal Filtering & Smoothing
- SciPy - Short-Time Fourier Transform
- SciPy - Wavelet Transform
- SciPy - Continuous Wavelet Transform
- SciPy - Discrete Wavelet Transform
- SciPy - Wavelet Packet Transform
- SciPy - Multi-Resolution Analysis
- SciPy - Stationary Wavelet Transform
- SciPy - Statistical Functions
- SciPy - Stats
- SciPy - Descriptive Statistics
- SciPy - Continuous Probability Distributions
- SciPy - Discrete Probability Distributions
- SciPy - Statistical Tests & Inference
- SciPy - Generating Random Samples
- SciPy - Kaplan-Meier Estimator Survival Analysis
- SciPy - Cox Proportional Hazards Model Survival Analysis
- SciPy Spatial Data
- SciPy - Spatial
- SciPy - Special Functions
- SciPy - Special Package
- SciPy Advanced Topics
- SciPy - CSGraph
- SciPy - ODR
- SciPy Useful Resources
- SciPy - Reference
- SciPy - Quick Guide
- SciPy - Cheatsheet
- SciPy - Useful Resources
- SciPy - Discussion
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. |