数据集(图片)求均值和标准差
一、环境配置
运行一下命令完成环境配置
pip install opencv-python numpy
二、源代码
import os
import cv2
import numpy as np
def compute_mean_std(Img_folders):
all_file = []
for images_path in Img_folders:
all_file.append([os.path.join(images_path, filename) for filename in os.listdir(images_path)])
print('------------------')
# 统计所有图片的数量
num_images = len(all_file[0])
print(num_images)
# 逐张图计算均值和方差
mean_r = mean_g = mean_b = 0
std_r = std_g = std_b = 0
for i in range(len(all_file)):
for j in range(len(all_file[i])):
# print(all_file[i][j])
img = cv2.imread(all_file[i][j])
img = np.asarray(img)
# img = img.astype(np.float32) / 255.
img = img.astype(np.float32)
# cv2
im_mean, im_stds = cv2.meanStdDev(img)
mean_r += im_mean[0]
mean_g += im_mean[1]
mean_b += im_mean[2]
std_r += im_stds[0]
std_g += im_stds[1]
std_b += im_stds[2]
print(f'means:[{im_mean[0][0]},{im_mean[1][0]},{im_mean[2][0]}]')
print(f'std:[{im_stds[0][0]},{im_stds[1][0]},{im_stds[2][0]}]\n')
# 计算平均值
aa_r = mean_r/num_images
aa_g = mean_g/num_images
aa_b = mean_b/num_images
bb_r = std_r/num_images
bb_g = std_g/num_images
bb_b = std_b/num_images
print(' last mean: ', aa_r, aa_g, aa_b)
print(' last stds: ', bb_r, bb_g, bb_b)
if __name__ == '__main__':
Img_folders = [r'E:\trashdatasets\combination\train\images']
compute_mean_std(Img_folders)
- 若您的数据集分布在不同的文件夹,只需修改
Img_folders
即可,将每个文件夹的路径写到该列表中。