SciPy - ndimage.binary_dilation() Function



The scipy.ndimage.binary_dilation() is a function in SciPy, which is used to perform the binary dilation operation on a binary image or array. Dilation expands the boundaries of foreground objects i.e., pixels with value 1 in the image by adding neighboring background pixels i.e., value 0 to the objects.

This function takes the input binary image and applies a structuring element i.e., a kernel, that defines the shape and size of the neighborhood for dilation. The output is a new binary image where foreground regions grow based on the structuring element.

The common parameters include 'structure' for defining the kernel, 'iterations' for repeated dilation and 'origin' for adjusting the kernel's placement.

Syntax

Following is the syntax of the function scipy.ndimage.binary_dilation() to perform Dilation operation on an image −

scipy.ndimage.binary_dilation(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_dilation() function −

  • input: The input image or array which can be binary or grayscale, on which the binary dilation operation is applied.
  • structure(optional): It is an array that explicitly specifies the structuring element. By default a cross-shaped structuring element is used.
  • iterations(optional): The number of times the dilation operation is applied. By default it is set to 1. If -1 then dilation is applied until the result no longer changes.
  • mask(optional): A binary mask that limits the dilation operation to specific areas of the input image and only regions where the mask is non-zero will be dilated.
  • 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 with a default value of 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 dilation, which can be slower but may handle certain custom structuring elements more effectively. The default value is False.

Return Value

The scipy.ndimage.binary_dilation() function returns the binary image after applying the dilation.

Basic Binary Dilation

The default structuring element is a cross-shaped kernel which dilates the input binary image by expanding foreground regions. Following is the example which shows how to use scipy.ndimage.binary_dilation() with its default settings −

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

# Create a binary input image
image = np.array([
    [0, 0, 0, 0, 0],
    [0, 1, 1, 0, 0],
    [0, 1, 0, 1, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0]
], dtype=bool)

# Apply binary dilation with default parameters
dilated_image = binary_dilation(image)

# Visualize the input and output
plt.figure(figsize=(10, 5))

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

# Dilated Image
plt.subplot(1, 2, 2)
plt.title("Dilated Image (Default Parameters)")
plt.imshow(dilated_image, cmap='gray')
plt.axis('off')

plt.tight_layout()
plt.show()

Here is the output of the function scipy.ndimage.binary_dilation() which is used to implement basic Example of Binary Dilation −

Dilation Basic Example

Binary Dilation with a Custom Structuring

The structuring element will be different from the default cross-shaped kernel. In this example we will use a custom structuring element i.e., disk-shaped structuring element will be used and we will apply the dilation operation to expand the foreground regions of a binary image −

import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import binary_dilation
from skimage.morphology import disk

# Create a binary input image
image = np.array([
    [0, 0, 0, 0, 0],
    [0, 1, 1, 0, 0],
    [0, 1, 0, 1, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0]
], dtype=bool)

# Create a custom disk-shaped structuring element with radius 2
structuring_element = disk(2)

# Apply binary dilation with the custom structuring element
dilated_image = binary_dilation(image, structure=structuring_element)

# Visualize the input and output
plt.figure(figsize=(10, 5))

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

# Dilated Image
plt.subplot(1, 2, 2)
plt.title("Dilated Image (Custom 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.binary_dilation() which is used to implement Binary Dilation with custom Structuring element −

Dilation Custom Example

Binary Dilation Multiple Times

In this example, we will show how to apply binary dilation multiple times using the iterations parameter. This parameter allows us to repeat the dilation operation multiple times which will progressively expand the foreground regions of the binary image −

import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import binary_dilation
from skimage.morphology import disk

# Create a binary input image
image = np.array([
    [0, 0, 0, 0, 0],
    [0, 1, 1, 0, 0],
    [0, 1, 0, 1, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0]
], dtype=bool)

# Create a custom disk-shaped structuring element with radius 1
structuring_element = disk(1)

# Apply binary dilation with 2 iterations
dilated_image_2_iter = binary_dilation(image, structure=structuring_element, iterations=2)

# Apply binary dilation with 3 iterations
dilated_image_3_iter = binary_dilation(image, structure=structuring_element, iterations=3)

# Visualize 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')

# Dilated Image (2 Iterations)
plt.subplot(1, 3, 2)
plt.title("Dilated Image (2 Iterations)")
plt.imshow(dilated_image_2_iter, cmap='gray')
plt.axis('off')

# Dilated Image (3 Iterations)
plt.subplot(1, 3, 3)
plt.title("Dilated Image (3 Iterations)")
plt.imshow(dilated_image_3_iter, cmap='gray')
plt.axis('off')

plt.tight_layout()
plt.show()

Here is the output of the function scipy.ndimage.binary_dilation() which is used to implement Binary Dilation with iteration −

Dilation iteration Example
scipy_morphological_operations.htm
Advertisements