【发布时间】:2018-10-18 17:27:24
【问题描述】:
我有一个形状为 (N, H, W, C) 的图像的 numpy 数组,其中 N 是图像的数量,H 是图像高度,W 是图像宽度,C 是 RGB 通道。
我想按通道标准化我的图像,因此对于每个图像,我想按通道减去图像通道的平均值并除以其标准差。
我在一个循环中执行此操作,这很有效,但是效率非常低,并且因为它会复制我的 RAM 太满了。
def standardize(img):
mean = np.mean(img)
std = np.std(img)
img = (img - mean) / std
return img
for img in rgb_images:
r_channel = standardize(img[:,:,0])
g_channel = standardize(img[:,:,1])
b_channel = standardize(img[:,:,2])
normalized_image = np.stack([r_channel, g_channel, b_channel], axis=-1)
standardized_images.append(normalized_image)
standardized_images = np.array(standardized_images)
如何更有效地利用 numpy 的功能?
【问题讨论】:
标签: python image numpy computer-vision normalization