意见箱
恒创运营部门将仔细参阅您的意见和建议,必要时将通过预留邮箱与您保持联络。感谢您的支持!
意见/建议
提交建议

Python中求图片数据集均值方差的代码是什么

来源:恒创科技 编辑:恒创科技编辑部
2023-12-31 01:34:59
在实际应用中,我们有时候会遇到“Python中求图片数据集均值方差的代码是什么”这样的问题,我们该怎样来处理呢?下文给大家介绍了解决方法,希望这篇“Python中求图片数据集均值方差的代码是什么”文章能帮助大家解决问题。




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列表索引怎样写,A[ : 2 ]与A[ : , 2]含义相同吗