- 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 - ndimage.label() Function
The scipy.ndimage.label() is a function in the scipy.ndimage module which is used to perform connected component labeling on a binary image. It labels distinct connected components in the input array based on the given connectivity such as 4-connectivity, 8-connectivity, etc.
The purpose of connected component labeling is to identify and label separate regions i.e., objects in a binary image. Each connected region is assigned a unique integer label which can then be used for further analysis such as object counting or region analysis.
This function works on binary images where non-zero elements are treated as foreground i.e., part of a connected component and zero elements are treated as the background.
Syntax
Following is the syntax of the function scipy.ndimage.label() to perform connected component labeling operation on the input image −
scipy.ndimage.label(input, structure=None, output=None)
Parameters
Following are the parameters of the scipy.ndimage.label() function −
- input: The input binary image (array) on which connected component labeling is applied. It can be a 2D or 3D binary array.
- structure (optional): The structuring element that defines the connectivity of the components. It is a binary array of the same shape as the input. If not specified the function uses a default structure representing 4-connectivity in 2D or 6-connectivity in 3D.
- output (optional): The array where the result of the operation is stored. If not specified then a new array is created.
Return Value
The scipy.ndimage.label() function returns two values as mentioned below −
- labeled_array: A labeled array of the same shape as the input where each connected component is assigned a unique integer label.
- num_features: The total number of connected components found in the input image.
Labeling Connected Components
Connected component labeling is commonly used in image analysis to identify distinct objects or regions. This function assigns each connected component a unique label which can then be used for counting, feature extraction or region-based analysis.
Below is an example that shows how to use scipy.ndimage.label() for labeling connected components in a binary image −
import numpy as np
from scipy.ndimage import label
# Create a binary image with connected components (2D)
binary_image = np.array([[0, 1, 1, 0],
[1, 1, 0, 0],
[0, 0, 1, 1],
[0, 1, 1, 0]])
# Perform connected component labeling
labeled_array, num_features = label(binary_image)
# Print the results
print("Labeled Array:")
print(labeled_array)
print("Number of Features:", num_features)
Here is the output of the function scipy.ndimage.label() used for connected component labeling in a 2D binary image −
Labeled Array: [[0 1 1 0] [1 1 0 0] [0 0 2 2] [0 2 2 0]] Number of Features: 2
Using Custom Connectivity
Connected component labeling can also be customized by specifying the connectivity structure using the structure parameter. In this example we will apply 8-connectivity to label connected components in a 2D image −
import numpy as np
from scipy.ndimage import label
# Create a binary image with connected components (2D)
binary_image = np.array([[0, 1, 1, 0],
[1, 1, 0, 0],
[0, 0, 1, 1],
[0, 1, 1, 0]])
# Define 8-connectivity structure
structure = np.array([[1, 1, 1],
[1, 1, 1],
[1, 1, 1]])
# Perform connected component labeling with 8-connectivity
labeled_array, num_features = label(binary_image, structure=structure)
# Print the results
print("Labeled Array (8-Connectivity):")
print(labeled_array)
print("Number of Features:", num_features)
Here is the output of the function scipy.ndimage.label() when 8-connectivity is used −
Labeled Array (8-Connectivity): [[0 1 1 0] [1 1 0 0] [0 0 1 1] [0 1 1 0]] Number of Features: 1
Labeling a 3D Volume
The scipy.ndimage.label() function can also be applied to 3D binary arrays. Below is an example that shows how to label connected components in a 3D volume −
import numpy as np
from scipy.ndimage import label
# 3D binary image
binary_volume = np.array([[[0, 1, 1],
[1, 1, 0],
[0, 0, 0]],
[[1, 1, 0],
[0, 1, 1],
[0, 0, 0]],
[[0, 0, 0],
[0, 1, 1],
[1, 1, 0]]])
# Perform labeling in 3D
labeled_volume, num_features = label(binary_volume)
# Print the results
print("Labeled Volume:")
print(labeled_volume)
print("Number of Features:", num_features)
Here is the output of the function scipy.ndimage.label() applied to a 3D volume −
Labeled Array (8-Connectivity): [[0 1 1 0] [1 1 0 0] [0 0 1 1] [0 1 1 0]] Number of Features: 1 PS D:\Tutorialspoint> python sample.py Labeled Volume: [[[0 1 1] [1 1 0] [0 0 0]] [[1 1 0] [0 1 1] [0 0 0]] [[0 0 0] [0 1 1] [1 1 0]]] Number of Features: 1