第一種寫法,先讀進來,再計算。比較耗內存。python
import cv2 import numpy as np import torch startt = 700 CNum = 100 # 挑選多少圖片進行計算 imgs=[] for i in range(startt, startt+CNum): img_path = os.path.join(root_path, filename[i]) img = cv2.imread(img_path) img = img[:, :, :, np.newaxis] imgs.append(torch.Tensor(img)) torch_imgs = torch.cat(imgs, dim=3) means, stdevs = [], [] for i in range(3): pixels = torch_imgs[:, :, i, :] # 拉成一行 means.append(torch.mean(pixels)) stdevs.append(torch.std(pixels)) # cv2 讀取的圖像格式爲BGR,PIL/Skimage讀取到的都是RGB不用轉 means.reverse() # BGR --> RGB stdevs.reverse() print("normMean = {}".format(means)) print("normStd = {}".format(stdevs))
第二種寫法,讀一張算一張,比較耗時:先過一遍計算出均值,再過一遍計算出方差。app
import os from PIL import Image import matplotlib.pyplot as plt import numpy as np from scipy.misc import imread startt = 4000 CNum = 1000 # 挑選多少圖片進行計算 num = 1000 * 3200 * 1800 # 這裏(3200,1800)是每幅圖片的大小,全部圖片尺寸都同樣 imgs=[] R_channel = 0 G_channel = 0 B_channel = 0 for i in range(startt, startt+CNum): img = imread(os.path.join(root_path, filename[i])) R_channel = R_channel + np.sum(img[:, :, 0]) G_channel = G_channel + np.sum(img[:, :, 1]) B_channel = B_channel + np.sum(img[:, :, 2]) R_mean = R_channel / num G_mean = G_channel / num B_mean = B_channel / num R_channel = 0 G_channel = 0 B_channel = 0 for i in range(startt, startt+CNum): img = imread(os.path.join(root_path, filename[i])) R_channel = R_channel + np.sum(np.power(img[:, :, 0]-R_mean, 2) ) G_channel = G_channel + np.sum(np.power(img[:, :, 1]-G_mean, 2) ) B_channel = B_channel + np.sum(np.power(img[:, :, 2]-B_mean, 2) ) R_std = np.sqrt(R_channel/num) G_std = np.sqrt(G_channel/num) B_std = np.sqrt(B_channel/num) # R:65.045966 G:70.3931815 B:78.0636285 print("R_mean is %f, G_mean is %f, B_mean is %f" % (R_mean, G_mean, B_mean)) print("R_std is %f, G_std is %f, B_std is %f" % (R_std, G_std, B_std))
第三種寫法,只須要遍歷一次:在一輪循環中計算出x,x^2; 而後x'=sum(x)/N ,又有sum(x^2),根據下式:orm
S^2
= sum((x-x')^2 )/N = sum(x^2+x'^2-2xx')/N
= {sum(x^2) + sum(x'^2) - 2x'*sum(x) }/N
= {sum(x^2) + N*(x'^2) - 2x'*(N*x') }/N
= {sum(x^2) - N*(x'^2) }/N
= sum(x^2)/N - x'^2blog
S = sqrt( sum(x^2)/N - (sum(x)/N )^2 )圖片
能夠知道,只須要通過一次遍歷,就能夠計算出數據集的均值和方差。ip
import os from PIL import Image import matplotlib.pyplot as plt import numpy as np from scipy.misc import imread startt = 5000 CNum = 1000 # 挑選多少圖片進行計算 R_channel = 0 G_channel = 0 B_channel = 0 R_channel_square = 0 G_channel_square = 0 B_channel_square = 0 pixels_num = 0 imgs = [] for i in range(startt, startt+CNum): img = imread(os.path.join(root_path, filename[i])) h, w, _ = img.shape pixels_num += h*w # 統計單個通道的像素數量 R_temp = img[:, :, 0] R_channel += np.sum(R_temp) R_channel_square += np.sum(np.power(R_temp, 2.0)) G_temp = img[:, :, 1] G_channel += np.sum(G_temp) G_channel_square += np.sum(np.power(G_temp, 2.0)) B_temp = img[:, :, 2] B_channel = B_channel + np.sum(B_temp) B_channel_square += np.sum(np.power(B_temp, 2.0)) R_mean = R_channel / pixels_num G_mean = G_channel / pixels_num B_mean = B_channel / pixels_num """ S^2 = sum((x-x')^2 )/N = sum(x^2+x'^2-2xx')/N = {sum(x^2) + sum(x'^2) - 2x'*sum(x) }/N = {sum(x^2) + N*(x'^2) - 2x'*(N*x') }/N = {sum(x^2) - N*(x'^2) }/N = sum(x^2)/N - x'^2 """ R_std = np.sqrt(R_channel_square/pixels_num - R_mean*R_mean) G_std = np.sqrt(G_channel_square/pixels_num - G_mean*G_mean) B_std = np.sqrt(B_channel_square/pixels_num - B_mean*B_mean) print("R_mean is %f, G_mean is %f, B_mean is %f" % (R_mean, G_mean, B_mean)) print("R_std is %f, G_std is %f, B_std is %f" % (R_std, G_std, B_std))