语义分割评价指标——95% Hausdorff距离

回顾以下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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值