计算图像数据集的RGB均值方差


import os
import cv2
import numpy as np

path = r'D:\_NLP\_datasets\totaltext\totaltext\training'       #图片保存路径

def compute(path):
    file_names = os.listdir(path)
    per_image_Rmean = []
    per_image_Gmean = []
    per_image_Bmean = []
    for file_name in file_names:
        img = cv2.imread(os.path.join(path, file_name), 1)
        per_image_Bmean.append(np.mean(img[:, :, 0]))
        per_image_Gmean.append(np.mean(img[:, :, 1]))
        per_image_Rmean.append(np.mean(img[:, :, 2]))
    R_mean = np.mean(per_image_Rmean)/255
    G_mean = np.mean(per_image_Gmean)/255
    B_mean = np.mean(per_image_Bmean)/255
    stdR = np.std(per_image_Rmean)/255
    stdG = np.std(per_image_Gmean)/255
    stdB = np.std(per_image_Bmean)/255
    return R_mean, G_mean, B_mean, stdR, stdG, stdB

if __name__ == '__main__':
    R, G, B, stdR, stdG, stdB= compute(path)
    print("B= ", R*255, "G= ", G*255, "R=", B*255, "\nstdB = ", stdR*255, "stdG = ", stdG*255, "stdR =", stdB*255)

输出示例:

B=  116.11477632488261 G=  108.483968093102 R= 101.99688746936845 
stdB =  32.342267457925246 stdG =  30.985564116268353 stdR = 34.18972420891065

你可能感兴趣的:(MyTools,均值算法,深度学习,机器学习)