opencv读取图像为b,g,r方法,比如
img = cv2.imread(“xx.jpg”)
cv2.imshow(“xx”,img)
import os
import glob
import numpy as np
import cv2
import tqdm
def img_squeeze(img_input):
img_out = img_input.reshape(1, -1)
img_out = np.squeeze(img_out, 0)
return img_out
filepath = r'./qiege/'
pathDirs = glob.glob(filepath + '/*.jpg')
pbar = tqdm.tqdm(total=len(pathDirs))
img_b = []
img_g = []
img_r = []
i=0
for pathDir in pathDirs:
i += 1
pathDir = pathDirs[int(np.random.rand(1)*(len(pathDirs)-1))]
if i > 100:
break
pbar.update(1)
img = cv2.imread(pathDir)
img_b = np.concatenate((img_b, img_squeeze(img[:, :, 0])))
img_g = np.concatenate((img_g, img_squeeze(img[:, :, 1])))
img_r = np.concatenate((img_r, img_squeeze(img[:, :, 2])))
print(np.mean(img_b), np.mean(img_g), np.mean(img_r))
print(np.std(img_b), np.std(img_g), np.std(img_r))
尽量别统计太多,可能会爆内存,可以随机统计一些。
最终得到的结果分别是b,g,r
若要归一化,还要除以255。