Python計(jì)算圖片數(shù)據(jù)集的均值方差示例詳解
前言
在做圖像處理的時(shí)候,有時(shí)候需要得到整個(gè)數(shù)據(jù)集的均值方差數(shù)值,以下代碼可以解決你的煩惱:
(做這個(gè)之前一定保證所有的圖片都是統(tǒng)一尺寸,不然算出來(lái)不對(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 # 獲取該圖片全稱(chēng) image = Image.open(img_path) # 打開(kāi)特定一張圖片 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))
以上就是Python計(jì)算圖片數(shù)據(jù)集的均值方差示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Python計(jì)算圖片數(shù)據(jù)集均值方差的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
在Python中處理日期和時(shí)間的基本知識(shí)點(diǎn)整理匯總
這篇文章主要介紹了在Python中處理日期和時(shí)間的基本知識(shí)點(diǎn)整理匯總,是Python入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-05-05完美解決keras 讀取多個(gè)hdf5文件進(jìn)行訓(xùn)練的問(wèn)題
這篇文章主要介紹了完美解決keras 讀取多個(gè)hdf5文件進(jìn)行訓(xùn)練的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07關(guān)于Tensorflow和Keras版本對(duì)照及環(huán)境安裝
這篇文章主要介紹了關(guān)于Tensorflow和Keras版本對(duì)照及環(huán)境安裝方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08Python實(shí)現(xiàn)自動(dòng)登錄百度空間的方法
這篇文章主要介紹了Python實(shí)現(xiàn)自動(dòng)登錄百度空間的方法,涉及Python的http請(qǐng)求發(fā)送、獲取響應(yīng)、cookie操作等相關(guān)技巧,需要的朋友可以參考下2017-06-06使用python加密主機(jī)文件幾種方法實(shí)現(xiàn)
本文主要介紹了使用python加密主機(jī)文件幾種方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02卷積神經(jīng)網(wǎng)絡(luò)(CNN)基于SqueezeNet的眼疾識(shí)別功能
SqueezeNet是一種輕量且高效的CNN模型,它參數(shù)比AlexNet少50倍,但模型性能(accuracy)與AlexNet接近,這篇文章主要介紹了卷積神經(jīng)網(wǎng)絡(luò)(CNN)基于SqueezeNet的眼疾識(shí)別,需要的朋友可以參考下2023-08-08