Skeletonization, also known as thinning, is a technique that is used to reduce the thickness of the shapes within an image while preserving their essential structure.
Skeletonization aims to transform a binary image into a simplified representation where the essential shape characteristics are retained as a skeleton or a one-pixel-wide representation. The process involves iteratively removing pixels from the edges of shapes within the image until only the skeleton remains. The resulting skeleton retains the topological and connectivity properties of the original shapes while reducing them to their essential structural elements.
Here is a simplified explanation of the steps involved in the skeletonization algorithm.
Initialization: Start with a binary image in which objects are represented by foreground pixels and the background by background pixels.
Iterative process: The algorithm proceeds with multiple passes over the image, identifying and removing border pixels of objects while maintaining their connectivity.
Border pixel removal: Border pixels meeting specific conditions are identified and removed during each pass. These conditions ensure that removing pixels does not break the connectivity or alter the topology of the shapes in the image.
Iterative convergence: The process continues iteratively until no more pixels can be removed without violating the conditions that maintain the connectivity and topology of the objects. At this point, the resulting image contains a skeletonized representation of the original shapes.
Let’s implement the process using the skeletonize()
method, which effectively applies the skeleton algorithm to derive the image’s skeleton.
skeletonize()
methodThe skeletonize()
method computes the skeleton of a 2D binary image. It takes an image that contains the objects to be skeletonized. Zeros represent the background, while ones represent the foreground.
Let’s run the following example to understand the working of the skeletonize()
method.
from skimage.morphology import skeletonizefrom skimage import dataimport matplotlib.pyplot as pltfrom skimage.util import invertimage = invert(data.binary_blobs())skeleton_image = skeletonize(image)figure, axes = plt.subplots(nrows=1, ncols=2, figsize=(8, 6))axis = axes.ravel()axis[0].imshow(image, cmap=plt.cm.gray)axis[0].axis('off')axis[0].set_title('Original image', fontsize=20)axis[1].imshow(skeleton_image, cmap=plt.cm.gray)axis[1].axis('off')axis[1].set_title('Skeleton of image', fontsize=20)figure.tight_layout()plt.show()
Lines 1–4: We import the skeletonize()
function from the skimage.morphology
module, the data
module from the skimage
package, and the invert()
function from the skimage.util
module. Additionally, we import the pyplot
submodule from the matplotlib
library as plt
, which is used for visualizing images.
Line 6: We load a sample binary image of blobs using data.binary_blobs()
, invert the colors using invert()
, and assigns it to the image
variable. Inverting the image is optional and depends on the desired input for the skeletonization process.
Line 7: The skeletonize()
function is used on the image
to generate its skeletonized version, which is stored in the skeleton_image
variable.
Lines 9–18: We display the original image and skeleton of the image using matplotlib
.
Skeletonization in image processing is essential for simplifying complex shapes into their basic structures, facilitating tasks like object recognition and pattern analysis. Its applications span various fields including biomedical imaging for analyzing blood vessels and neuronal networks, robotics for path planning, and industrial inspection for detecting defects in manufactured products.
Free Resources