【问题标题】:Mean RGB image out of 4D np.array4D np.array 中的平均 RGB 图像
【发布时间】:2020-05-10 00:00:16
【问题描述】:

我有一个 4D 数据 np.array,由 150 个 50X70 的图像组成,分解为 3 个通道。
数据的形状为 (150, 50, 70, 3)。 我需要按通道计算这 150 张图像的平均值,形状为 (50, 70, 3) (其中平均 R 通道将是 150 个 R 通道的平均值等) 我试过了:

average = data.mean(axis=0) averageimage = Image.fromarray(average, 'RGB') averageimage.show()

但即使它给出了正确的形状,图像看起来也像是随机的颜色噪声。

编辑:我试过了

def average_image(a_lot_of_images): avg = np.zeros((a_lot_of_images.shape[1], a_lot_of_images.shape[2], a_lot_of_images.shape[3])) for i in range(a_lot_of_images.shape[0]): avg[:,:,0] += a_lot_of_images[i,:,:,0] avg[:,:,1] += a_lot_of_images[i,:,:,1] avg[:,:,2] += a_lot_of_images[i,:,:,2] for i in [0,1,2]: avg[:,:,i] = avg[:,:,i]/a_lot_of_images.shape[0] return avg

并且输出看起来仍然像颜色噪声。

【问题讨论】:

    标签: python-imaging-library rgb mean numpy-ndarray tensor


    【解决方案1】:

    def average_image(a_lot_of_images): avg = np.zeros((a_lot_of_images.shape[1], a_lot_of_images.shape[2], a_lot_of_images.shape[3]), dtype=float) for i in range(a_lot_of_images.shape[0]): avg[:, :, 0] += a_lot_of_images[i, :, :, 0] avg[:, :, 1] += a_lot_of_images[i, :, :, 1] avg[:, :, 2] += a_lot_of_images[i, :, :, 2] for i in [0,1,2]: avg[:, :, i] = avg[:, :, i]/a_lot_of_images.shape[0] avg = np.array(np.round(avg), dtype=np.uint8) return avg

    有效!有没有办法以更优雅的方式做到这一点?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-29
      • 1970-01-01
      • 1970-01-01
      • 2014-04-24
      • 2011-10-11
      • 1970-01-01
      • 2020-11-06
      • 2017-08-22
      相关资源
      最近更新 更多