您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)python如何實(shí)現(xiàn)自動(dòng)計(jì)算圖像數(shù)據(jù)集的RGB均值,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
圖像數(shù)據(jù)集往往要進(jìn)行去均值,以保證更快的收斂。
代碼:
創(chuàng)建一個(gè)mean.py,寫(xiě)入如下代碼。修改路徑即可使用
''' qhy 2018.12.3 ''' import os import numpy as np import cv2 ims_path='C:/Users/my/Desktop/JPEGImages/'# 圖像數(shù)據(jù)集的路徑 ims_list=os.listdir(ims_path) R_means=[] G_means=[] B_means=[] for im_list in ims_list: im=cv2.imread(ims_path+im_list) #extrect value of diffient channel im_R=im[:,:,0] im_G=im[:,:,1] im_B=im[:,:,2] #count mean for every channel im_R_mean=np.mean(im_R) im_G_mean=np.mean(im_G) im_B_mean=np.mean(im_B) #save single mean value to a set of means R_means.append(im_R_mean) G_means.append(im_G_mean) B_means.append(im_B_mean) print('圖片:{} 的 RGB平均值為 \n[{},{},{}]'.format(im_list,im_R_mean,im_G_mean,im_B_mean) ) #three sets into a large set a=[R_means,G_means,B_means] mean=[0,0,0] #count the sum of different channel means mean[0]=np.mean(a[0]) mean[1]=np.mean(a[1]) mean[2]=np.mean(a[2]) print('數(shù)據(jù)集的BGR平均值為\n[{},{},{}]'.format( mean[0],mean[1],mean[2]) ) #cv.imread()讀取Img時(shí)候?qū)gb轉(zhuǎn)換為了bgr,謝謝taylover-pei的修正。
終端運(yùn)行: python mean.py
結(jié)果示例如下:
關(guān)于“python如何實(shí)現(xiàn)自動(dòng)計(jì)算圖像數(shù)據(jù)集的RGB均值”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。