import numpy as np
import os
import cv2
import re
from itertools import chain
from glob import glob


def gen_mean_std(root_path="/root/data/segmentation/VOCtrainval_11-May-2012/VOCdevkit/VOC2012/JPEGImages", txt_path=""):
    """
    功能：获得 1、root_path路径下所有图片
             2、txt_path文件中所有图片路径（TODO）
             的mean & std
    参数：
        root_path:所有图片的文件夹
        txt_path:包含所有图片路径的文件（TODO）
    """
    all_images = os.listdir(root_path)

    R_channel = 0
    G_channel = 0
    B_channel = 0
    num = 0
    for idx in range(len(all_images)):
        filename = all_images[idx]
        img = cv2.imread(os.path.join(root_path, 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 += img.shape[0]*img.shape[1]

    R_mean = R_channel / num
    G_mean = G_channel / num
    B_mean = B_channel / num

    R_channel = 0
    G_channel = 0
    B_channel = 0
    for idx in range(len(all_images)):
        filename = all_images[idx]
        img = cv2.imread(os.path.join(root_path, 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))


if __name__ == "__main__":
    gen_mean_std()
import numpy as np
import os
import cv2
import re
from itertools import chain
from glob import glob


def gen_mean_std(root_path="/root/data/segmentation/VOCtrainval_11-May-2012/VOCdevkit/VOC2012/JPEGImages", txt_path=""):
    """
    功能：获得 1、root_path路径下所有图片
             2、txt_path文件中所有图片路径（TODO）
             的mean & std
    参数：
        root_path:所有图片的文件夹
        txt_path:包含所有图片路径的文件（TODO）
    """
    all_images = os.listdir(root_path)

    R_channel = 0
    G_channel = 0
    B_channel = 0
    num = 0
    for idx in range(len(all_images)):
        filename = all_images[idx]
        img = cv2.imread(os.path.join(root_path, 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 += img.shape[0]*img.shape[1]

    R_mean = R_channel / num
    G_mean = G_channel / num
    B_mean = B_channel / num

    R_channel = 0
    G_channel = 0
    B_channel = 0
    for idx in range(len(all_images)):
        filename = all_images[idx]
        img = cv2.imread(os.path.join(root_path, 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))


if __name__ == "__main__":
    gen_mean_std()
功能：获得 1、root_path路径下所有图片              2、txt_path文件中所有图片路径（TODO）              的mean & std     参数：         root_path:所有图片的文件夹         txt_path:包含所有图片路径的文件（TODO）