Scikit Image - Skeletonizing an Image



Skeletonization is an image processing technique that reduces binary objects in an image to 1-pixel wide representations. The purpose of the skeletonization technique is to reduce the thickness of the objects in the binary image while preserving their essential structure. This is commonly used in image processing applications, like shape analysis, pattern recognition, and feature extraction.

The scikit-image (skimage) library provides the skeletonize() and skeletonize_3d() functions in the morphology module to perform skeletonization of 2D and 3D binary images, respectively.

Using the skimage.morphology.skeletonize() function

The morphology.skeletonize() function is used to compute the skeleton of a binary image. which involves reducing each connected component in the binary image to a single-pixel wide skeleton.

Syntax

Following is the syntax of this function −

skimage.morphology.skeletonize(image, *, method=None)

Parameters

  • image: This parameter should be a 2D or 3D NumPy array representing the binary image you want to skeletonize. In the binary image, zeros represents the background, and nonzero values represent the foreground object of the image.
  • method: This parameter specifies which algorithm to use for skeletonization.

Return Value

It returns a NumPy array representing the thinned image, which is the result of skeletonizing the input binary image.

Example

The following example demonstrates how to use the skimage.morphology.skeletonize() function to skeletonize a binary image.

from skimage.morphology import skeletonize
from skimage import io
import matplotlib.pyplot as plt
from skimage.util import invert

# Load an image
original_image = io.imread('Images/Black1.png', as_gray=True)
binary_image = invert(original_image)

# Perform skeletonization
skeleton = skeletonize(binary_image)

# Display the input and Skeletonized images side by side
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
ax = axes.ravel()

ax[0].imshow(binary_image, cmap=plt.cm.gray)
ax[0].axis('off')
ax[0].set_title('input Binary Image')

ax[1].imshow(skeleton, cmap=plt.cm.gray)
ax[1].axis('off')
ax[1].set_title('Skeletonized Image')

fig.tight_layout()
plt.show()

Output

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

Using the skimage.morphology.skeletonize_3d() function

The morphology.skeletonize_3d() function is used to compute the skeleton of a binary image in three dimensions (3D).

Syntax

Following is the syntax of this function−

skimage.morphology.skeletonize_3d(image)

Parameters

  • image (ndarray): This parameter should be a 2D or 3D NumPy array representing the binary image you want to skeletonize. In the binary image, zeros represent the background, and nonzero values represent the foreground object of the image.

Return Value

It returns a NumPy array representing the thinned image.

Example

The following example demonstrates skeletonization of a 3D structure(binary array) using skeletonize_3d() function.

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from skimage.morphology import ball
from skimage.morphology import skeletonize_3d
from skimage.util import invert

# Generate 3D array using the ball() function
struc_3d = ball(5)

# Perform skeletonization
skeleton_3d = skeletonize_3d(invert(struc_3d))

# Generate 3D structuring elements.
images = {
    "Input 3d Image": struc_3d,
    "3D Skeletonized Image": skeleton_3d
}

# Visualize the elements.
fig = plt.figure(figsize=(15, 15))

idx = 1
for title, image in images.items():
    ax = fig.add_subplot(3, 3, idx, projection=Axes3D.name)
    ax.voxels(image)
    ax.set_title(title)
    idx += 1

fig.tight_layout()
plt.show()

Output

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

Advertisements