
- 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_closing() Function
The scipy.ndimage.binary_closing() is a function in SciPy which is a morphological operation used to close small holes or gaps in binary images. It is the reverse of binary opening and is performed by first applying binary dilation and then binary erosion.
This operation helps to fill small dark regions i.e., holes surrounded by lighter areas in the image. It is particularly useful for removing noise or closing gaps in the shapes of objects.
This function takes a binary input image and applies the closing operation using a structuring element with parameters for iterations, output and boundary handling using border_value.
Syntax
Following is the syntax of the function scipy.ndimage.binary_closing() to perform Closing which is a morphological operation on an image −
scipy.ndimage.binary_closing(input, structure=None, iterations=1, output=None, origin=0, mask=None, border_value=0, brute_force=False)
Parameters
Following are the parameters of the scipy.ndimage.binary_closing() function −
- input: The input image or array which can be binary i.e., 0s and 1s or gray-scale on which the binary closing operation is applied.
- structure (optional): This is the structuring element used for the closing operation. If not provided then a default cross-shaped structuring element will be used.
- iterations (optional): The number of times the closing operation is applied. The default value is 1. If set to -1 then the closing operation will continue until the result no longer changes.
- output (optional): This is the array where the result will be stored. If not specified then a new array will be created.
- 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.
- mask (optional): A binary mask that limits the closing operation to specific areas of the input image. Only regions where the mask is non-zero will be processed.
- border_value (optional): This specifies the value used for padding the borders of the image. It can be either 0 or 1. The default value is 0.
- brute_force (optional): If set to True then a brute-force method is used for closing which can be slower but may handle certain custom structuring elements more effectively. The default value is False.
Return Value
The scipy.ndimage.binary_closing() function returns a binary image (array) where the closing operation has been applied.
Example of Basic Binary Closing
Following is the example of the function scipy.ndimage.binary_closing() in which binary closing operation is applied to a simple binary image without any custom structuring element or iterations −
import numpy as np import matplotlib.pyplot as plt from scipy.ndimage import binary_closing from skimage.data import binary_blobs # Set the random seed for reproducibility np.random.seed(1) # Generate a sample binary image (blobs) image = binary_blobs(length=128, blob_size_fraction=0.1) # Convert the image to a boolean array (0 or 1) image = image.astype(bool) # Apply binary closing result = binary_closing(image) # Plot original and closed images plt.figure(figsize=(12, 6)) plt.subplot(1, 2, 1) plt.title("Original Image") plt.imshow(image, cmap='gray') plt.axis('off') plt.subplot(1, 2, 2) plt.title("Binary Closing Result") plt.imshow(result, cmap='gray') plt.axis('off') plt.tight_layout() plt.show()
Here is the output of the function scipy.ndimage.binary_closing() which is used to implement basic Example of Binary closing −

Binary Closing with Multiple Iterations
As we know that Binary closing is a morphological operation that first applies dilation followed by erosion. It is often used to remove small holes or noise in binary images. This operation can be applied multiple times by specifying the number of iterations which helps in further smoothing or closing of small gaps. Here is an example of applying binary closing with multiple iterations in scipy.ndimage.binary_closing() −
import numpy as np import matplotlib.pyplot as plt from scipy.ndimage import binary_closing from skimage.data import binary_blobs # Set the random seed for reproducibility np.random.seed(1) # Generate a sample binary image (blobs) image = binary_blobs(length=128, blob_size_fraction=0.1) # Convert the image to a boolean array (0 or 1) image = image.astype(bool) # Apply binary closing with multiple iterations (e.g., 5 iterations) result = binary_closing(image, iterations=5) # Plot original and closed images plt.figure(figsize=(12, 6)) plt.subplot(1, 2, 1) plt.title("Original Image") plt.imshow(image, cmap='gray') plt.axis('off') plt.subplot(1, 2, 2) plt.title("Binary Closing Result (5 Iterations)") plt.imshow(result, cmap='gray') plt.axis('off') plt.tight_layout() plt.show()
Here is the output of the function scipy.ndimage.binary_closing() in which Binary closing with multiple iterations −

Binary Closing with Border Value
This example demonstrates the use of a border value to specify how the image boundaries should be handled using the function scipy.ndimage.binary_closing() −
import numpy as np import matplotlib.pyplot as plt from scipy.ndimage import binary_closing from skimage.data import binary_blobs # Generate a sample binary image (blobs) image = binary_blobs(length=128, blob_size_fraction=0.1) # Apply binary closing with a border value of 1 result = binary_closing(image, border_value=1) # Plot original and closed images plt.figure(figsize=(12, 6)) plt.subplot(1, 2, 1) plt.title("Original Image") plt.imshow(image, cmap='gray') plt.axis('off') plt.subplot(1, 2, 2) plt.title("Binary Closing with Border Value") plt.imshow(result, cmap='gray') plt.axis('off') plt.tight_layout() plt.show()
Here is the output of the function scipy.ndimage.binary_closing() in which Binary closing with boundary value −
