close

Skeletonization using OpenCV-Python

home

Skeletonization using OpenCV-Python

opencvpython.blogspot.com

Skeletonization using OpenCV-Python

I see people asking an algorithm for skeletonization very frequently. At first, I had no idea about it. But today, I saw a blog which demonstrates simple method to do this. Code was in C++, so I would like to convert it to Python here.

What is Skeletonization?


Skeletonization using OpenCV-Python



Answer is just right in the term. Simply, it make a thick blob very thin, may be one pixel width. Visit the wikipedia page for more details : Topological Skeleton

Code : 

import cv2
import numpy as np

img = cv2.imread('sofsk.png',0)
size = np.size(img)
skel = np.zeros(img.shape,np.uint8)

ret,img = cv2.threshold(img,127,255,0)
element = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
done = False

while( not done):
eroded = cv2.erode(img,element)
temp = cv2.dilate(eroded,element)
temp = cv2.subtract(img,temp)
skel = cv2.bitwise_or(skel,temp)
img = eroded.copy()

zeros = size - cv2.countNonZero(img)
if zeros==size:
done = True

cv2.imshow("skel",skel)
cv2.waitKey(0)
cv2.destroyAllWindows()

Below is the result I got:

Skeletonization using OpenCV-PythonSkeletonization using OpenCV-Python





References : 

1) http://felix.abecassis.me/2011/09/opencv-morphological-skeleton/

Share

1 Comments on OpenCV-Python: Skeletonization using OpenCV-Python

  • Anonymous
    on March 22, 2013 | 02:45 Anonymoussaid :
    "great article. Do you have any idea how to remove those small 'hooks/ending' from skeleton"
Skeletonization using OpenCV-Python

Report "Skeletonization using OpenCV-Python"

Are you sure you want to report this post for ?

Cancel
×