【问题标题】:Indexing a batch of images (3D numpy array) using a 2D array to get pixel values for each image使用 2D 数组索引一批图像(3D numpy 数组)以获取每个图像的像素值
【发布时间】:2021-08-10 18:45:45
【问题描述】:

我有一个代表一批图像的 3D 数组 (batch_size x W x H)

images = np.random.rand(8,100,100)

和一个二维数组 (batch_size x 2)

indices = np.random.randint(0, 100, size=(8, 2))

其中包含用于访问图像像素值的 x,y 索引。以下

images[:,indices[:,0],indices[:,1]

返回一个形状 (8,8) 的数组,但我想获得一个形状 (8,) 的数组,其中包含存储在 indices 中的各个 x、y 位置的每个图像的像素值。

寻找一种不使用 for 循环来索引数组的方法。

【问题讨论】:

    标签: python arrays indexing


    【解决方案1】:

    我想我解决了。只需要得到输出的对角线:

    output = images[:,indices[:,0],indices[:,1]

    np.diag(output) 给出每个图像在 x,y 位置(索引)的像素值。

    【讨论】:

      猜你喜欢
      • 2012-12-31
      • 2021-08-23
      • 1970-01-01
      • 1970-01-01
      • 2018-10-26
      • 1970-01-01
      • 1970-01-01
      • 2021-07-14
      • 2019-12-23
      相关资源
      最近更新 更多