回顾以下95% Hausdorff距离的概念,一张比较经典直观的图:

一、 最快理解

  • max(d_XY, d_YX):取X=>Y距离 和 Y=>X距离的最大值。

        其中

  • X=>Y =max min x=>y :X所有点都和Y集合计算最小的距离,得到的距离集合再取最大值。

        同理

  • Y=>X =max min y=>x :Y所有点都和X集合计算最小的距离,得到的距离集合再取最大值。

二 选取95%

1.针对二分类将预测出来的二值图像划分为布尔前景/背景

2.所有前景的坐标计算

numpy提供了argwhere 方法筛选数组中非0项,然后返回这些项的坐标。

3. 对坐标进行排序:np.sort()

4. 列表切割(所有坐标数*95%)的数组

三、实现

def hausdorff95(X, Y):
    # 将二值图像转换为布尔数组,True 表示前景
    X_foreground = X.astype(bool)
    Y_foreground = Y.astype(bool)

    # 计算前景点的坐标
    X_coords = np.argwhere(X_foreground)
    Y_coords = np.argwhere(Y_foreground)

    # 计算 X 中每个点到 Y 的所有点的距离
    X_to_Y_dist = distance_matrix(X_coords, Y_coords)

    # 计算 Y 中每个点到 X 的所有点的距离
    Y_to_X_dist = distance_matrix(Y_coords, X_coords)

    # 将 X 到 Y 的距离和 Y 到 X 的距离合并为一个数组
    all_distances = np.concatenate((X_to_Y_dist.flatten(), Y_to_X_dist.flatten()))

    # 排除掉最大的5%的距离
    distances_sorted = np.sort(all_distances)
    num_points = len(all_distances)
    threshold_index = int(num_points * 0.95)

    # 确保至少有一个距离被选中
    if threshold_index == 0:
        threshold_index = 1

    # 计算95% Hausdorff 距离
    distances_filtered = distances_sorted[:threshold_index]
    if len(distances_filtered) > 0:
        hd95_value = np.max(distances_filtered)  # 取最大值,而不是平均值
    else:
        hd95_value = 0  # 如果没有距离被选中,可以返回0或其他合适的值

    return hd95_value

Logo

立足具身智能前沿赛道,致力于搭建全球化、开源化、全栈式技术交流与实践共创平台。

更多推荐