Python计算图片数据集的均值方差示例详解

 更新时间:2022年05月19日 14:31:06   作者:萤-火  
这篇文章主要为大家介绍了Python计算图片数据集的均值方差,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

前言

在做图像处理的时候,有时候需要得到整个数据集的均值方差数值,以下代码可以解决你的烦恼:

(做这个之前一定保证所有的图片都是统一尺寸,不然算出来不对,我的代码里设计的是512*512,可以自己调整,同一尺寸的代码我也有:

Python批量reshape图片

# -*- coding: utf-8 -*-
"""
Created on Thu Aug 23 16:06:35 2018
@author: libo
"""
from PIL import Image
import os
def image_resize(image_path, new_path):           # 统一图片尺寸
    print('============>>修改图片尺寸')
    for img_name in os.listdir(image_path):
        img_path = image_path + "/" + img_name    # 获取该图片全称
        image = Image.open(img_path)              # 打开特定一张图片
        image = image.resize((512, 512))          # 设置需要转换的图片大小
        # process the 1 channel image
        image.save(new_path + '/'+ img_name)
    print("end the processing!")
if __name__ == '__main__':
    print("ready for ::::::::  ")
    ori_path = r"Z:\pycharm_projects\ssd\VOC2007\JPEGImages"                # 输入图片的文件夹路径
    new_path = 'Z:/pycharm_projects/ssd/VOC2007/reshape'                   # resize之后的文件夹路径
    image_resize(ori_path, new_path)
import os
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
from scipy.misc import imread
filepath = r'Z:\pycharm_projects\ssd\VOC2007\reshape'  # 数据集目录
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)) / 255.0
    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) * 512 * 512  # 这里(512,512)是每幅图片的大小,所有图片尺寸都一样
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 range(len(pathDir)):
    filename = pathDir[idx]
    img = imread(os.path.join(filepath, filename)) / 255.0
    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))

可能有点慢,慢慢等着就行。。。。。。。

最后得到的结果是介个

参考

计算数据集均值和方差

import os
from PIL import Image  
import matplotlib.pyplot as plt
import numpy as np
from scipy.misc import imread 
filepath = ‘/home/JPEGImages‘ # 数据集目录
pathDir = os.listdir(filepath)
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])
    G_channel = G_channel + np.sum(img[:,:,1])
    B_channel = B_channel + np.sum(img[:,:,2])
num = len(pathDir) * 384 * 512 # 这里(384,512)是每幅图片的大小,所有图片尺寸都一样
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))

以上就是Python计算图片数据集的均值方差示例详解的详细内容,更多关于Python计算图片数据集均值方差的资料请关注脚本之家其它相关文章!

相关文章

  • 玩转python爬虫之正则表达式

    玩转python爬虫之正则表达式

    这篇文章主要介绍了python爬虫的正则表达式,正则表达式在Python爬虫是必不可少的神兵利器,本文整理了Python中的正则表达式的相关内容,感兴趣的小伙伴们可以参考一下
    2016-02-02
  • Python Pandas学习之series的二元运算详解

    Python Pandas学习之series的二元运算详解

    二元运算是指由两个元素形成第三个元素的一种规则,例如数的加法及乘法;更一般地,由两个集合形成第三个集合的产生方法或构成规则称为二次运算。本文将详细讲讲Pandas中series的二元运算,感兴趣的可以了解一下
    2022-09-09
  • python django 实现验证码的功能实例代码

    python django 实现验证码的功能实例代码

    本篇文章主要介绍了python django 实现验证码的功能实例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • 如何解决jupyter notebook中文乱码问题

    如何解决jupyter notebook中文乱码问题

    这篇文章主要介绍了如何解决jupyter notebook中文乱码问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-06-06
  • Python类的基本写法与注释风格介绍

    Python类的基本写法与注释风格介绍

    这篇文章主要介绍了Python类的基本写法与注释风格,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • python tqdm用法及实例详解

    python tqdm用法及实例详解

    在本篇文章里小编给大家整理的是一篇关于python tqdm用法及实例详解内容,有需要的朋友们可以学习下。
    2021-06-06
  • Python实现的排列组合计算操作示例

    Python实现的排列组合计算操作示例

    这篇文章主要介绍了Python实现的排列组合计算操作,涉及Python数学运算的相关函数与使用技巧,需要的朋友可以参考下
    2017-10-10
  • pip install 安装路径修改的方法步骤

    pip install 安装路径修改的方法步骤

    本文主要介绍了pip install 安装路径修改的方法步骤,文中通过图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-01-01
  • python从入门到精通 windows安装python图文教程

    python从入门到精通 windows安装python图文教程

    这篇文章主要为大家详细介绍了python从入门到精通,windows安装python图文教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-05-05
  • Python实现读取Linux系统的CPU以及内存占用

    Python实现读取Linux系统的CPU以及内存占用

    这篇文章主要为大家详细介绍了如何利用Python语言实现Linux系统的CPU以及内存占用,文中的示例代码讲解详细,具有一定的学习价值,需要的可以收藏一下
    2023-05-05

最新评论