千家信息网

Python怎么计算图片数据集的均值方差

发表于:2025-02-01 作者:千家信息网编辑
千家信息网最后更新 2025年02月01日,本文小编为大家详细介绍"Python怎么计算图片数据集的均值方差",内容详细,步骤清晰,细节处理妥当,希望这篇"Python怎么计算图片数据集的均值方差"文章能帮助大家解决疑惑,下面跟着小编的思路慢慢
千家信息网最后更新 2025年02月01日Python怎么计算图片数据集的均值方差

本文小编为大家详细介绍"Python怎么计算图片数据集的均值方差",内容详细,步骤清晰,细节处理妥当,希望这篇"Python怎么计算图片数据集的均值方差"文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

Python批量reshape图片

# -*- coding: utf-8 -*-"""Created on Thu Aug 23 16:06:35 2018@author: libo"""from PIL import Imageimport osdef 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 osfrom PIL import Imageimport matplotlib.pyplot as pltimport numpy as npfrom scipy.misc import imreadfilepath = r'Z:\pycharm_projects\ssd\VOC2007\reshape'  # 数据集目录pathDir = os.listdir(filepath)R_channel = 0G_channel = 0B_channel = 0for 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 / numG_mean = G_channel / numB_mean = B_channel / numR_channel = 0G_channel = 0B_channel = 0for 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 osfrom PIL import Image  import matplotlib.pyplot as pltimport numpy as npfrom scipy.misc import imread filepath = '/home/JPEGImages' # 数据集目录pathDir = os.listdir(filepath)R_channel = 0G_channel = 0B_channel = 0for 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 / numG_mean = G_channel / numB_mean = B_channel / num
R_channel = 0G_channel = 0B_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 / numG_var = G_channel / numB_var = B_channel / numprint("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怎么计算图片数据集的均值方差"文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注行业资讯频道。

0