【深度学习】数据增强基本介绍和常用的数据增强方法
import os
import random
from PIL import Image, ImageEnhance, ImageOps, ImageFilter
# 翻转图像
def flip_image(image, mode='horizontal'):
if mode == 'horizontal':
return image.transpose(Image.FLIP_LEFT_RIGHT)
elif mode == 'vertical':
return image.transpose(Image.FLIP_TOP_BOTTOM)
else:
raise ValueError("Mode should be 'horizontal' or 'vertical'")
# 旋转图像
def rotate_image(image, angle):
return image.rotate(angle)
# 缩放图像
def scale_image(image, scale_factor):
width, height = image.size
return image.resize((int(width * scale_factor), int(height * scale_factor)))
# 平移图像
def translate_image(image, x, y):
return ImageOps.offset(image, x, y)
# 裁剪图像
def crop_image(image, crop_box):
return image.crop(crop_box)
# 调整亮度、对比度、饱和度、色调
def adjust_color(image, brightness=1, contrast=1, saturation=1, hue=1):
enhancer = ImageEnhance.Brightness(image)
image = enhancer.enhance(brightness)
enhancer = ImageEnhance.Contrast(image)
image = enhancer.enhance(contrast)
enhancer = ImageEnhance.Color(image)
image = enhancer.enhance(saturation)
# hue adjustment not directly available in PIL, skipped
return image
# 添加噪声
def add_noise(image, noise_type='gaussian', mean=0, std=1):
# This function is a placeholder; PIL doesn't support direct noise addition
return image
# 模糊图像
def blur_image(image, blur_type='gaussian', radius=2):
if blur_type == 'gaussian':
return image.filter(ImageFilter.GaussianBlur(radius))
elif blur_type == 'motion':
return image.filter(ImageFilter.MotionBlur(radius)) # Pillow doesn't have MotionBlur, custom implementation needed
else:
raise ValueError("Blur type should be 'gaussian' or 'motion'")
# 仿射变换
def affine_transform(image, matrix):
return image.transform(image.size, Image.AFFINE, matrix)
def main():
input_image_path = '' # 输入图像路径
output_folder = 'output_path' # 输出文件夹
if not os.path.exists(output_folder):
os.makedirs(output_folder)
image = Image.open(input_image_path)
# 设置增强方法及其参数
methods = [
('flip', {'mode': 'horizontal'}),
('rotate', {'angle': 45}),
('scale', {'scale_factor': 1.5}),
('translate', {'x': 10, 'y': 20}),
('crop', {'crop_box': (10, 10, 200, 200)}),
('adjust_color', {'brightness': 1.2, 'contrast': 1.5, 'saturation': 1.3}),
('add_noise', {'noise_type': 'gaussian', 'mean': 0, 'std': 1}),
('blur', {'blur_type': 'gaussian', 'radius': 2}),
('affine', {'matrix': (1, 0.2, 0, 0.2, 1, 0)})
]
# 应用选择的增强方法
for method_name, params in methods:
if method_name == 'flip':
result_image = flip_image(image, **params)
elif method_name == 'rotate':
result_image = rotate_image(image, **params)
elif method_name == 'scale':
result_image = scale_image(image, **params)
elif method_name == 'translate':
result_image = translate_image(image, **params)
elif method_name == 'crop':
result_image = crop_image(image, **params)
elif method_name == 'adjust_color':
result_image = adjust_color(image, **params)
elif method_name == 'add_noise':
result_image = add_noise(image, **params)
elif method_name == 'blur':
result_image = blur_image(image, **params)
elif method_name == 'affine':
result_image = affine_transform(image, **params)
else:
continue
output_image_path = os.path.join(output_folder, f"{method_name}_output.jpg")
result_image.save(output_image_path)
if __name__ == '__main__':
main()