Selected Reading

SciPy - ndimage.generate_binary_structure() Function



The scipy.ndimage.generate_binary_structure() is a function in the SciPy library which is used to create a structuring element for binary morphological operations. It generates a multi-dimensional array where True values represent the shape of the structuring element.

This function takes two parameters namely, rank which specifies the number of dimensions and connectivity which determines the extent of connectivity with 1 for a cross shape or 2 for a full neighborhood.

Structuring elements are essential for operations such as dilation, erosion, opening and closing in image processing. This function provides flexibility for defining shapes in both 2D and higher-dimensional spaces.

Syntax

Following is the syntax of the function scipy.ndimage.generate_binary_structure() to create Structuring element −

scipy.ndimage.generate_binary_structure(rank, connectivity)

Parameters

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

  • rank(int): The dimensionality of the structuring element such as 2 for 2D, 3 for 3D, etc.
  • connectivity(int): This parameter determines the type of connectivity or neighborhood. 1 includes only the nearest neighbors in each dimension i.e., cross-shaped neighborhood in 2D and 2 includes diagonal neighbors i.e., full square or cube neighborhood in 2D or 3D.

Return Value

The scipy.ndimage.generate_binary_structure() function returns a binary structuring element represented as a NumPy array of boolean values.

2D Structuring Element with Connectivity 2

A 2D structuring element with connectivity 2 includes all the pixels surrounding the central pixel by forming a square shape which is also called full connectivity. This means that the central pixel is connected to its 8 neighbors. Here's an example of creating and using a 2D structuring element with connectivity 2 −

from scipy.ndimage import generate_binary_structure

# Create a 2D structuring element with connectivity 2
struct_2d_full = generate_binary_structure(rank=2, connectivity=2)

# Print the structuring element
print("2D Structuring Element (Connectivity 2):\n", struct_2d_full)

Here is the output of the function scipy.ndimage.generate_binary_structure() which is used to create a 2D structuring element −

2D Structuring Element (Connectivity 2):
 [[ True  True  True]
 [ True  True  True]
 [ True  True  True]]

Enlarging Structuring Elements

In morphological operations, enlarging a structuring element can make the effect of dilation, erosion or other operations more pronounced. We can enlarge a structuring element using scipy.ndimage.iterate_structure(). This function repeats the structuring element dilation multiple times, effectively increasing its size.

Following is the example which uses the scipy.ndimage.iterate_structure() function along with the scipy.ndimage.generate_binary_structure() function for enlarging structuring element −

from scipy.ndimage import generate_binary_structure, iterate_structure

# Create a 2D cross-shaped structuring element
struct_2d = generate_binary_structure(rank=2, connectivity=1)
print("Original Structuring Element:\n", struct_2d)

# Enlarge the structuring element by 2 iterations
enlarged_struct = iterate_structure(struct_2d, iterations=2)
print("Enlarged Structuring Element (2 iterations):\n", enlarged_struct)

Here is the output of the enlarging the structuring element −

2D Structuring Element (Connectivity 2):
 [[ True  True  True]
 [ True  True  True]
 [ True  True  True]]

Closing with a Structuring Element

Using a structuring element with closing involves applying the closing operation on a binary image. Closing is a morphological operation that fills small holes and gaps in the foreground while preserving the overall shape. It consists of a dilation followed by an erosion.

Following is the example which uses the scipy.ndimage.generate_binary_structure() function in Closing −

from scipy.ndimage import binary_closing, generate_binary_structure
import numpy as np
import matplotlib.pyplot as plt

# Create a binary image with a small gap
image_with_gaps = np.zeros((10, 10), dtype=int)
image_with_gaps[4:7, 4:7] = 1  # Square in the center
image_with_gaps[5, 5] = 0      # A small hole
image_with_gaps[6, 4] = 0      # Gap in the square

# Define a 2D structuring element with connectivity 2 (full connectivity)
struct_2d = generate_binary_structure(rank=2, connectivity=2)

# Apply the closing operation
closed_image = binary_closing(image_with_gaps, structure=struct_2d)

# Plot the results
plt.figure(figsize=(15, 5))

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

# Structuring Element
plt.subplot(1, 3, 2)
plt.title("Structuring Element")
plt.imshow(struct_2d, cmap='gray')
plt.axis('off')

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

plt.show()

Here is the output of the structuring element used in Closing −

Closing with structuring element
Advertisements