Image normalization

Image normalization refers to scaling the pixel values ​​of an image so that they are evenly distributed within the range of pixel values, so that the distribution of pixel values ​​of different images is similar, which is convenient for comparison and processing. Specifically, image normalization can help us:

1. Improve the robustness of the model: In deep learning, the model is very sensitive to changes in input data, and image normalization can reduce the differences between different images, thereby improving the robustness of the model.

2. Speed ​​up training: Normalized pictures can make the training process more stable, speed up the convergence speed, and improve training efficiency.

3. Improve the accuracy of the model: Normalized pictures can make it easier for the model to learn the characteristics of the picture, thereby improving the accuracy of the model.

from PIL import Image
import os

# 待处理图片文件夹路径
input_dir = '待处理图片'

# 处理后图片文件夹路径
output_dir = '处理后图片'

# 归一化参数
normalize_min = 0
normalize_max = 255

# 伽马校正参数
gamma = 1.5

# 遍历待处理图片文件夹下的所有图片
for filename in os.listdir(input_dir):
    if filename.endswith('.jpg') or filename.endswith('.png'):
        # 打开待处理图片
        img = Image.open(os.path.join(input_dir, filename))
        
        # 归一化
        img = Image.eval(img, lambda x: (x - normalize_min) * (255 / (normalize_max - normalize_min)))
        
        # 伽马校正
        img = Image.eval(img, lambda x: x ** gamma)
        
        # 保存处理后的图片
        output_filename = os.path.join(output_dir, filename)
        img.save(output_filename)

Guess you like

Origin blog.csdn.net/qq_50942093/article/details/131051717