当前位置:   article > 正文

图片数据集的均值与标准差计算_python中如何计算图像r、g、b、三个维度的均值和标准差

python中如何计算图像r、g、b、三个维度的均值和标准差

模型训练需要对图片数据集对样本进行归一化,因此要求均值和标准差
但因为笔记本内存不够,用数组寄存没办法实现,所以写了一个低配版的python脚本

1. 获取图片名称列表

def get_image_list(img_dir, isclasses=False):
    """将图像的绝对路径生成列表
    args: img_dir:存放图片的目录
          isclasses:图片是否按类别存放标志
    return: 图片文件名称列表
    """
    img_list = []
    # 路径下图像是否按类别分类存放
    if isclasses:
        img_file = os.listdir(img_dir)
        for class_name in img_file:
            if not os.path.isfile(os.path.join(img_dir, class_name)):     
                class_img_list = os.listdir(os.path.join(img_dir, class_name))
                img_list.extend(class_img_list)            
    else:
        img_list = os.listdir(img_dir)
    # print(img_list)
    print('image numbers: {}'.format(len(img_list)))
    return img_list
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

2. 求所有样本的R、G、B均值

def get_image_pixel_mean(img_dir, img_list, img_size):
    """求数据集图像的R、G、B均值
    args: img_dir:
          img_list:
          img_size:
    """
    R_sum = 0
    G_sum = 0
    B_sum = 0
    count = 0
    # 循环读取所有图片
    for img_name in img_list:
        img_path = os.path.join(img_dir, img_name)
        if not os.path.isdir(img_path):
            image = cv2.imread(img_path)
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            image = cv2.resize(image, (img_size, img_size))      # <class 'numpy.ndarray'>
            R_sum += image[:, :, 0].mean()
            G_sum += image[:, :, 1].mean()
            B_sum += image[:, :, 2].mean()
            count += 1
    R_mean = R_sum / count
    G_mean = G_sum / count
    B_mean = B_sum / count
    print('R_mean:{}, G_mean:{}, B_mean:{}'.format(R_mean,G_mean,B_mean))
    RGB_mean = [R_mean, G_mean, B_mean]
    return RGB_mean
  • 1
  • 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

3. 求所有样本的R、G、B标准差

def get_image_pixel_std(img_dir, img_mean, img_list, img_size):
    R_squared_mean = 0
    G_squared_mean = 0
    B_squared_mean = 0
    count = 0
    image_mean = np.array(img_mean)
    # 循环读取所有图片
    for img_name in img_list:
        img_path = os.path.join(img_dir, img_name)
        if not os.path.isdir(img_path):
            image = cv2.imread(img_path)
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            image = cv2.resize(image, (img_size, img_size))      # <class 'numpy.ndarray'>
            image = image - image_mean    # 零均值
            # 求单张图片的方差,并累加
            R_squared_mean += np.mean(np.square(image[:, :, 0]).flatten())
            G_squared_mean += np.mean(np.square(image[:, :, 1]).flatten())
            B_squared_mean += np.mean(np.square(image[:, :, 2]).flatten())
            count += 1
    # 求R、G、B的方差
    R_std = math.sqrt(R_squared_mean / count)
    G_std = math.sqrt(G_squared_mean / count)
    B_std = math.sqrt(B_squared_mean / count)
    print('R_std:{}, G_std:{}, B_std:{}'.format(R_std, G_std, B_std))
    RGB_std = [R_std, G_std, B_std]
    return RGB_std
  • 1
  • 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

3. 完整代码

import os
import cv2
import numpy as np
import math


def get_image_list(img_dir, isclasses=False):
    """将图像的名称列表
    args: img_dir:存放图片的目录
          isclasses:图片是否按类别存放标志
    return: 图片文件名称列表
    """
    img_list = []
    # 路径下图像是否按类别分类存放
    if isclasses:
        img_file = os.listdir(img_dir)
        for class_name in img_file:
            if not os.path.isfile(os.path.join(img_dir, class_name)):     
                class_img_list = os.listdir(os.path.join(img_dir, class_name))
                img_list.extend(class_img_list)         
    else:
        img_list = os.listdir(img_dir)
    print(img_list)
    print('image numbers: {}'.format(len(img_list)))
    return img_list


def get_image_pixel_mean(img_dir, img_list, img_size):
    """求数据集图像的R、G、B均值
    args: img_dir:
          img_list:
          img_size:
    """
    R_sum = 0
    G_sum = 0
    B_sum = 0
    count = 0
    # 循环读取所有图片
    for img_name in img_list:
        img_path = os.path.join(img_dir, img_name)
        if not os.path.isdir(img_path):
            image = cv2.imread(img_path)
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            image = cv2.resize(image, (img_size, img_size))      # <class 'numpy.ndarray'>
            R_sum += image[:, :, 0].mean()
            G_sum += image[:, :, 1].mean()
            B_sum += image[:, :, 2].mean()
            count += 1
    R_mean = R_sum / count
    G_mean = G_sum / count
    B_mean = B_sum / count
    print('R_mean:{}, G_mean:{}, B_mean:{}'.format(R_mean,G_mean,B_mean))
    RGB_mean = [R_mean, G_mean, B_mean]
    return RGB_mean


def get_image_pixel_std(img_dir, img_mean, img_list, img_size):
    R_squared_mean = 0
    G_squared_mean = 0
    B_squared_mean = 0
    count = 0
    image_mean = np.array(img_mean)
    # 循环读取所有图片
    for img_name in img_list:
        img_path = os.path.join(img_dir, img_name)
        if not os.path.isdir(img_path):
            image = cv2.imread(img_path)    # 读取图片
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            image = cv2.resize(image, (img_size, img_size))      # <class 'numpy.ndarray'>
            image = image - image_mean    # 零均值
            # 求单张图片的方差
            R_squared_mean += np.mean(np.square(image[:, :, 0]).flatten())
            G_squared_mean += np.mean(np.square(image[:, :, 1]).flatten())
            B_squared_mean += np.mean(np.square(image[:, :, 2]).flatten())
            count += 1
    R_std = math.sqrt(R_squared_mean / count)
    G_std = math.sqrt(G_squared_mean / count)
    B_std = math.sqrt(B_squared_mean / count)
    print('R_std:{}, G_std:{}, B_std:{}'.format(R_std, G_std, B_std))
    RGB_std = [R_std, G_std, B_std]
    return RGB_std


if __name__ == '__main__':
    image_dir = '/图片文件路径'
    image_list = get_image_list(image_dir, isclasses=False)
    RGB_mean = get_image_pixel_mean(image_dir, image_list, img_size=224)
    get_image_pixel_std(image_dir, RGB_mean, image_list, img_size=224)
  • 1
  • 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
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/133082
推荐阅读
相关标签
  

闽ICP备14008679号