【问题标题】:augmentation on images to look like real example python增强图像看起来像真实示例 python
【发布时间】:2021-07-09 21:55:50
【问题描述】:

我已经生成了许多图像,如下图所示[![在此处输入图像描述] 现在,要转换所有此类图像,例如real world vehicle number plate 图像。 例如- 如何将这些类型的augmentation 保存在另一个文件夹中。

【问题讨论】:

    标签: python opencv image-processing image-augmentation albumentations


    【解决方案1】:

    解决方案

    查看图书馆:albumentations。试着回答这个问题:“你拥有的图像和你想要的图像有什么区别?”。例如,该图像是:

    • 像素化程度更高,
    • 颗粒状,
    • 分辨率较低,
    • 上面也可以有钉子/紧固螺钉
    • 可能在主编号下方或上方写有其他内容
    • 可能有阴影
    • 车牌可能在某些地方亮度不均等。

    Albumentations,帮助您想出多种类型的图像增强。请尝试按照我的建议分解此问题,然后尝试从allementations 中找出您需要哪些augmentations。

    使用albumentations 的图像增强示例

    以下代码块 (source) 向您展示了如何为图像增强应用标注。如果你有一个图像和一个蒙版,它们都会经历相同的转换。

    另一个来自 kaggle 的例子:Image Augmentation Demo with albumentation

    from albumentations import (
        HorizontalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,
        Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,
        IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine,
        IAASharpen, IAAEmboss, RandomBrightnessContrast, Flip, OneOf, Compose
    )
    import numpy as np
    
    def strong_aug(p=0.5):
        return Compose([
            RandomRotate90(),
            Flip(),
            Transpose(),
            OneOf([
                IAAAdditiveGaussianNoise(),
                GaussNoise(),
            ], p=0.2),
            OneOf([
                MotionBlur(p=0.2),
                MedianBlur(blur_limit=3, p=0.1),
                Blur(blur_limit=3, p=0.1),
            ], p=0.2),
            ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.2, rotate_limit=45, p=0.2),
            OneOf([
                OpticalDistortion(p=0.3),
                GridDistortion(p=0.1),
                IAAPiecewiseAffine(p=0.3),
            ], p=0.2),
            OneOf([
                CLAHE(clip_limit=2),
                IAASharpen(),
                IAAEmboss(),
                RandomBrightnessContrast(),
            ], p=0.3),
            HueSaturationValue(p=0.3),
        ], p=p)
    
    image = np.ones((300, 300, 3), dtype=np.uint8)
    mask = np.ones((300, 300), dtype=np.uint8)
    whatever_data = "my name"
    augmentation = strong_aug(p=0.9)
    data = {"image": image, "mask": mask, "whatever_data": whatever_data, "additional": "hello"}
    augmented = augmentation(**data)
    image, mask, whatever_data, additional = augmented["image"], augmented["mask"], augmented["whatever_data"], augmented["additional"]
    

    策略

    • 首先将增强的数量减少到最低限度
    • 保存单个增强图像
    • 在增强后保存一些图像。
    • 现在测试和更新您的增强管道,以满足您模拟真实场景的要求。
    • 最终确定您的管道并在更多图像上运行它。
    • 计时:多少张图片需要多长时间。
    • 然后最后在所有图像上运行它:这次您可以估计运行它需要多长时间。

    注意:每次图像通过增强管道时,只有一个增强图像实例从中出来。因此,假设您想要每张图片有 10 个不同的增强版本,您需要将每张图片通过增强管道 10 次,然后再继续处理下一张图片。

    # this will not be what you end up using
    # but you can begin to understand what 
    # you need to do with it.
    
    def simple_aug(p-0,5):
        return return Compose([
            RandomRotate90(),
            # Flip(),
            # Transpose(),
            OneOf([
                IAAAdditiveGaussianNoise(),
                GaussNoise(),
            ], p=0.2),
        ])
    
    # for a single image: check first
    image = ... # write your code to read in your image here
    augmentation = strong_aug(p=0.5)
    
    augmented = augmentation({'image': image}) # see albumentations docs
    # SAVE the image
    # If you are using imageio or PIL, saving an image 
    # is rather straight forward, and I will let you
    # figure that out.
    # save the content of the variable: augmented['image']
    

    多张图片

    假设每个图像通过增强管道传递10 次,您的代码可能如下所示:

    import os
    
    # I assume you have a way of loading your 
    # images from the filesystem, and they come 
    # out of `images` (an iterator)
    
    NUM_AUG_REPEAT = 10
    AUG_SAVE_DIR = 'data/augmented'
    
    # create directory of not present already
    if not os.path.isdir(AUG_SAVE_DIR):
        os.makedirs(AUG_SAVE_DIR)
    
    # This will create augmentation ids for the same image
    # example: '00', '01', '02', ..., '08', '09' for
    #          - NUM_AUG_REPEAT = 10
    aug_id = lambda x: str(x).zfill(len(str(NUM_AUG_REPEAT)))
    
    for image in images:
        for i in range(NUM_AUG_REPEAT):
            data = {'image': image}
            augmented = augmentation(**data)
            # I assume you have a function: save_image(image_path, image)
            # You need to write this function with 
            # whatever logic necessary. (Hint: use imageio or PIL.Image)
            image_filename = f'image_name_{aug_id(i)}.png'
            save_image(os.path.join(AUG_SAVE_DIR, image_filename), augmented['image'])
    

    【讨论】:

    • 增强我可以尝试,但是因为我有 1000 个这样的图像要增强..我无法对所有图像进行一次增强并将所有增强的图像保存到单独的文件夹中,你能帮我解决这个问题吗?
    • @CypherX 我也被困在同一种增强中,并对文件夹中的所有图像应用增强并保存在单独的文件夹中,如果您详细说明这一点,将会有很大帮助
    • @k_p 看起来您遇到了文件管理问题,而不是图像处理问题。听起来你只需要一个 for 循环来表示“for in_img in input_folder: out_img = do_augmentation(in_img); save(out_img, output_folder)”。如果是这样,请显示您的图像读取/保存代码以获得帮助。
    • @k_p 您可以即时扩充它们,然后(假设您想训练一些图像模型)将它们提供给您训练的模型。或者,您可以对这些图像应用增强,然后将它们保存到文件夹中。当然,它会运行一段时间,但让所有这些都通过你的增强程序只是时间问题。我建议你尝试一些非常基本的东西(简单的增强),然后在它之上进行开发。
    • @k_p 我更新了答案。请检查一下。我希望这会有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 2019-08-06
    • 2012-02-02
    • 1970-01-01
    • 2015-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多