Skip to content

Instantly share code, notes, and snippets.

@nicoguaro
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicoguaro/f46548c7ec6daf5e6f1a to your computer and use it in GitHub Desktop.
Save nicoguaro/f46548c7ec6daf5e6f1a to your computer and use it in GitHub Desktop.
Calculate two-side Hausdorff distance between two sets using a brute force approach.
# -*- coding: utf-8 -*-
"""
Calculate two-side Hausdorff distance between two sets using a brute
force approach.
References
[1] http://en.wikipedia.org/wiki/Hausdorff_distance
@author: Nicolas Guarin-Zapata
"""
import numpy as np
def brute_force_hausdorff(set1, set2, dist):
""" Compute the Hausforff distance of two sets
Parameters
----------
set1 : (m) iterable of points
Set of points.
set2 : (n) iterable of points
Set of points.
dist : function
Distance function.
Returns
-------
dH : float
Two side Hausdorff distance for the given distance function.
"""
m = len(set1)
n = len(set2)
M = np.zeros((m,n))
for row, x1 in enumerate(set1):
for col, x2 in enumerate(set2):
M[row, col] = dist(x1, x2)
dH = np.max([np.max(np.min(M,axis=1)), np.max(np.min(M,axis=0))])
return dH
# %%
set1 = [1, 2, 3, 5]
set2 = [1, 2]
def dist(x, y):
return abs(x - y)
dH = brute_force_hausdorff(set1, set2, dist)
print dH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment