import cv2
import numpy as np
R = 0.
G = 0.
B = 0.
R_2 = 0.
G_2 = 0.
B_2 = 0.
N = 0.
# #读取图片
img = cv2.imread("kids.jpg")
#BGR转换RGB
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = np.array(img)
h, w, c = img.shape
N += h*w
R_t = img[:, :, 0]
R += np.sum(R_t)#R通道的像素值之和
R_2 += np.sum(np.power(R_t, 2.0))#R通道的像素值的平方的和
G_t = img[:, :, 1]
G += np.sum(G_t)#R通道的像素值之和
G_2 += np.sum(np.power(G_t, 2.0))#R通道的像素值的平方的和
B_t = img[:, :, 2]
B += np.sum(B_t)#R通道的像素值之和
B_2 += np.sum(np.power(B_t, 2.0))#R通道的像素值的平方的和
R_mean = R/N#R通道的均值
G_mean = G/N#R通道的均值
B_mean = B/N#R通道的均值
R_std = np.sqrt(R_2/N - R_mean*R_mean)#R通道的方差
G_std = np.sqrt(G_2/N - G_mean*G_mean)#R通道的方差
B_std = np.sqrt(B_2/N - B_mean*B_mean)
print("R_mean均值:%f,G_mean均值:%f,B_mean均值:%f" % (R_mean,G_mean,B_mean) )
print("R_std方差:%f,G_std方差:%f,B_std方差:%f," % (R_std,B_std,G_std) )