我有一个4D数据np.array,其中包含150个分解为3个通道的图像50X70。数据的形状为(150、50、70、3)。我需要按通道(形状为(50,70,3))的那150张图像的平均值(其中平均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
并且输出仍然看起来像彩色噪点。
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
有效!有没有办法以更优雅的方式做到这一点?