使用阈值平均算法对图像进行平滑处理和编程实现将彩色图像变换为单色图像(分别为R、G、B三种)
def get_image_pixel_std(img_dir, img_mean, img_list, img_size):
R_squared_mean = 0
G_squared_mean = 0
B_squared_mean = 0
count = 0
image_mean = np.array(img_mean)
# 循环读取所有图片
for img_name in img_list:
img_path = os.path.join(img_dir, img_name)
if not os.path.isdir(img_path):
image = cv2.imread(img_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, (img_size, img_size)) # <class 'numpy.ndarray'>
image = image - image_mean # 零均值
# 求单张图片的方差,并累加
R_squared_mean += np.mean(np.square(image[:, :, 0]).flatten())
G_squared_mean += np.mean(np.square(image[:, :, 1]).flatten())
B_squared_mean += np.mean(np.square(image[:, :, 2]).flatten())
count += 1
# 求R、G、B的方差
R_std = math.sqrt(R_squared_mean / count)
G_std = math.sqrt(G_squared_mean / count)
B_std = math.sqrt(B_squared_mean / count)
print('R_std:{}, G_std:{}, B_std:{}'.format(R_std, G_std, B_std))
RGB_std = [R_std, G_std, B_std]
return RGB_std