SciPy - ndimage.grey_dilation() Function



The scipy.ndimage.grey_dilation() is a function used to perform grayscale dilation on an image. Dilation in the view of Image Processing refers to expanding the boundaries of bright regions in a grayscale image by making them larger.

This function works by applying a structuring element over the image and for each pixel by replacing it with the maximum value of the pixels in the neighborhood defined by the structuring element. The result is an image where bright regions grow and dark regions shrink.

It is commonly used in image processing for noise removal by enhancing features or isolating specific structures in an image.

Syntax

Following is the syntax of the function scipy.ndimage.grey_dilation() to perform grey scale dilation on an image −

scipy.ndimage.grey_dilation(input, size=None, footprint=None, structure=None, output=None, mode='reflect', cval=0.0, origin=0)

Parameters

Following are the parameters of the scipy.ndimage.grey_dilation() function −

  • input: The input image or array on which the grayscale dilation operation is applied. It can be a binary or grayscale image.
  • size (optional): The size of the structuring element used for the dilation 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. Available modes include '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_dilation() function returns an array of the same shape as the input where each pixel's value is replaced by the maximum value in the neighborhood defined by the structuring element.

Example of Basic Grayscale Dilation

Following is the example of the function scipy.ndimage.grey_dilation() in which grey scale Dilation operation is applied on a simple 2D array using a default 3x3 structuring element −

import numpy as np
import scipy.ndimage as ndimage
import matplotlib.pyplot as plt

# Create a sample 2D grayscale image
image = np.array([[1, 2, 3],
                  [4, 5, 6],
                  [7, 8, 9]])

# Apply grey dilation with default 3x3 size
dilated_image = ndimage.grey_dilation(image, size=(3, 3))

# Display the original and dilated images
plt.figure(figsize=(8, 4))
plt.subplot(1, 2, 1)
plt.title("Original Image")
plt.imshow(image, cmap='gray')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.title("Grey Dilation")
plt.imshow(dilated_image, cmap='gray')
plt.axis('off')

plt.tight_layout()
plt.show()

Following is the output of the function scipy.ndimage.grey_dilation() which is used to implement basic Example of Greyscale Dilation −

Greyscale Dilation Basic Example

Grayscale Dilation with Larger Structuring

A larger structuring element will dilate the image more by considering a broader neighborhood of pixels for the operation. To perform grey dilation with a larger structuring element we can specify size parameter as larger for the grey_dilation() function. Here's an example that shows how the grey dilation with a larger structuring element works −

import numpy as np
import scipy.ndimage as ndimage
import matplotlib.pyplot as plt

# Create a sample 2D grayscale image
image = np.array([[1, 2, 3],
                  [4, 5, 6],
                  [7, 8, 9]])

# Apply grey dilation with a 5x5 structuring element
dilated_image = ndimage.grey_dilation(image, size=(5, 5))

# Display the original and dilated images
plt.figure(figsize=(8, 4))
plt.subplot(1, 2, 1)
plt.title("Original Image")
plt.imshow(image, cmap='gray')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.title("Grey Dilation (5x5 Structuring Element)")
plt.imshow(dilated_image, cmap='gray')
plt.axis('off')

plt.tight_layout()
plt.show()

Here is the output of the function scipy.ndimage.grey_dilation() which implement Greyscale Dilation with larger structuring element−

Greyscale Dilation larger structuring element Example

Grayscale Dilation with Different Origin

In the view of grey dilation the origin parameter specifies the position of the structuring element relative to the current pixel. By default the origin is at the center of the structuring element i.e., origin=0. However by changing the origin shifts the structuring element which can affect how dilation is applied to the image. Here's an example that shows how to perform grey dilation with different origins is applied to an image −

import numpy as np
import scipy.ndimage as ndimage
import matplotlib.pyplot as plt

# Create a sample 2D grayscale image
image = np.array([[1, 2, 3],
                  [4, 5, 6],
                  [7, 8, 9]])

# Apply grey dilation with a 3x3 structuring element and origin=1
dilated_image_origin_1 = ndimage.grey_dilation(image, size=(3, 3), origin=1)

# Apply grey dilation with a 3x3 structuring element and origin=-1
dilated_image_origin_minus_1 = ndimage.grey_dilation(image, size=(3, 3), origin=-1)

# Display the original and dilated images
plt.figure(figsize=(12, 6))

# Original image
plt.subplot(1, 3, 1)
plt.title("Original Image")
plt.imshow(image, cmap='gray')
plt.axis('off')

# Dilation with origin=1
plt.subplot(1, 3, 2)
plt.title("Grey Dilation (origin=1)")
plt.imshow(dilated_image_origin_1, cmap='gray')
plt.axis('off')

# Dilation with origin=-1
plt.subplot(1, 3, 3)
plt.title("Grey Dilation (origin=-1)")
plt.imshow(dilated_image_origin_minus_1, cmap='gray')
plt.axis('off')

plt.tight_layout()
plt.show()

Below is the output of the function scipy.ndimage.grey_dilation() which implement Greyscale Dilation with different origin −

Greyscale Dilation with origin Example
scipy_morphological_operations.htm
Advertisements