Python计算图片数据集的均值方差示例详解
作者:萤-火 发布时间:2023-02-26 15:55:38
标签: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))
来源:https://blog.csdn.net/weixin_41765699/article/details/100118660


猜你喜欢
- 一、pip异常有一小部分童鞋在打开cmd输入pip后出现下面情况:Did not provide a commandDid not prov
- vue1.0中 vm.$dispatch 和 vm.$broadcast 被弃用,改用$emit,$onvm.$on( event, cal
- 本文实例为大家分享了python实现局域网内聊天功能的具体代码,供大家参考,具体内容如下功能: 可以向局域网内开启接收信息功能的ip进行发送
- 1.简介Psycopg是一种用于执行SQL语句的PythonAPI,可以为PostgreSQL、openGauss数据库提供统一访问接口,应
- 本文实例为大家分享了vue实现表单录入的具体代码,供大家参考,具体内容如下最终效果:代码:<template> <div
- 需求在某应用中,需要根据一定的规则生成随机的IP地址,规则类似于192.168.11.0/24这样的CIDR形式给出。实现经过艰苦卓绝的调试
- 基于ASP技术开发Internet/Intranet上的MIS系统是非常方便的,首先是它借用了ADO技术和概念,同时
- 本文以YOLOv5-6.1版本为例一、Add1.在common.py后加入如下代码# 结合BiFPN 设置可学习参数 学习不同分支的权重#
- 今天发现了一个显示ORACLE语法的好网站。内容太多,就不一一摘录了,记在这里,也方便自己查找。http://ss64.com/ora/ 目
- 程序设计中会经常碰到一种情况,就是事先无法得知用户会需要哪些数据,必须根据用户选择后再从服务器重新提取数据后反馈给用户。比如一简单的情况,用
- 1.SQL Server2019安装包下载1.1进入官网SQL Server 20191.2下载安装包1点击Continue2.填写个人信息
- 课程亮点系统分析目标网页html标签数据解析方法海量图片数据一键保存环境介绍python 3.8pycharm模块使用requests &g
- 要使数据库具备更强的抵御侵犯的能力,你要采取几步措施。有些措施只是良好的服务器管理的一部分,如拥有SQL Server最新的补丁,其他则包括
- 直接to_excel会被覆盖,借助ExcelWriter可以实现写多个sheet。from openpyxl import load_wor
- 方法一使用以下流式代码,无论下载文件的大小如何,Python 内存占用都不会增加:def download_file(url):  
- 形参可以设置参数默认值,设置遵循从右至左原则例如:fun(x=0,y=1),fun(x,y=1),但不可以是fun(x=1,y)形参设置可以
- JDBC(Java Database Connectivity),即Java数据库连接。通过JDBC编程,可以使Java应用程序和数据库进行
- 默认vue项目中已经使用vue-cli生成,安装axios,基于element-ui开发,axiosconfig目录和api目录是同级,主要
- 匿名函数什么是匿名函数用一句话表达只有返回值的函数就是匿名函数。匿名函数只用来实现一些简单的函数功能,所以追求代码的简洁和高效。使用关键字
- 我用 ip=Request.ServerVariables ("