Your question was not clearly phrased. Chamfer distance is the distance between two curves or two binary images
Say you have two curves.
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