我有多个网格(numpy数组[Nk,Ny,Nx]),想要使用Hausdorff距离作为这些网格相似性的度量标准。scipy中有几个模块(scipy.spatial.distance.cdist、scipy.spatial.distance.pdist)可以计算2D数组之间的欧几里德距离。现在,为了比较网格,我必须选择一些横截面(例如grid1 [0,:]和grid2 [0,:]),并将它们彼此比较。是否可能直接计算3D网格之间的Hausdorff距离?
我有多个网格(numpy数组[Nk,Ny,Nx]),想要使用Hausdorff距离作为这些网格相似性的度量标准。scipy中有几个模块(scipy.spatial.distance.cdist、scipy.spatial.distance.pdist)可以计算2D数组之间的欧几里德距离。现在,为了比较网格,我必须选择一些横截面(例如grid1 [0,:]和grid2 [0,:]),并将它们彼此比较。是否可能直接计算3D网格之间的Hausdorff距离?
我是新手,但面临着相同的挑战,尝试在三维层面直接解决它。
以下是我编写的函数:
def Hausdorff_dist(vol_a,vol_b):
dist_lst = []
for idx in range(len(vol_a)):
dist_min = 1000.0
for idx2 in range(len(vol_b)):
dist= np.linalg.norm(vol_a[idx]-vol_b[idx2])
if dist_min > dist:
dist_min = dist
dist_lst.append(dist_min)
return np.max(dist_lst)
def bbox(array, point, radius):
a = array[np.where(np.logical_and(array[:, 0] >= point[0] - radius, array[:, 0] <= point[0] + radius))]
b = a[np.where(np.logical_and(a[:, 1] >= point[1] - radius, a[:, 1] <= point[1] + radius))]
c = b[np.where(np.logical_and(b[:, 2] >= point[2] - radius, b[:, 2] <= point[2] + radius))]
return c
还有用于距离计算的另一段代码:
def hausdorff(surface_a, surface_b):
# Taking two arrays as input file, the function is searching for the Hausdorff distane of "surface_a" to "surface_b"
dists = []
l = len(surface_a)
for i in xrange(l):
# walking through all the points of surface_a
dist_min = 1000.0
radius = 0
b_mod = np.empty(shape=(0, 0, 0))
# increasing the cube size around the point until the cube contains at least 1 point
while b_mod.shape[0] == 0:
b_mod = bbox(surface_b, surface_a[i], radius)
radius += 1
# to avoid getting false result (point is close to the edge, but along an axis another one is closer),
# increasing the size of the cube
b_mod = bbox(surface_b, surface_a[i], radius * math.sqrt(3))
for j in range(len(b_mod)):
# walking through the small number of points to find the minimum distance
dist = np.linalg.norm(surface_a[i] - b_mod[j])
if dist_min > dist:
dist_min = dist
dists.append(dist_min)
return np.max(dists)
如果有人多年后仍在寻找此问题的答案……自2016年起,scipy现在包含一个用于计算三维Hausdorff距离的函数:
有一个名为point_cloud_utils的软件包,它提供了一些3D度量,例如Hausdorff距离。使用pip install point_cloud_utils
安装它,然后按照以下方式使用:
import point_cloud_utils as pcu
import numpy as np
# Generate two random point sets
a = np.random.rand(1000, 3)
b = np.random.rand(500, 3)
# Compute one-sided squared Hausdorff distances
hausdorff_a_to_b = pcu.one_sided_hausdorff_distance(a, b)
hausdorff_b_to_a = pcu.one_sided_hausdorff_distance(b, a)
# Take a max of the one sided squared distances to get the two sided Hausdorff distance
hausdorff_dist = pcu.hausdorff_distance(a, b)