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

lsmepo6l  于 2021-09-08  发布在  Java
关注(0)|答案(1)|浏览(467)

我已经生成了许多图像,如下图[[在此处输入图像描述]现在,要转换所有此类图像,如 real world vehicle number plate 形象。例如—

如何解决这些问题 augmentation 并将所有增强图像保存在另一个文件夹中。

unftdfkk

unftdfkk1#

解决方案

查看图书馆: albumentations . 试着回答以下问题:“你拥有的形象和你想要的形象有什么不同?”。例如,该图像是:
更加像素化,
颗粒状的,
分辨率较低,,
也可能有钉子/紧固螺钉
可能在主数字下方或上方写有其他内容
可能有阴影
号码牌在某些地方的亮度可能不均匀。
相册,帮助你想出许多类型的图像增强。请试着像我建议的那样解决这个问题,然后试着从相册中找出你需要的插图。

使用相册进行图像增强的示例

下面的代码块(源代码)向您展示了如何将相册应用于图像增强。如果您有一个图像和一个遮罩,它们都将经历相同的变换。
kaggle的另一个例子:带有相册的图像增强演示

  1. from albumentations import (
  2. HorizontalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,
  3. Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,
  4. IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine,
  5. IAASharpen, IAAEmboss, RandomBrightnessContrast, Flip, OneOf, Compose
  6. )
  7. import numpy as np
  8. def strong_aug(p=0.5):
  9. return Compose([
  10. RandomRotate90(),
  11. Flip(),
  12. Transpose(),
  13. OneOf([
  14. IAAAdditiveGaussianNoise(),
  15. GaussNoise(),
  16. ], p=0.2),
  17. OneOf([
  18. MotionBlur(p=0.2),
  19. MedianBlur(blur_limit=3, p=0.1),
  20. Blur(blur_limit=3, p=0.1),
  21. ], p=0.2),
  22. ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.2, rotate_limit=45, p=0.2),
  23. OneOf([
  24. OpticalDistortion(p=0.3),
  25. GridDistortion(p=0.1),
  26. IAAPiecewiseAffine(p=0.3),
  27. ], p=0.2),
  28. OneOf([
  29. CLAHE(clip_limit=2),
  30. IAASharpen(),
  31. IAAEmboss(),
  32. RandomBrightnessContrast(),
  33. ], p=0.3),
  34. HueSaturationValue(p=0.3),
  35. ], p=p)
  36. image = np.ones((300, 300, 3), dtype=np.uint8)
  37. mask = np.ones((300, 300), dtype=np.uint8)
  38. whatever_data = "my name"
  39. augmentation = strong_aug(p=0.9)
  40. data = {"image": image, "mask": mask, "whatever_data": whatever_data, "additional": "hello"}
  41. augmented = augmentation(**data)
  42. image, mask, whatever_data, additional = augmented["image"], augmented["mask"], augmented["whatever_data"], augmented["additional"]

策略

首先,将增强的数量降至最低
保存单个增强图像
放大后保存一些图像。
现在测试并更新您的增强管道,以满足您模拟地面实况场景的需求。
完成管道并在大量图像上运行。
时间:多少张图片需要多长时间。
然后最后在所有图像上运行它:这一次,您可以对运行它所需的时间进行时间估计。
注意:每次图像通过增强管道时,只有一个增强图像示例从中出来。因此,假设您想要每个图像的10个不同的增强版本,您需要将每个图像通过增强管道10次,然后再转到下一个图像。

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

对于多个图像

假设每个图像都经过 10 通过扩充管道,您的代码可能如下所示:

  1. import os
  2. # I assume you have a way of loading your
  3. # images from the filesystem, and they come
  4. # out of `images` (an iterator)
  5. NUM_AUG_REPEAT = 10
  6. AUG_SAVE_DIR = 'data/augmented'
  7. # create directory of not present already
  8. if not os.path.isdir(AUG_SAVE_DIR):
  9. os.makedirs(AUG_SAVE_DIR)
  10. # This will create augmentation ids for the same image
  11. # example: '00', '01', '02', ..., '08', '09' for
  12. # - NUM_AUG_REPEAT = 10
  13. aug_id = lambda x: str(x).zfill(len(str(NUM_AUG_REPEAT)))
  14. for image in images:
  15. for i in range(NUM_AUG_REPEAT):
  16. data = {'image': image}
  17. augmented = augmentation(**data)
  18. # I assume you have a function: save_image(image_path, image)
  19. # You need to write this function with
  20. # whatever logic necessary. (Hint: use imageio or PIL.Image)
  21. image_filename = f'image_name_{aug_id(i)}.png'
  22. save_image(os.path.join(AUG_SAVE_DIR, image_filename), augmented['image'])
展开查看全部

相关问题