【发布时间】:2021-01-18 07:28:04
【问题描述】:
图像数组的形状是(540, 960, 3),它是这样的:
img_rgb = [[[ 95 71 71]
[ 95 71 71]
[ 95 71 71]
...
[182 171 181]
[182 171 181]
[182 171 181]]
[[ 95 71 70]
[ 95 71 70]
[ 95 71 71]
...
[183 172 182]
[183 172 182]
[183 172 182]]
[[ 95 72 70]
[ 95 71 70]
[ 95 71 71]
...
[183 172 182]
[183 172 182]
[183 172 182]]
...
[[ 36 35 45]
[ 36 35 45]
[ 36 35 45]
...
[ 49 45 50]
[ 49 45 50]
[ 49 45 50]]
[[ 36 35 45]
[ 36 35 45]
[ 36 35 45]
...
[ 49 45 50]
[ 49 45 50]
[ 49 45 50]]
[[ 36 35 45]
[ 36 35 45]
[ 36 35 45]
...
[ 49 45 50]
[ 49 45 50]
[ 49 45 50]]]
我想通过indices 获取指示每个元素索引的元素,indices 如下所示:
indices = [
[0, 0], [0, 1], [0, 2]
]
预期输出
[
[ 95 71 71],
[ 95 71 71],
[ 95 71 71],
]
这些链接中有两个类似的问题,一个是Python numpy 2D array sum over certain indices,另一个是Finding the (x,y) indexes of specific (R,G,B) color values from images stored in NumPy ndarrays。
当我尝试img_rgb[tuple(indices)] 提出的第一个问题时,它得到了IndexError: too many indices for array。
【问题讨论】:
-
这能回答你的问题吗? Indexing numpy array with another numpy array - 你需要转置:
img_rgb[tuple(np.array(indices).T)] -
@DanielF 或
img_rgb[tuple(zip(*indices))] -
谢谢,
img_rgb[tuple(np.array(indices).T)]工作。