Python中求图片数据集均值方差的代码是什么
Admin 2022-08-24 群英技术资讯 551 次浏览
(做这个之前一定保证所有的图片都是统一尺寸,不然算出来不对,我的代码里设计的是512*512,可以自己调整,同一尺寸的代码我也有:
# -*- 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))
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
PyTorch是一个开源的Python机器学习库,基于Torch,用于自然语言处理等应用程序。它不仅能够实现强大的GPU加速,同时还支持动态神经网络。本文将介绍PyTorch搭建CNN如何实现风速预测,感兴趣的可以学习一下
很多小伙伴在提到python数据可视化的时候第一反应就是matplotlib库,但实际上python还有很多很好用的数据可视化的库,下面这篇文章主要给大家介绍了关于如何利用Python抓取数据并可视化的相关资料,需要的朋友可以参考下
这篇文章主要介绍了Python requests timeout的设置,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
python中导入的方式有哪些?我们在进行python项目时,经常会需要导入模块,导入包等等,那么我们应该怎样实现呢?要注意哪些问题?下面我们就一起来看看python中的导入方式。
这篇文章主要介绍了python中Flask Web 表单的使用方法介绍,表单的操作是Web程序开发中最核心的模块之一,绝大多数的动态交互功能都是通过表单的形式实现的。更多介绍需要的小伙伴可以参考下面文章内容
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008