Understanding Hausdorff Distance


I want to use hausdorff distance to evaluate the quality of my segmentation algorithm and so I try examples from skimage and scipy but weirdly they give me differents results …

Does someone know why ? Which one should I use ?

import numpy as np
# create data
shape = (60, 60)
image = np.zeros(shape)
x_diamond, y_diamond = 30, 30
r = 10
plt_x = [0, 1, 0, -1]
plt_y = [1, 0, -1, 0]
set_ax = [(x_diamond + r * x) for x in plt_x]
set_ay = [(y_diamond + r * y) for y in plt_y]
x_kite, y_kite = 30, 30
x_r, y_r = 15, 20
set_bx = [(x_kite + x_r * x) for x in plt_x]
set_by = [(y_kite + y_r * y) for y in plt_y]
# ====== SKIMAGE =========
from skimage import metrics
coords_a = np.zeros(shape, dtype=bool)
coords_b = np.zeros(shape, dtype=bool)
for x, y in zip(set_ax, set_ay):
    coords_a[(x, y)] = True
for x, y in zip(set_bx, set_by):
    coords_b[(x, y)] = True
metrics.hausdorff_distance(coords_a, coords_b)
# >>> return 10

# ====== SCIPY =========
from scipy.spatial.distance import directed_hausdorff
set_a = np.array((set_ax, set_ay))
set_b = np.array((set_bx, set_by))
directed_hausdorff(set_a, set_b)
# >>> return 14.142135623730951

By definition, the Hausdorff distance is the largest distance from a point in one set to the closest point in the other set so it needs a measure to evaluate that distance. What I suspect is that the two libraries differ with respect to the underlying distance measure they use by default. You’d probably have to look at the source code for confirmation if it’s not in the documentation. Check also if there’s an option to specify the distance measure and make it the same in both cases to see if now the result is the same.

Both documentation say that they use Euclidean distance. I have checked the source code and it seems that skimage cKDTree and scipy 2 for loop for computing the result …

I missed it initially but you’re using the directed Hausdorff distance in one case and the Hausdorff distance in the other. If d(a,b) is the directed Hausdorff distance between set a and set b then the Hausdorff distance is max(d(a,b), d(b,a)).

Yes I have also tried it, it changes nothing

Ok, I found my error ! Skimage want in input an image of boolean and scipy want the associated coordinates