Get the App
SLTechnology News&Howtos  ›  Development  › 

How does Python calculate the mean Variance of Picture data sets

Shulou Source: shulou.com Published: 2022-05-31 16:38:18 09月20日 Update

In this article, the editor introduces in detail "Python how to calculate the mean variance of the picture data set", the content is detailed, the steps are clear, and the details are handled properly. I hope that this "Python how to calculate the mean variance of the picture data set" article can help you solve your doubts.

Python batch reshape pictures #-*-coding: utf-8-*-"Created on Thu Aug 23 16:06:35 2018@author: libo"from PIL import Imageimport osdef image_resize (image_path) New_path): # uniform picture size print ('= > > modify picture size') for img_name in os.listdir (image_path): img_path = image_path + "/" + img_name # get the full name of the picture image = Image.open (img_path) # Open a specific picture image = image.resize 512)) # set the image size to be converted # 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" # enter the folder path of the picture new_path = the folder path image_resize (ori_path) after the picture's folder path image_resize = 'ZGROGRAPHER pycharmpictures projects' ssdUnip VOC2007 New_path) import osfrom PIL import Imageimport matplotlib.pyplot as pltimport numpy as npfrom scipy.misc import imreadfilepath = pycharm_projects\ ssd\ VOC2007\ reshape' # dataset directory pathDir = os.listdir (filepath) R_channel = 0B_channel = 0for 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 # here (512512) is the size of each picture All pictures have the same size R_mean = R_channel / numG_mean = G_channel / numB_mean = B_channel / numR_channel = 0G_channel = 0B_channel = 0for 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))

It may be a little slow. Just wait slowly.

The final result is an introduction.

Refer to the calculated dataset mean and variance import osfrom PIL import Image import matplotlib.pyplot as pltimport numpy as npfrom scipy.misc import imread filepath ='/ home/JPEGImages' # dataset directory pathDir = os.listdir (filepath) R_channel = 0G_channel = 0for 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 # here (384512) is the size of each picture All pictures have the same size R_mean = R_channel / numG_mean = G_channel / numB_mean = B_channel / numR_channel = 0G_channel = 0B_channel = 0for 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 / numG_var = G_channel / numB_var = B_channel / numprint ("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)) This article "how Python calculates the mean variance of picture data sets" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

Tags: Pictures data mean variance size size articles content files folders directories paths proper full name ideas new knowledge more steps knowledge knowledge points Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Apple Shulou Tech Info Huawei MariaDB