如何手动计算图像数据集的均值和方差

1.代码

import os
from PIL import Image  
import matplotlib.pyplot as plt
import numpy as np
from scipy.misc import imread 
 
filepath = '/home/JPEGImages' # 数据集目录文件夹
# pathdir是得到该文件夹下的所有文件名
pathDir = os.listdir(filepath)
 
"""
先计算均值,R、G、B三通道先置为0
"""
R_channel = 0
G_channel = 0
B_channel = 0

for idx in range(len(pathDir)):
    filename = pathDir[idx]
    # 依次读取图片,pathDir[idx]
    img = imread(os.path.join(filepath, filename))
    R_channel = R_channel + np.sum(img[:,:,0])
    G_channel = G_channel + np.sum(img[:,:,1])
    B_channel = B_channel + np.sum(img[:,:,2])
    
num = len(pathDir) * 384 * 512 # 这里(384,512)是每幅图片的大小,所有图片尺寸都一样
# 三通道,其实就是叠加起来的
# 把每个通道所有的像素点加起来求和,再除以总数(图片个数*像素点数(H*W))
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 idx in xrange(len(pathDir)):
    filename = pathDir[idx]
    img = imread(os.path.join(filepath, filename))
    R_channel = R_channel + np.sum((img[:,:,0] - R_mean)**2)
    G_channel = G_channel + np.sum((img[:,:,1] - G_mean)**2)
    B_channel = B_channel + np.sum((img[:,:,2] - B_mean)**2)
    
R_var = R_channel / num
G_var = G_channel / num
B_var = B_channel / num
print("R_mean is %f, G_mean is %f, B_mean is %f" % (R_mean, G_mean, B_mean))
print("R_var is %f, G_var is %f, B_var is %f" % (R_var, G_var, B_var))

2.解释

可能对于图片 均值和方差不能用np.mean(img,axis=0/1/2)这样去计算,这种情况算出来的是个降维的矩阵,并不是需要的单一的数字

上面的代码没有运行,因为是借鉴了的
但是看懂了的

3.参考来源

1.如何计算数据集均值和方差
2.os.listdir的作用

你可能感兴趣的:(pytorch系列)