计算图像数据集RGB各通道的均值和方差(转载)
计算图像数据集RGB各通道的均值和方差
第一种写法,先读进来,再计算。比较耗内存。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
import cv2import numpy as npimport torchstartt = 700CNum = 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 --> RGBstdevs.reverse()print("normMean = {}".format(means))print("normStd = {}".format(stdevs)) |
第二种写法,读一张算一张,比较耗时:先过一遍计算出均值,再过一遍计算出方差。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
import osfrom PIL import Imageimport matplotlib.pyplot as pltimport numpy as npfrom scipy.misc import imreadstartt = 4000CNum = 1000 # 挑选多少图片进行计算num = 1000 * 3200 * 1800 # 这里(3200,1800)是每幅图片的大小,所有图片尺寸都一样imgs=[]R_channel = 0G_channel = 0B_channel = 0for 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 / numG_mean = G_channel / numB_mean = B_channel / numR_channel = 0G_channel = 0B_channel = 0for 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.0636285print("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),根据下式:
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
S = sqrt( sum(x^2)/N - (sum(x)/N )^2 )
可以知道,只需要经过一次遍历,就可以计算出数据集的均值和方差。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
import osfrom PIL import Imageimport matplotlib.pyplot as pltimport numpy as npfrom scipy.misc import imreadstartt = 5000CNum = 1000 # 挑选多少图片进行计算R_channel = 0G_channel = 0B_channel = 0R_channel_square = 0G_channel_square = 0B_channel_square = 0pixels_num = 0imgs = []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_numG_mean = G_channel / pixels_numB_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)) |

浙公网安备 33010602011771号