0

我有一个 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

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

4

1 回答 1

0

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

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

于 2020-05-10T03:25:38.260 回答