python方差的计算公式_Python计算图片数据集的均值方差

得到整个数据集的均值方差数值

import os

from PIL import Image

import matplotlib.pyplot as plt

import numpy as np

# from scipy.misc import imread

## scipy_1.3.1 not allowed

from imageio import imread

filepath = '/home/deeplearning/NEW/tianchi_data/VOC2007-4/JPEGImages' # 数据集目录

pathDir = os.listdir(filepath)

R_channel = 0

G_channel = 0

B_channel = 0

for idx in range(len(pathDir)):

filename = 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) * 1024 * 1024 # 这里(1024,1024)是每幅图片的大小,所有图片尺寸都一样

R_mean = R_channel / num # or /255.0

G_mean = G_channel / num

B_mean = B_channel / num

R_channel = 0

G_channel = 0

B_channel = 0

for idx in range(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 = np.sqrt(R_channel / num)

G_var = np.sqrt(G_channel / num)

B_var = np.sqrt(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))

# R_mean is 143.532975, G_mean is 145.831770, B_mean is 151.186388

# R_var is 48.226279, G_var is 45.276815, B_var is 40.3711321

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

参考:https://blog.csdn.net/weixin_41765699/article/details/100118660

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值