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

如何向量化获取三维数组中每张图像的最大值坐标?

向量化获取三维数组中每张图像最大值的坐标

直接用NumPy的argmax结合索引转换就能实现无循环的向量化操作,具体代码如下:

  1. 获取每张图像最大值的扁平化索引
flat_indices = np.argmax(subtracted_image, axis=(1, 2))

axis=(1,2)指定沿着每张图像的行、列维度取最大值的索引,得到的flat_indices是长度为522的一维数组,每个元素对应单张87×87图像中最大值的扁平化位置。

  1. 转换为x、y坐标(行、列索引)
# 列坐标(y轴)
y_coords = flat_indices % 87
# 行坐标(x轴)
x_coords = flat_indices // 87

这里的x_coords和y_coords都是长度为522的数组,分别对应每张图像最大值的行、列位置。

  1. (可选)合并坐标为二维数组
    如果需要把坐标整理成(522, 2)的格式,用stack拼接:
max_coords = np.stack([x_coords, y_coords], axis=1)

注意:如果单张图像中有多个相同的最大值,np.argmax只会返回第一个出现的最大值的索引。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 02:55:44