Files
MPKFNet/CalDatasetMeanVar.py
2026-07-17 18:55:31 +08:00

50 lines
1.8 KiB
Python
Executable File

import os
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
#from scipy.misc import imread,imresize
import cv2,argparse
parser1 = argparse.ArgumentParser(description="CUI")
parser1.add_argument("root", default='/data1/Hatcher/model/project/data_MPKFNet/rawdata/竹林嘴-3月3日候鸟视频.mp4', type=str,
help='root.')
parser1.add_argument("imgsize", default='640', type=str,
help='imgsize')
args = parser1.parse_args()
filepath = args.root
imageSize = int(args.imgsize)
pathDir = os.listdir(filepath)
R_channel = 0
G_channel = 0
B_channel = 0
for idx in range(len(pathDir)):
filename = pathDir[idx]
print(filename)
img = cv2.imread(os.path.join(filepath, filename))
img = cv2.resize(img,(imageSize,imageSize))/ 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 # 这里(512,512)是每幅图片的大小,所有图片尺寸都一样
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(pathDir)):
filename = pathDir[idx]
print(filename)
img = cv2.imread(os.path.join(filepath, filename))
img = cv2.resize(img, (imageSize, imageSize)) / 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))