对图像进行增强,使其看起来像真实的python示例

2024-05-23 23:08:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经生成了许多图像,如下图所示[![在此处输入图像描述] 现在,希望转换所有此类图像,如real world vehicle number plateimage。 比如说- enter image description here 如何创建这些类型的augmentation并将所有增强图像保存到另一个文件夹中


Tags: 图像文件夹类型numberworldrealvehicleaugmentation
1条回答
网友
1楼 · 发布于 2024-05-23 23:08:03

解决方案

查看库:^{}。尝试回答以下问题:“您拥有的图像与您想要的图像之间有什么区别?”。例如,该图像是:

  • 更加像素化
  • 颗粒状的
  • 分辨率较低,
  • 也可能有钉子/紧固螺钉
  • 可能在主数字下方或上方写有其他内容
  • 可能有阴影
  • 号码牌在某些地方的亮度可能不均匀

Albumentations,帮助您提出多种类型的图像增强。请试着像我建议的那样解决这个问题,然后试着从相册中找出你需要的插图

使用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"]

策略

  • 首先,将增强的数量降至最低
  • 保存单个增强图像
  • 放大后保存一些图像
  • 现在测试并更新您的增强管道,以满足您模拟地面实况场景的需求
  • 完成管道并在大量图像上运行
  • 时间:多少张图片需要多长时间
  • 然后最后在所有图像上运行它:这一次,您可以对运行它所需的时间进行时间估计

NOTE: every time an image passes through the augmentation pipeline, only a single instance of augmented image comes out of it. So, say you want 10 different augmented versions of each image, you will need to pass each image through the augmentation pipeline 10 times, before moving on to the next image.

# 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'])

相关问题 更多 >