繁体   English   中英

NumPy 3D 数组:从二维坐标列表中获取项目

[英]NumPy 3D Array: Get items from a list of 2D co-ordinates

我有一个 numpy 数组,如下所示。

img = [
  [
    [135. 100.  72.],
    [124. 102.  63.],
    [161.  67.  59.],
    [102.  92. 165.],
    [127. 215. 155.]
  ],
  [
    [254. 255. 255.],
    [216. 195. 238.],
    [109. 200. 141.],
    [ 99. 141. 153.],
    [ 55. 200.  95.]
  ],
  [
    [255. 254. 255.],
    [176. 126. 221.],
    [121. 185. 158.],
    [134. 224. 160.],
    [168. 136. 113.]
  ]
]

然后我有另一个数组,如下所示。 我想将此视为前一个的坐标数组

crds = [
  [1, 3], # Corresponds to [ 99. 141. 153.] in img
  [2, 2] # Corresponds to [121. 185. 158.] in img
]

我需要从img数组中提取以下结果。

[
  [ 99. 141. 153.],
  [121. 185. 158.]
]

我如何实现这一目标? 我可以在不迭代的情况下做到这一点吗?

假设两个 numpy arrays 作为输入:

img = np.array(img)
crds = np.array(crds)

您可以使用:

img[crds[:,0], crds[:,1]]

output:

array([[ 99, 141, 153],
       [121, 185, 158]])

抱歉,如果我不完全理解您的问题,但您可以使用坐标索引作为数组的索引。 例如,

打印(img[crds[0, 0], crds[0, 1]])

将打印 [99 141 153] 的结果。

打印(img[crds[1, 0], crds[1, 1]])

将打印 [121 185 158] 的结果。

这是我第一次尝试回答问题,所以如果我误解了,我很抱歉。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM