Python怎么计算图片数据集的均值方差
导读:本文共1907.5字符,通常情况下阅读需要6分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: Python批量reshape图片#-*-coding:utf-8-*-"""CreatedonThuAug2316:06:352018@author:libo"""fromPILimportImageimportosdefimage_resize(image_path,new_path)... ...
音频解说
目录
(为您整理了一些要点),点击可以直达。Python批量reshape图片
#-*-coding:utf-8-*-"""CreatedonThuAug2316:06:352018@author:libo"""fromPILimportImageimportosdefimage_resize(image_path,new_path):#统一图片尺寸print('============>>修改图片尺寸')forimg_nameinos.listdir(image_path):img_path=image_path+"/"+img_name#获取该图片全称image=Image.open(img_path)#打开特定一张图片image=image.resize((512,512))#设置需要转换的图片大小#processthe1channelimageimage.save(new_path+'/'+img_name)print("endtheprocessing!")if__name__=='__main__':print("readyfor::::::::")ori_path=r"Z:\pycharm_projects\ssd\VOC2007\JPEGImages"#输入图片的文件夹路径new_path='Z:/pycharm_projects/ssd/VOC2007/reshape'#resize之后的文件夹路径image_resize(ori_path,new_path)
importosfromPILimportImageimportmatplotlib.pyplotaspltimportnumpyasnpfromscipy.miscimportimreadfilepath=r'Z:\pycharm_projects\ssd\VOC2007\reshape'#数据集目录pathDir=os.listdir(filepath)R_channel=0G_channel=0B_channel=0foridxinrange(len(pathDir)):filename=pathDir[idx]img=imread(os.path.join(filepath,filename))/255.0R_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/numG_mean=G_channel/numB_mean=B_channel/numR_channel=0G_channel=0B_channel=0foridxinrange(len(pathDir)):filename=pathDir[idx]img=imread(os.path.join(filepath,filename))/255.0R_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_meanis%f,G_meanis%f,B_meanis%f"%(R_mean,G_mean,B_mean))print("R_varis%f,G_varis%f,B_varis%f"%(R_var,G_var,B_var))
可能有点慢,慢慢等着就行。。。。。。。
最后得到的结果是介个
参考
计算数据集均值和方差
importosfromPILimportImageimportmatplotlib.pyplotaspltimportnumpyasnpfromscipy.miscimportimreadfilepath=‘/home/JPEGImages‘#数据集目录pathDir=os.listdir(filepath)R_channel=0G_channel=0B_channel=0foridxinxrange(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/numG_mean=G_channel/numB_mean=B_channel/num
R_channel=0G_channel=0B_channel=0
foridxinxrange(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/numG_var=G_channel/numB_var=B_channel/numprint("R_meanis%f,G_meanis%f,B_meanis%f"%(R_mean,G_mean,B_mean))print("R_varis%f,G_varis%f,B_varis%f"%(R_var,G_var,B_var))
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:
Python怎么计算图片数据集的均值方差的详细内容,希望对您有所帮助,信息来源于网络。