
- 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.grey_closing() Function
The scipy.ndimage.grey_closing() is a function in the scipy.ndimage module, which is used to perform grey-level morphological closing on an image. It applies a dilation followed by an erosion operation using a specific structuring element.
The purpose of grey-level closing is to fill small holes or gaps in an image while preserving the shape and size of larger structures.
Unlike binary closing which works on binary images, grey-level closing operates on grayscale images by preserving the intensity of pixels. This function requires an input image and a structuring element to define the neighborhood used in the operation.
Syntax
Following is the syntax of the function scipy.ndimage.grey_closing() to perform grey scale Closing operation on the input image −
scipy.ndimage.grey_closing(input, size=None, footprint=None, structure=None, output=None, mode='reflect', cval=0.0, origin=0, *, axes=None)
Parameters
Following are the parameters of the scipy.ndimage.grey_closing() function −
- input: The input image or array on which the grayscale closing operation is applied. It can be a binary or grayscale image.
- size (optional): The size of the structuring element used for the closing operation and specified as a tuple e.g., (3, 3). This defines the neighborhood dimensions.
- footprint (optional): A binary array that defines the shape of the structuring element. It overrides the size parameter when provided.
- structure (optional): An array specifying the weights for the structuring element. It overrides both size and footprint if provided.
- output (optional): The array where the result of the operation is stored. If not specified then a new array is created.
- mode (optional): This parameter defines how the boundaries of the image are handled and the available modes are 'reflect', 'constant', 'nearest', 'mirror' and 'wrap'. The default value is 'reflect'.
- cval (optional): The constant value used for padding when mode='constant'. The default value is 0.0.
- origin (optional): It controls the placement of the structuring element relative to the current pixel. A value of 0 centers the element and positive or negative values shift it.
Return Value
The scipy.ndimage.grey_closing() function returns the processed array after applying the grayscale morphological closing.
Filling Small Holes in Binary Image
Grayscale morphological operations are powerful techniques for image preprocessing. One common application of grayscale closing grey_closing() is to fill small holes or gaps in a binary image while preserving the overall structure. Following is the example which shows how to use grey_closing() function to fill holes −
import numpy as np import scipy.ndimage as ndi import matplotlib.pyplot as plt # Create a binary image with holes (100x100 pixels) binary_image = np.zeros((100, 100), dtype=int) binary_image[30:70, 30:70] = 1 # Main object (square) binary_image[40, 40] = 0 # Add a hole binary_image[50, 50] = 0 # Add another hole # Apply grey_closing to fill small holes filled_image = ndi.grey_closing(binary_image, size=(5, 5)) # Display the original and processed images fig, axes = plt.subplots(1, 2, figsize=(12, 6)) # Original binary image with holes axes[0].imshow(binary_image, cmap='gray') axes[0].set_title("Binary Image with Holes") axes[0].axis("off") # Image after filling holes axes[1].imshow(filled_image, cmap='gray') axes[1].set_title("Image after Filling Holes") axes[1].axis("off") plt.tight_layout() plt.show()
Here is the output of the function scipy.ndimage.grey_closing() which is used to implement Greyscale closing to fill holes −

Smoothing Object Boundaries
Grayscale closing is also used to smooth the boundaries of objects by filling small gaps or concavities. Below is the example of using the function scipy.ndimage.grey_closing() for smoothing object boundaries −
import numpy as np import scipy.ndimage as ndi import matplotlib.pyplot as plt # Create a binary image with rough edges binary_image = np.zeros((100, 100), dtype=int) binary_image[30:70, 30:70] = 1 # Main object (square) binary_image[40:50, 30] = 0 # Add gap binary_image[50, 40:50] = 0 # Add concavity # Apply grey_closing to smooth edges smoothed_image = ndi.grey_closing(binary_image, size=(3, 3)) # Display the original and processed images fig, axes = plt.subplots(1, 2, figsize=(12, 6)) # Original binary image with rough edges axes[0].imshow(binary_image, cmap='gray') axes[0].set_title("Binary Image with Rough Edges") axes[0].axis("off") # Smoothed image axes[1].imshow(smoothed_image, cmap='gray') axes[1].set_title("Image after Smoothing") axes[1].axis("off") plt.tight_layout() plt.show()
Here is the output of the function scipy.ndimage.grey_closing() which is used to implement Greyscale closing to smooth boundaries −

Handling Non-Rectangular Neighborhoods with Footprint
While rectangular structuring elements are common in morphological operations which are certain applications require non-rectangular neighborhoods for more flexible processing. The footprint parameter in grey_closing() or similar functions in scipy.ndimage let us define custom-shaped structuring elements such as circles, crosses or other shapes for more precise control over the operation. Here is the example of showing how to use the scipy.ndimage.grey_closing() function for handling non-rectangular neighborhoods i.e., circular footprint with footprint −
import numpy as np import scipy.ndimage as ndi import matplotlib.pyplot as plt # Create a binary image with holes and gaps binary_image = np.zeros((100, 100), dtype=int) binary_image[30:70, 30:70] = 1 # Main object (square) binary_image[40, 40] = 0 # Add a hole binary_image[50, 50] = 0 # Add another hole # Create a circular footprint radius = 3 y, x = np.ogrid[-radius:radius+1, -radius:radius+1] footprint = x**2 + y**2 <= radius**2 # Apply grey_closing with a circular footprint processed_image = ndi.grey_closing(binary_image, footprint=footprint) # Display the original and processed images fig, axes = plt.subplots(1, 2, figsize=(12, 6)) # Original binary image with holes axes[0].imshow(binary_image, cmap='gray') axes[0].set_title("Binary Image with Holes") axes[0].axis("off") # Processed image axes[1].imshow(processed_image, cmap='gray') axes[1].set_title("Image after Circular Footprint Closing") axes[1].axis("off") plt.tight_layout() plt.show()
Here is the output of the function scipy.ndimage.grey_closing() which is used to implement Greyscale closing to handle Non Rectangular Neighborhoods with footprints −
