python自動計算圖像數據集的RGB均值

本文實例為大傢分享瞭python自動計算圖像數據集的RGB均值,供大傢參考,具體內容如下

圖像數據集往往要進行去均值,以保證更快的收斂。

代碼:

創建一個mean.py,寫入如下代碼。修改路徑即可使用

'''
qhy
2018.12.3
'''
import os
import numpy as np
import cv2
 
ims_path='C:/Users/my/Desktop/JPEGImages/'# 圖像數據集的路徑
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('數據集的BGR平均值為\n[{},{},{}]'.format( mean[0],mean[1],mean[2]) )
#cv.imread()讀取Img時候將rgb轉換為瞭bgr,謝謝taylover-pei的修正。

終端運行: python mean.py

結果示例如下:

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: