```python
import os
import numpy as np
import imageio
if __name__ == "__main__":
R_channel = 0.
G_channel = 0.
B_channel = 0.
R_channel_square = 0.
G_channel_square = 0.
B_channel_square = 0.
filename = []
root_path = "dataset\\train"
for image in sorted(os.listdir(os.path.join(root_path, "imgs"))):
filename.append(os.path.join(root_path, "imgs", image))
for label in sorted(os.listdir(os.path.join(root_path, "labels"))):
filename.append(os.path.join(root_path, "labels", label))
imgs=[]
length = len(filename)
pixels_num = 768*1024*length
for i in range(length):
if i<10000:
img = imageio.imread(filename[i])
else:
img = np.array(imageio.imread(filename[i])).astype("uint8")[:, :, np.newaxis].repeat(3,2)
img = img / 255
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))
print(i,"/",length)
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))
```

图片均值和方差的计算