我想标准化我的图像数据集,并使用与原始数据集相同的文件和文件夹名称保存这些标准化图像。 当我尝试绘制通道的直方图以检查图像是否正确标准化时,此代码是否有问题?我在红色像素直方图的 0 和 1 处得到两个峰值(作为示例)。难道我做错了什么。用于规范化所有数据集的代码是否正确?我怎样才能确保所有图像都正确标准化以及如何绘制可视化来检查这种标准化。
除了标准化之外,我还可以如何对数据集进行缩放,这是处理图像时的必要步骤吗?
import os
import cv2
def normalization(img_path):
image = cv2.imread(img, cv2.IMREAD_COLOR)
n_img = cv2.normalize(image, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
return n_img
input_root_dir = 'Path to orginal dataset'
output_root_dir = 'Path to the output Folder for saving images'
for dir, _, file in os.walk(input_root_dir):
for i in file:
if file.endswith(('.jpg', '.png', '.jpeg')):
input_image_path = os.path.join(dir, file)
relative_dir = os.path.relpath(dir, input_root_dir)
output_dir = os.path.join(output_root_dir, relative_dir)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
output_image_path = os.path.join(output_dir, file)
nor_img = normalization(input_image_path)
cv2.imwrite(output_image_path, (n_img * 255).astype('uint8'))
print(f"Processing: {input_image_path}")
print(f"Saving to: {output_image_path}")
您应该使用
nor_img
来写入输出图像。
cv2.imwrite(output_image_path, (nor_img * 255).astype('uint8'))