如何获取 numpy 数组的特定索引值?

问题描述 投票:0回答:1

我有一个 numpy 数组(形状:(256, 256, 3))图像和

我有用于索引的 numpy 数组,如下所示。

array([[  3, 230],
       [  3, 231],
       [  3, 232],
       ...,
       [114, 151],
       [115, 149],
       [115, 150]], dtype=int64)

但是,我想获取图像的 numpy 数组(形状:(256, 256, 3))的值

索引如下。这样,我可以获得特定索引的 3 个通道值。

array([[  3, 230, :],
       [  3, 231, :],
       [  3, 232, :],
       ...,
       [114, 151, :],
       [115, 149, :],
       [115, 150, :]], dtype=int64)

如何获取值?

numpy
1个回答
0
投票

您可以像这样索引到图像数组的第一个维度:

import numpy as np

# create an example image with different entries for each channel
image = (np.ones((256, 256)) * np.arange(3).reshape(3, 1, 1)).T

idx = np.asarray([[3, 230],
                  [3, 231],
                  [3, 232],
                  [114, 151],
                  [115, 149],
                  [115, 150]], dtype=np.int64)

values = image[idx[:, 0], idx[:, 1]]

print(values)

哪个打印:

[[0. 1. 2.]
 [0. 1. 2.]
 [0. 1. 2.]
 [0. 1. 2.]
 [0. 1. 2.]
 [0. 1. 2.]]

我希望这有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.