1

are there any functions or libraries implemented in python for computing the image skeleton (skeletonization) using chamfer distance transform?

the following link is an example of chamfer distance transform: http://www.inf.u-szeged.hu/~palagyi/skel/chamfer34.gif

Thank you

SMA
  • 415
  • 1
  • 5
  • 10
  • Duplicate of https://stackoverflow.com/questions/3437688/how-to-use-chamfer-matching-algorithm-for-finding-similar-images ? – Yonatan Simson Dec 02 '18 at 14:52

2 Answers2

2

Your question was not clearly phrased. Chamfer distance is the distance between two curves or two binary images

Say you have two curves.

  • Curve A
  • Curve B

The simplest way to calculate the Chamfer transform is convert curve A into Distance Transform in a image. Then use the distances to calculate the nearest distance between each point in Curve A and points of curve B.

In other words, the sum of closest point distances between both curves or binary images.

Sample Code

import numpy as np
import cv2

# for Chamfer Distance between two curves
p_a - n x 2 numpy array
p_b - n x 2 numpy array
image_shape - (h, w) tuple
def chamfer(p_a, p_b, image_shape):
    mask = np.ones(image_shape[:2], dtype=np.uint8) * 255
    mask[p_a[:, 1].astype(int), p_a[:, 0].astype(int)] = 0
    dist = cv2.distanceTransform(mask, cv2.DIST_L2, 3,  dstType=cv2.CV_32F)
    return dist[p_b[:, 1].astype(int), p_b[:, 0].astype(int)].sum()

 chamfer_dist = 0.5 * (chamfer(p_a, p_b, image_shape) + chamfer(p_b, p_a, image_shape)

Another option is to use Hausdorff Distance which is considered in some respects to better

Community
  • 1
  • 1
Yonatan Simson
  • 2,395
  • 1
  • 24
  • 35
  • 2
    The chamfer distance transform is an efficient algorithm to compute the distance transform in a discrete, regular grid. – Cris Luengo Dec 02 '18 at 15:34
1

Not sure if this is what you're looking for, but I have an efficient implementation of the TEASAR skeletonization algorithm using the exact euclidean distance transform here: https://github.com/seung-lab/kimimaro

SapphireSun
  • 9,170
  • 11
  • 46
  • 59