簡體   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