What is skeletonization in image processing?
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.
Steps involved in skeletonization
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.
The skeletonize() method
The 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()
Code explanation
Lines 1–4: We import the
skeletonize()function from theskimage.morphologymodule, thedatamodule from theskimagepackage, and theinvert()function from theskimage.utilmodule. Additionally, we import thepyplotsubmodule from thematplotliblibrary asplt, which is used for visualizing images.Line 6: We load a sample binary image of blobs using
data.binary_blobs(), invert the colors usinginvert(), and assigns it to theimagevariable. Inverting the image is optional and depends on the desired input for the skeletonization process.Line 7: The
skeletonize()function is used on theimageto generate its skeletonized version, which is stored in theskeleton_imagevariable.Lines 9–18: We display the original image and skeleton of the image using
matplotlib.
Conclusion
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