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 −

Greyscale Closing Hole Filling Example

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 −

Greyscale Closing Smoothing Example

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 −

Greyscale Closing Non Rectangular Example
scipy_morphological_operations.htm
Advertisements