
- 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.binary_erosion() Function
The scipy.ndimage.binary_erosion() is a function used to perform binary erosion on an input binary image or array. Erosion is a morphological operation that reduces the size of foreground objects, typically represented by '1's in a binary image.
This function works by applying a structuring element to the image where each pixel in the output is set to '1' if the structuring element fits entirely within the foreground of the image. If not then the pixel is set to '0'. This operation is useful for removing small objects, breaking apart connected components or reducing object boundaries in binary images.
Syntax
Following is the syntax of the function scipy.ndimage.binary_erosion() to perform Erosion operation on an image −
scipy.ndimage.binary_erosion(input, structure=None, iterations=1, mask=None, output=None, border_value=0, origin=0, brute_force=False)
Parameters
Following are the parameters of the scipy.ndimage.binary_erosion() function −
- input: The input image or array which can be binary or grayscale, on which the binary erosion operation is applied.
- structure(optional): It is an array that explicitly specifies the structuring element. It overrides both size and footprint when provided.
- iterations(optional): The number of iterations the erosion operation is applied. By default, it is set to 1.
- mask(optional): A binary mask that limits the erosion operation to specific areas of the input image. Only regions where the mask is non-zero will be eroded.
- output(optional): This is an array where the result will be stored. If not provided then a new array is allocated.
- border_value(optional): This parameter specifies the value used for padding the borders of the image. It can be 0 or 1 and controls how the structuring element interacts with the image borders. The default value is 0.
- origin(optional): This parameter controls the placement of the structuring element relative to the current pixel. A value of 0 centers the structuring element at the pixel. Positive or negative values shift the structuring element.
- brute_force(optional): If True then a brute-force method is used for erosion, which can be slower but may handle certain custom structuring elements more effectively. The default value is False.
Return Value
The scipy.ndimage.binary_erosion() function returns binary array of the same shape as the input by representing the result of the binary erosion operation.
Basic Binary Erosion with Default Parameters
Following is the basic example of the scipy.ndimage.binary_erosion() which shows a simple binary erosion on a binary image using the default structuring element i.e., a 3x3 square −
import numpy as np import matplotlib.pyplot as plt from scipy.ndimage import binary_erosion # Create a binary image image = np.array([[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]) # Apply binary erosion with the default structuring element (3x3 square) eroded_image = binary_erosion(image) # Display the original and eroded images plt.figure(figsize=(10, 5)) plt.subplot(1, 2, 1) plt.title("Original Image") plt.imshow(image, cmap='gray') plt.axis('off') plt.subplot(1, 2, 2) plt.title("Eroded Image") plt.imshow(eroded_image, cmap='gray') plt.axis('off') plt.tight_layout() plt.show()
Here is the output of the function scipy.ndimage.binary_erosion() which is used to implement basic Example of Binary Erosion −

Binary Erosion with Custom Structuring
We can create a custom structuring element such as a disk or a rectangle and apply it during the erosion process. Here's an example of binary erosion using a custom structuring element in SciPy by using the function scipy.ndimage.binary_erosion() −
import numpy as np import matplotlib.pyplot as plt from scipy.ndimage import binary_erosion from skimage.morphology import disk # Create a simple binary image (1s and 0s) image = np.array([[1, 1, 1, 0, 0], [1, 1, 1, 0, 0], [1, 1, 1, 0, 0], [0, 0, 0, 0, 0]]) # Create a custom structuring element (disk of radius 2) structuring_element = disk(2) # Apply binary erosion with the custom structuring element eroded_image = binary_erosion(image, structure=structuring_element) # Plot the original and eroded images plt.figure(figsize=(10, 5)) # Original Image plt.subplot(1, 2, 1) plt.title("Original Image") plt.imshow(image, cmap='gray') plt.axis('off') # Eroded Image plt.subplot(1, 2, 2) plt.title("Eroded Image (Disk Structuring Element)") plt.imshow(eroded_image, cmap='gray') plt.axis('off') plt.tight_layout() plt.show()
Following is the output of the function scipy.ndimage.binary_erosion() where we can implement Binary Erosion with custom preferences −

Binary Erosion with Border Value Set to 1
In this example, the border_value parameter of the function scipy.ndimage.binary_erosion() is set to 1 by ensuring that the border of the image is treated as 1 during erosion −
import numpy as np import matplotlib.pyplot as plt from scipy.ndimage import binary_erosion # Create a binary image image = np.array([[1, 1, 1, 1], [1, 0, 0, 1], [1, 0, 0, 1], [1, 1, 1, 1]]) # Apply binary erosion with border_value set to 1 eroded_image = binary_erosion(image, border_value=1) # Display the original and eroded images plt.figure(figsize=(10, 5)) plt.subplot(1, 2, 1) plt.title("Original Image") plt.imshow(image, cmap='gray') plt.axis('off') plt.subplot(1, 2, 2) plt.title("Eroded Image with Border Value = 1") plt.imshow(eroded_image, cmap='gray') plt.axis('off') plt.tight_layout() plt.show()
Following is the output of the function scipy.ndimage.binary_erosion() with specifying the border parameter −
