华域联盟 Python python自动计算图像数据集的RGB均值

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

结果示例如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持华域联盟。

您可能感兴趣的文章:

本文由 华域联盟 原创撰写:华域联盟 » python自动计算图像数据集的RGB均值

转载请保留出处和原文链接:https://www.cnhackhy.com/28937.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部