数据增强代码

实现功能:

                1、随机亮度

                2、随机裁剪

                3、椒盐噪声

                4、高斯噪声

                5、平移

                6、选装

from PIL import Image, ImageEnhance
import random
import numpy as np


def random_brightness_enhance_and_save(image_path, output_path):
    """
    随机亮度增强函数,并保存增强后的图像,在保存前转换图像模式为RGB。

    参数:
    image_path: 输入图像的路径。
    output_path: 增强后图像的保存路径。
    """

    # 打开图像
    img = Image.open(image_path)
    # 生成随机亮度增强因子,范围从0.5到1.5
    enhancer_factor = random.uniform(1.4, 1.5)
    # 创建一个亮度增强器
    enhancer = ImageEnhance.Brightness(img)
    # 应用增强因子
    img_enhanced = enhancer.enhance(enhancer_factor)
    # 如果图像模式是RGBA,则转换为RGB
    if img_enhanced.mode == 'RGBA':
        img_enhanced = img_enhanced.convert('RGB')
    # 保存增强后的图像
    img_enhanced.save(output_path)

def add_salt_and_pepper_noise(image_path, output_path, noise_ratio=0.02):
    """
    给图像添加椒盐噪声并保存。

    参数:
    image_path: 输入图像的路径。
    output_path: 增强后图像的保存路径。
    noise_ratio: 噪声比例,默认为0.02。
    """
    # 打开图像
    img = Image.open(image_path)
    img = img.convert('RGB')  # 确保图像是RGB模式

    # 获取图像数据
    pixels = img.load()

    for i in range(img.size[0]):  # 对于所有的x
        for j in range(img.size[1]):  # 对于所有的y
            r = random.random()  # 生成一个随机数
            if r < noise_ratio / 2:
                # 将像素值设置为黑色
                # pixels[i, j] = (0, 0, 0)
                pass
            elif r < noise_ratio:
                # 将像素值设置为白色
                pixels[i, j] = (255, 255, 255)

    # 保存增强后的图像
    img.save(output_path)
def add_gaussian_noise(image_path, output_path, mean=0, sigma=25):
    """
    给图像添加高斯噪声并保存。

    参数:
    image_path: 输入图像的路径。
    output_path: 增强后图像的保存路径。
    mean: 高斯噪声的均值,默认为0。
    sigma: 高斯噪声的标准差,默认为25。
    """
    # 打开图像并转换为数组
    img = Image.open(image_path)
    img = img.convert('RGB')  # 确保图像是RGB模式
    img_arr = np.array(img)

    # 生成高斯噪声
    noise = np.random.normal(mean, sigma, img_arr.shape)

    # 将噪声添加到图像数组
    noisy_img_arr = img_arr + noise

    # 确保结果仍然在合法的像素范围内
    noisy_img_arr_clipped = np.clip(noisy_img_arr, 0, 255)

    # 将结果数组转换回图像
    noisy_img = Image.fromarray(noisy_img_arr_clipped.astype('uint8'), 'RGB')

    # 保存增强后的图像
    noisy_img.save(output_path)


def translate_image(image_path, output_path, translate_x, translate_y):
    """
    对图像进行平移并保存。

    参数:
    image_path: 输入图像的路径。
    output_path: 处理后图像的保存路径。
    translate_x: 水平方向上的平移距离,正值向右平移,负值向左平移。
    translate_y: 垂直方向上的平移距离,正值向下平移,负值向上平移。
    """
    img = Image.open(image_path)

    # 创建平移变换矩阵
    translation_matrix = (1, 0, translate_x, 0, 1, translate_y)

    # 应用平移变换
    translated_img = img.transform(img.size, Image.AFFINE, translation_matrix)

    # 如果图像模式是RGBA,则转换为RGB
    if translated_img.mode == 'RGBA':
        translated_img = translated_img.convert('RGB')
    # 保存处理后的图像
    translated_img.save(output_path)


def rotate_image(image_path, output_path, angle, expand=True):
    """
    对图像进行旋转并保存。

    参数:
    image_path: 输入图像的路径。
    output_path: 处理后图像的保存路径。
    angle: 旋转角度,正值表示逆时针旋转,负值表示顺时针旋转。
    expand: 是否扩展图像的大小以适应整个旋转后的图像,默认为True。
    """
    img = Image.open(image_path)

    # 应用旋转变换
    rotated_img = img.rotate(angle, expand=expand)

    # 如果图像模式是RGBA,则转换为RGB
    if rotated_img.mode == 'RGBA':
        rotated_img = rotated_img.convert('RGB')
    # 保存处理后的图像
    rotated_img.save(output_path)


# 使用示例
# 假设你有一个名为"example.png"的图像文件(可能包含透明通道),并希望保存增强后的图像为"enhanced_example.jpg"
random_brightness_enhance_and_save("1.jpg", "./random_light.jpg")
# 假设你有一个名为"example.jpg"的图像文件,并希望保存添加椒盐噪声后的图像为"sp_noise_example.jpg"
add_salt_and_pepper_noise("1.jpg", "sp_noise_example.jpg", noise_ratio=0.05)
# 假设你有一个名为"example.jpg"的图像文件,并希望保存添加高斯噪声后的图像为"gaussian_noise_example.jpg"
add_gaussian_noise("1.jpg", "gaussian_noise_example.jpg")
# 使用示例
translate_image("1.jpg", "translated_example.jpg", 50, 30)
# 使用示例
rotate_image("1.jpg", "rotated_example.jpg", 45)

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值