Scikit Image - Diameter Closing and Opening an Image



Diameter closing and opening are morphological operations based on attribute operators also known as connected operators, which are a family of contour-preserving filtering operations used in mathematical morphology. These operations can be implemented using max-trees, which are hierarchical representations of images.

Diameter closing is particularly useful for removing all dark structures of an image while preserving long and thin structures based on their maximal extension. Whereas, the diameter opening removes all bright structures in an image while preserving larger and thin structures.

The scikit image library provides the diameter_closing() and diameter_opening() functions in the morphology module to perform these diameter closing and opening operations on images.

Using the skimage.morphology.diameter_closing() function

The diameter_closing() function is used to perform a diameter-closing operation on an input image. Diameter closing removes dark structures in an image that have a maximal extension (defined as the maximal extension of their bounding box) smaller than a specified threshold. This operation is also known as Bounding Box Closing and is similar to morphological closing, but it preserves long and thin structures.

Syntax

Following is the syntax of this function −

skimage.morphology.diameter_closing(image, diameter_threshold=8, connectivity=1, parent=None, tree_traverser=None)

Parameters

  • image (ndarray): The input image for which the diameter closing operation is to be performed. This image can be of any type.
  • diameter_threshold (unsigned int): The maximal extension parameter in the number of pixels. The default value is 8.
  • connectivity (unsigned int, optional): The neighborhood connectivity. This parameter specifies the maximum number of orthogonal steps to reach a neighbor. In 2D, it is typically set to 1 for a 4-neighborhood and 2 for an 8-neighborhood. The default value is 1.
  • parent (ndarray, int64, optional): A precomputed parent image representing the max tree of the inverted image. Providing a precomputed parent image can speed up the function.
  • tree_traverser (1D array, int64, optional): A precomputed traverser where the pixels are ordered such that every pixel is preceded by its parent (except for the root which has no parent). Providing a precomputed traverser can also speed up the function.

Return Value

It returns an output image of the same shape and type as the input image, where the dark structures that meet the diameter threshold criterion have been preserved.

Example

This example creates an image with multiple local minima and then performs a diameter closing operation on it using the morphology.diameter_closing() function.

import numpy as np
from skimage.morphology import diameter_closing
import matplotlib.pyplot as plt

# Create the image
w = 12
x, y = np.mgrid[0:w, 0:w]
image = 180 + 0.2 * ((x - w/2)**2 + (y - w/2)**2)

# Add local minima
image[2:3, 1:5] = 160
image[2:4, 9:11] = 140
image[9:11, 2:4] = 120
image[9:10, 9:11] = 100
image[10, 10] = 100

# Convert the image to integer
image = image.astype(int)

# Perform diameter closing with a threshold of 3 and connectivity=1
closed = diameter_closing(image, 3, connectivity=1)

# Display the original and closed images
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
ax = axes.ravel()

ax[0].imshow(image, cmap=plt.cm.gray)
ax[0].set_title('Original Image')

ax[1].imshow(closed, cmap=plt.cm.gray)
ax[1].set_title('Diameter Closing (Threshold = 3)')
plt.tight_layout()
plt.show()

Output

On executing the above program, you will get the following output −

Using the skimage.morphology.diameter_opening() function

The diameter_opening() function is used to perform a diameter opening operation on an input image. Diameter opening removes bright structures in an image that have a maximal extension (defined as the maximal extension of their bounding box) smaller than a specified threshold. This operation is also known as Bounding Box Opening and is particularly useful for preserving long and thin structures in an image. It is based on the max-tree representation of the image.

Syntax

Following is the syntax of this function −

skimage.morphology.diameter_opening(image, diameter_threshold=8, connectivity=1, parent=None, tree_traverser=None)

Parameters

  • image (ndarray): The input image for which the diameter opening operation is to be performed. This image can be of any type.
  • diameter_threshold (unsigned int): The maximal extension parameter in number of pixels. The default value is 8.
  • connectivity (unsigned int, optional): The neighborhood connectivity. This parameter specifies the maximum number of orthogonal steps to reach a neighbor. In 2D, it is typically set to 1 for a 4-neighborhood and 2 for an 8-neighborhood. The default value is 1.
  • parent (ndarray, int64, optional): A parent image representing the max tree of the input image. The value of each pixel in this image is the index of its parent in the raveled array.
  • tree_traverser (1D array, int64, optional): The ordered pixel indices referring to the raveled array. Pixels are ordered such that every pixel is preceded by its parent (except for the root, which has no parent).

Return Value

It returns an output image of the same shape and type as the input image, where the bright structures that meet the diameter threshold criterion have been removed.

Example

The following example demonestrates how to use the morphology.diameter_opening() function to perform the diameter opening of an image.

import numpy as np
from skimage.morphology import diameter_opening
import matplotlib.pyplot as plt

# Create the image
w = 12
x, y = np.mgrid[0:w, 0:w]
image = 20 - 0.2 * ((x - w/2)**2 + (y - w/2)**2)

# Add local maxima
image[2:3, 1:5] = 40
image[2:4, 9:11] = 60
image[9:11, 2:4] = 80
image[9:10, 9:11] = 100
image[10, 10] = 100

# Convert the image to integer
image = image.astype(int)

# Perform diameter opening with a threshold of 3 and connectivity=1
opened = diameter_opening(image, 3, connectivity=1)

# Display the original and opened images
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
ax = axes.ravel()

ax[0].imshow(image, cmap=plt.cm.gray)
ax[0].set_title('Original Image')

ax[1].imshow(opened, cmap=plt.cm.gray)
ax[1].set_title('Diameter Opening (Threshold = 3)')
plt.tight_layout()
plt.show()

Output

On executing the above program, you will get the following output −

Advertisements