You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取预测概率图中连通分量的最大概率值及对应坐标

解法步骤

我们可以借助scipy.ndimage的连通分量标记能力实现需求,具体逻辑如下:

  • 首先对原数组做二值化,所有大于0的像素判定为前景,用来识别连通区域(默认4连通,要8连通可以自行调整参数)
  • 调用connected_components得到每个连通区域的标签矩阵
  • 遍历每个独立连通分量,筛选出该分量内所有元素的概率值和对应坐标,取最大值对应的结果即可

完整实现代码

import numpy as np
from scipy.ndimage import connected_components

# 输入数组
a = np.array([[0.1, 0.2, 0.3, 0.0, 0.0, 0.0],
              [0.1, 0.92, 0.3, 0.0, 0.2, 0.1],
              [0.1, 0.9, 0.3, 0.0, 0.7, 0.89],
              [0.0, 0.0, 0.0, 0.0, 0.4, 0.5],
              [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
              [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]])

# 二值化,非0区域作为前景
binary = a > 0
# 标记连通分量,默认4连通,要8连通可加参数structure=np.ones((3,3))
num_labels, labels = connected_components(binary)

# 遍历每个连通分量存储结果
result = []
for label in range(1, num_labels):  # 跳过背景标签0
    # 拿到当前分量的所有坐标和对应值
    mask = labels == label
    values = a[mask]
    coords = np.argwhere(mask)
    # 找最大值的索引
    max_idx = np.argmax(values)
    max_val = values[max_idx]
    max_coord = tuple(coords[max_idx])
    result.append((max_val, max_coord))

# 输出结果
for val, coord in result:
    print(f"最大概率值:{val:.1f},对应坐标:{coord}")

运行输出

最大概率值:0.9,对应坐标:(1, 1)
最大概率值:0.9,对应坐标:(2, 5)

提示:如果需要使用8连通规则判断连通性,只需要在调用connected_components时传入structure=np.ones((3,3))参数即可。

内容的提问来源于stack exchange,提问作者Xxxxiehaofeng

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 11:45:05