網站首頁 編程語言 正文
前言
在做圖像處理的時候,有時候需要得到整個數據集的均值方差數值,以下代碼可以解決你的煩惱:
(做這個之前一定保證所有的圖片都是統一尺寸,不然算出來不對,我的代碼里設計的是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
相關推薦
- 2023-02-28 ts之 Mixin混入(ts對象的混入、類的混入)
- 2022-02-27 Spring Boot -- 創建工程時 Spring Initializr 報錯 Error:co
- 2022-06-24 python文件讀取和導包的絕對路徑、相對路徑詳解_python
- 2022-05-08 ASP.NET?MVC緩存過濾器用法_實用技巧
- 2022-02-26 微信小程序 - 所有頁面一次性(只需要執行一次)全部開啟分享朋友圈功能(wx.showShareMe
- 2022-01-31 有關“數據統計”的一些概念 -- PV UV VV IP跳出率等
- 2022-08-12 Qt實現拖動單個控件移動的示例代碼_C 語言
- 2022-07-03 C++超詳細講解隱藏私有屬性和方法的兩種實現方式_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支