avatarNeeramitra Reddy

Summary

This article explains skeletonization, a process of reducing foreground regions in a binary image to a skeletal remnant, using OpenCV in Python.

Abstract

In this article, the author explains the concept of skeletonization in image processing using OpenCV in Python. The author starts by defining binary images and BLOBs (Binary Large Objects), which are groups of connected pixels in a binary image. Skeletonization is described as a process that reduces foreground regions in a binary image to a skeletal remnant that preserves the extent and connectivity of the original region while throwing away most of the original foreground pixels. The author explains the importance of skeletonization in various applications, such as object representation, retrieval, manipulation, matching, registration, tracking, recognition, and compression. The article also provides a step-by-step guide on how to perform skeletonization using OpenCV in Python, including the necessary libraries, loading the image in grayscale, thresholding the image, and applying the skeletonization algorithm. The article concludes by showing an example of an elephant image that has been skeletonized using the explained method.

Bullet points

  • Skeletonization is a process of reducing foreground regions in a binary image to a skeletal remnant.
  • Skeletonization preserves the extent and connectivity of the original region while throwing away most of the original foreground pixels.
  • Skeletonization is widely used in various applications, such as object representation, retrieval, manipulation, matching, registration, tracking, recognition, and compression.
  • Skeletonization can facilitate quick and accurate image processing on the light skeleton instead of an otherwise large and memory-intensive operation on the original image.
  • To perform skeletonization, the necessary libraries, including OpenCV, must be imported.
  • The image must be loaded in grayscale and thresholded before applying the skeletonization algorithm.
  • The skeletonization algorithm uses the basic morphological operations of dilation and erosion.
  • The algorithm repeatedly erodes the image and refines the skeleton until the original image is completely eroded.
  • The article provides an example of an elephant image that has been skeletonized using the explained method.

Skeletonization in Python using OpenCV

Let me start off with a (not so) funny joke.

Why are skeletons so calm?

Because nothing gets under their skin.

What the heck is Skeletonization?

Before talking about skeletonization, let’s understand what Binary Image and BLOBs are.

Binary Image as the name suggests is an image where the value of every pixel in the image is either 0 (Pure Black) or 255 (Pure White).

A binary image

BLOB (Binary Large Object) refers to a group of connected pixels in a binary image.

Black BLOBs are circled in Red in this binary image

Skeletonization is a process of reducing foreground regions in a binary image to a skeletal remnant that largely preserves the extent and connectivity of the original region while throwing away most of the original foreground pixels.

In simpler words, Skeletonization makes a BLOB very thin (typically 1 pixel).

This example illustrates skeletonization perfectly

Why Skeletonize?

Skeletonization is widely used in object representation, retrieval, manipulation, matching, registration, tracking, recognition, compression, etc

It can facilitate quick and accurate image processing on the light skeleton instead of an otherwise large and memory-intensive operation on the original image.

Let’s start.

Let’s start off by importing the necessary libraries:

import cv2
import numpy as np

We need a guinea pig a.k.a test image to work on. I will be using the below image.

Why an Elephant you ask? Well, I like Elephants and they are kinda cool I guess.

Download the above image or any other image of your choice and name it as testimg.

Let’s load the image in grayscale and binarize it using thresholding.

Wait a second, what’s thresholding?

Thresholding is the process of converting a grayscale image into a binary image. Based on a certain threshold value, the pixels with value greater than the threshold will be converted to 255 (White) and lesser than the threshold will be converted to 0 (Black).

#Load the image in grayscale
img = cv2.imread('A://testimg.jpg',0)
#Threshold the image with threshold value 127
ret,img = cv2.threshold(img,127,255,0)
Thresholded Image

Time to address the Elephant in the room now.

How the hell do you skeletonize?

I found an excellent blog that demonstrates an excellent method implemented in C++. I will be using the same method here.

How to skeletonize?

A skeleton can be computed using only the two basic morphological operations: dilate and erode.

Let’s talk about dilation and erosion first.

Dilation: In extremely simple terms, this operation causes white regions within an image to “grow” or “dilate”.

ORIGINAL vs DILATED

Erosion: In extremely simple terms, this operation causes white regions within an image to “shrink” or “erode”.

ORIGINAL vs ERODED

Both erosion and dilation make use of something called a kernel.

The Kernel is a matrix usually shaped as a plus or a cross that is convoluted with the image matrix to get the result.

Opening is simply Erosion followed by Dilation.

Opening is extremely useful in removing noise as illustrated below.

ORIGINAL vs OPENED

So the process is basically repeatedly eroding the image and refining the skeleton.

STEPS:

  1. Starting off with an empty skeleton.
  2. Computing the opening of the original image. Let’s call this open.
  3. Substracting open from the original image. Let’s call this temp.
  4. Eroding the original image and refining the skeleton by computing the union of the current skeleton and temp.
  5. Repeat Steps 2–4 till the original image is completely eroded.

That’s all guys!

Complete Code:

Here’s our beloved elephant in all it’s skeletonized glory.

Computer Vision
Image Processing
Skeleton
Opencv Python
Opencv
Recommended from ReadMedium