Tips for batch image enhancement (offline/online) in deep learning

Method 1: Use the Augly library to write an easy-to-use image enhancement function

It is used to expand image samples and increase the diversity of samples. It is very simple to use. It needs to install the Augly library in advance and run it in the terminal virtual environment:

--------pip install augly

2. All the codes are shared as follows (the code still has room for optimization, optimize according to your own needs):

import os
import random
import augly.image as imaugs
import PIL.Image as Image

img_path = "./img_test"                              # 需要增强的图像路径
save_path = "./test_save"                           # 保存路径


def augly_augmentation(aug_image):
    aug = [
        imaugs.blur(aug_image, radius=random.randint(1, 2)),                        # 图像模糊
        imaugs.brightness(aug_image, factor=random.uniform(0.5, 1.5)),              # 改变亮度
        imaugs.change_aspect_ratio(aug_image, ratio=random.uniform(0.8, 1.5)),      # 改变图像宽高比
        imaugs.color_jitter(aug_image, brightness_factor=random.uniform(0.8, 1.5),  # 颜色晃动
                            contrast_factor=random.uniform(0.8, 1.5), saturation_factor=random.uniform(0.8, 1.5)),
        imaugs.crop(aug_image, x1=random.uniform(0, 0.1), y1=random.uniform(0, 0.1), x2=random.uniform(0.9, 1),
                    y2=random.uniform(0.9, 1)),                                 # 随机裁剪
        imaugs.hflip(aug_image),                                                # 水平翻转
        imaugs.opacity(aug_image, level=random.uniform(0.5, 1)),                # 改变图像透明度
        imaugs.pixelization(aug_image, ratio=random.uniform(0.5, 1)),           # 马赛克
        imaugs.random_noise(aug_image),                                         # 随机噪声
        imaugs.rotate(aug_image, degrees=random.randint(3, 10)),                # 随机旋转一定角度
        imaugs.shuffle_pixels(aug_image, factor=random.uniform(0, 0.1)),        # 随机像素比任意化
        imaugs.saturation(aug_image, factor=random.uniform(1, 1.5)),            # 改变饱和度
        imaugs.contrast(aug_image, factor=random.uniform(1, 1.5)),              # 对比度增强
        imaugs.grayscale(aug_image)                                             # 转灰度
    ]
    return random.choice(aug)                                                   # 从以上函数中随机选其一进行数据增强


for name in os.listdir(img_path):
    aug_image = Image.open(os.path.join(img_path, name))
    count = 3                           # 每张图片需要增强的次数
    for i in range(count):
        image = augly_augmentation(aug_image)
        image = image.convert("RGB")
        image.save(os.path.join(save_path, name[:-4]+"_{}.jpg".format(i)))

Of course, you can also choose to enhance online during the data loading process. You only need to get the image in __gettem__ in the class and call a function there:

Method 2: Use the image enhancement library imgaug to enhance and expand the data

1. The imgaug module provides mirroring, displacement, rotation, etc., and runs in the terminal virtual environment:

--------pip install imgaug

2. All the codes are shared as follows:

import numpy as np
import imgaug.augmenters as iaa
import os
import cv2


# 启用的卷积(滤波器)增广操作
def gen_matrix(image, nb_channels, random_state):
    matrix_A = np.array([[0, -1, 0],
                         [-1, 4, -1],
                         [0, -1, 0]])

    matrix_B = np.array([[0, 0, 0],
                         [0, -4, 1],
                         [0, 2, 1]])
    if random_state.rand() < 0.5:
        return [matrix_A] * nb_channels
    else:
        return [matrix_B] * nb_channels

 The main function is mainly to rewrite the method call of iaa

if __name__ == '__main__':
    # da = iaa.Sequential([])
    seq = iaa.SomeOf((2, None), [
        iaa.Crop(px=(0, 16)),  # 从每侧裁剪图像0到16px(随机选择)
        iaa.Fliplr(0.5),  # 水平翻转图像
        iaa.Flipud(0.5),  # 垂直翻转图像
        # iaa.GaussianBlur(sigma=(0, 1.0))  # 使用0到3.0的sigma模糊图像
        # iaa.Affine(rotate=45),  # 仿射旋转
        iaa.Sharpen(alpha=(0.8, 1.0), lightness=(0.9, 2.0)),  # 锐化图像,然后使用0.0到1.0之间的alpha将结果与原始图像重叠
        iaa.Noop(),  # Noop 无操作增强器
        iaa.CropAndPad(percent=(-0.25, 0.25)),  # 增强器按照像素或百分比(相对于输入图像大小)来裁剪/填充图像
        # iaa.Superpixels(p_replace=(0.1, 0.3), n_segments=4),  # 每个图像生成大约64个超像素。 用平均像素颜色替换每个概率为50%
        # 颜色空间从RGB更改为HSV,然后将50-100添加到第一个通道,然后转换回RGB
        iaa.Sequential([iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace="HSV"),
                        iaa.WithChannels(0, iaa.Add((0, 20))),
                        iaa.ChangeColorspace(from_colorspace="HSV", to_colorspace="RGB")]),
        iaa.Grayscale(alpha=(0.0, 1.0)),  # 将图像更改为灰度,并通过改变强度将其与原始图像叠加,有效地删除0到100%的颜色
        iaa.Emboss(alpha=1.0, strength=(0.5, 1.5)),  # 浮雕图像,然后使用0.0到1.0之间的alpha将结果与原始图像叠加
        # 检测图像中具有随机方向(0到360度)的边缘,将图像转换为黑白图像,然后使用介于0.0和1.0之间的随机alpha将这些图像与原始图像叠加
        iaa.DirectedEdgeDetect(alpha=(0.0, 0.5), direction=1.0),
        iaa.Affine(scale=(0.5, 1.5)),  # 将图像缩放到原始大小的50到150%的值
        iaa.ContrastNormalization((0.9, 1.5)),  # 将对比度标准化0.5到1.5倍,每个图像随机采样
        iaa.Invert(0.25, per_channel=0.5),  # 50%的图像,以25%的概率(每个图像)反转这些图像中的所有像素
        # 将50%的图像乘以0.5到1.5之间的随机值,并在剩余50%中乘以通道,即每个通道和像素独立地采样一个乘数
        iaa.MultiplyElementwise((0.5, 1.5), per_channel=0.5),
        iaa.Multiply((0.5, 1.5)),  # 将每个图像乘以0.5到1.5之间的随机值
        iaa.Add((-40, 40)),  # 图像中添加-40到40之间的随机值,每个图像对每个图像采样一次
        iaa.Convolve(matrix=gen_matrix)  # 使用3x3内核卷积每个图像,每个图像动态选择
    ], random_order=True)
    imgs_path = './img_test'
    img_files = os.listdir(imgs_path)
    for info_img in img_files:
        img_list = []
        img_file = imgs_path + '/' + info_img
        img = cv2.imread(img_file)
        img_list.append(img)
        images_aug = seq.augment_images(img_list)
        cv2.imwrite("./test/a0_" + info_img, images_aug[0])

The place that needs to be modified by yourself is mainly the path imgs_path to read the image, and the location to save the path, it’s over!

Guess you like

Origin blog.csdn.net/baidu_36590445/article/details/125736921