網(wǎng)站首頁 編程語言 正文
前言
在做圖像處理的時(shí)候,有時(shí)候需要得到整個(gè)數(shù)據(jù)集的均值方差數(shù)值,以下代碼可以解決你的煩惱:
(做這個(gè)之前一定保證所有的圖片都是統(tǒng)一尺寸,不然算出來不對(duì),我的代碼里設(shè)計(jì)的是512*512,可以自己調(diào)整,同一尺寸的代碼我也有:
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): # 統(tǒng)一圖片尺寸
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)) # 設(shè)置需要轉(zhuǎn)換的圖片大小
# 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' # 數(shù)據(jù)集目錄
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))
可能有點(diǎn)慢,慢慢等著就行。。。。。。。
最后得到的結(jié)果是介個(gè)
參考
計(jì)算數(shù)據(jù)集均值和方差
import os
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
from scipy.misc import imread
filepath = ‘/home/JPEGImages‘ # 數(shù)據(jù)集目錄
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
相關(guān)推薦
- 2022-06-07 C語言超詳細(xì)講解排序算法下篇_C 語言
- 2022-06-28 python生成圖片驗(yàn)證碼的方法_python
- 2023-07-14 react 利用插件react-draggable實(shí)現(xiàn)拖拽功能
- 2022-04-09 SpringBoot上傳文件并配置本地資源映射來訪問文件
- 2022-10-12 Nginx?504?Gateway?Time-out的兩種最新解決方案_nginx
- 2022-07-01 Oracle數(shù)據(jù)庫用戶密碼過期的解決方法_oracle
- 2022-08-07 Qt使用QPainter實(shí)現(xiàn)自定義圓形進(jìn)度條_C 語言
- 2022-06-18 C語言簡(jiǎn)明講解單引號(hào)與雙引號(hào)的使用_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支