Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there any build-in function can do skeletonization in openCV?

Tags:

python

opencv

I found some implementation in C/C++ such as voronoi skeleton. Usually those codes require intensive looping, which is bad in python. Is there any build-in skeleton function can be called in python?

like image 739
Wang Avatar asked Oct 13 '15 06:10

Wang


People also ask

Can OpenCV do image processing?

OpenCV is a pre-built, open-source CPU-only library (package) that is widely used for computer vision, machine learning, and image processing applications.

What is Skeletonization in image processing?

Skeletonization is a process for 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.

What is thinning or Skeletonization algorithm?

Skeletonization and also known as thinning process is an important step in pre-processing phase. Skeletonization is a crucial process for many applications such as OCR, writer identification ect. However, the improvements in this area still remain due to researches recently.

What is binary skeleton in Python?

Skeletonization reduces binary objects to 1 pixel wide representations. This can be useful for feature extraction, and/or representing an object's topology. skeletonize works by making successive passes of the image.


1 Answers

OpenCV doesn't have a skeleton function, but you can make your own function. From here:

The skeleton/MAT can be produced in two main ways.

The first is to use some kind of morphological thinning that successively erodes away pixels from the boundary (while preserving the end points of line segments) until no more thinning is possible, at which point what is left approximates the skeleton.

The alternative method is to first calculate the distance transform of the image. The skeleton then lies along the singularities (i.e. creases or curvature discontinuities) in the distance transform. This latter approach is more suited to calculating the MAT since the MAT is the same as the distance transform but with all points off the skeleton suppressed to zero.

Here you can find an example that uses morphological operations:

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()
like image 178
Miki Avatar answered Oct 02 '22 06:10

Miki