How to calculate the mean and std of my own dataset?

Now,I want to calculate the mean and std of my dataset(about 10000 images).
I know I can get the mean by:

However, I do not know how to get the std further effectively.
Can someone tell me how to get it? Better with the corresponding code!
Thanks in advance!


Would this answer from the other thread work?


Got same question here. Previously I was using ImageNet fixed Normalize technique. What is the difference between Imagenet and self dataset?
When I used ImageNet Normalize, the image becomes dark(pixels are below 0). I will try to get specific mean and std of my dataset to see if the image would not be black after Normalization.

Just as you did for mean, you can easily adapt your code to calculate standard deviation (after you calculated the means). In addition, if you count the number of pixels (width, height) in the loop, even if your images have different sizes you can get the exact number to divide the sum:

R_channel = 0
G_channel = 0
B_channel = 0

total_pixel = 0
for idx in xrange(len(pathDir)):
    filename = pathDir[idx]
    img = imread(os.path.join(filepath, filename))

    total_pixel = total_pixel + img.shape[0] * img.shape[1]

    R_total = R_total + np.sum((img[:, :, 0] - R_mean) ** 2)
    G_total = G_total + np.sum((img[:, :, 1] - G_mean) ** 2)
    B_total = B_total + np.sum((img[:, :, 2] - B_mean) ** 2)

R_std = sqrt(R_total / total_count)
G_std = sqrt(G_total / total_count)
B_std = sqrt(B_total / total_count)