In opencv if i use findcontours function, i get contours and i can compute area, perimeter, convex hull. if i use a Skeletonized image of a circle how can i compute perimeter,area, convex hull of the skeletonized circle?



enter image description here

My skeletonize function:

import numpy as np
import cv2
import scipy.ndimage.morphology as m

def skeletonize(img):
    h1 = np.array([[0, 0, 0],[0, 1, 0],[1, 1, 1]]) 
    m1 = np.array([[1, 1, 1],[0, 0, 0],[0, 0, 0]]) 
    h2 = np.array([[0, 0, 0],[1, 1, 0],[0, 1, 0]]) 
    m2 = np.array([[0, 1, 1],[0, 0, 1],[0, 0, 0]])    
    hit_list = [] 
    miss_list = []
    for k in range(4): 
        hit_list.append(np.rot90(h1, k))
        hit_list.append(np.rot90(h2, k))
        miss_list.append(np.rot90(m1, k))
        miss_list.append(np.rot90(m2, k))    
    img = img.copy()
    while True:
        last = img
        for hit, miss in zip(hit_list, miss_list): 
            hm = m.binary_hit_or_miss(img, hit, miss) 
            img = np.logical_and(img, np.logical_not(hm)) 
        if np.all(img == last):  
    return img

nomeimg = "JPEG/e(5).jpg"    
img = cv2.imread(nomeimg,0)
ret,img = cv2.threshold(img,127,255,0)
element = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
img = 255 - img
img = cv2.dilate(img, element, iterations=7)
img = cv2.erode(img, element)

#element = cv2.getStructuringElement(cv2.MORPH_CROSS,(6,6)) 

skel = skeletonize(img)
skel = skel.astype(np.uint8)*255

It is not clear if the skeletonized image can be reversed to a contour image. Skeletonization is often done by calculating the distance transform and then thresholding the result or performing a non-maximum suppression. At this point you have already lost too much information and the features, you are asking for cannot be calculated.

If you have the distance transform, you can approximate those values:

  • You get the radius ($r$) of the circle from the height of peak which represents the circle (the skeleton of an ideal circle would be a dot)
  • Perimeter = $2\pi r$
  • Area = $\pi r^2$
  • Convex hull $\rightarrow$ draw a circle with radius $r$ at the position where the peak is
| improve this answer | |
  • $\begingroup$ So it's better extract contour from an image than a skeletonization? Is has any sens retrieve perimeter, area convex hull from a skeleton or not? $\endgroup$ – postgres Mar 4 '13 at 13:40
  • $\begingroup$ If you want to calculate shape features, it is probably easier to use the contour itself. $\endgroup$ – bjoernz Mar 4 '13 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.