『python』统计数据集均值与方差

# coding=utf-8
""" 
计算图片数据集所有图片的均值和方差
@author: libo
"""
import numpy as np
import cv2
import os

img_h, img_w = 224, 224   #根据自己数据集适当调整,影响不大
means, stdevs = [], []
img_list = []
 
imgs_path = 'D:/database/VOCdevkit/VOC2012/JPEGImages/'
imgs_path_list = os.listdir(imgs_path)
 
len_ = len(imgs_path_list)
i = 0
for item in imgs_path_list:
    img = cv2.imread(os.path.join(imgs_path,item))
    img = cv2.resize(img,(img_w,img_h))
    img = img[:, :, :, np.newaxis]
    img_list.append(img)
    i += 1
    print(i,'/',len_)    
 
imgs = np.concatenate(img_list, axis=3)
imgs = imgs.astype(np.float32) / 255.
 
for i in range(3):
    pixels = imgs[:, :, i, :].ravel()  # 拉成一行
    means.append(np.mean(pixels))
    stdevs.append(np.std(pixels))
 
# BGR --> RGB , CV读取的需要转换,PIL读取的不用转换
means.reverse()
stdevs.reverse()

print("normMean = {}".format(means))
print("normStd = {}".format(stdevs))

你可能感兴趣的:(python,学习,python,深度学习,opencv)