语义分割中的数据增强Augmentor基本使用方法(全随机生成和对每张图片均进行一次增强)

目录

一、安装

二、使用

1. 载入包

2. 图片增强

3. 图像输出


一、安装

pip install Augmentor

二、使用

1. 载入包

注意此处两个文件夹中图片名必须完全一致,包括后缀名。这样的话对原图进行了操作也会同步对掩码图像进行操作(要是jpg都得是jpg,要是png都得是png,本人用的labelme后josn文件转的png,是将jpg的原图转化成png,而不是对掩码图像进行修改)

import Augmentor

#注意此处两个文件夹中图片名必须完全一致,包括后缀名
p = Augmentor.Pipeline('./images') #images中是原图像
p.ground_truth('./labels') #labels中是掩码图像

2. 图片增强

基于现在网上关于Augmentor的使用方法不多,很多功能都没有被列出来,可以使用help去查看一下里面包含的各种用法。

help(Augmentor)
Methods inherited from Pipeline:
     |  
     |  __call__(self, augmentor_image)
     |      Function used by the ThreadPoolExecutor to process the pipeline
     |      using multiple threads. Do not call directly.
     |      
     |      This function does nothing except call :func:`_execute`, rather
     |      than :func:`_execute` being called directly in :func:`sample`.
     |      This makes it possible for the procedure to be *pickled* and
     |      therefore suitable for multi-threading.
     |      
     |      :param augmentor_image: The image to pass through the pipeline.
     |      :return: None
     |  
     |  black_and_white(self, probability, threshold=128)
     |      Convert images to black and white. In other words convert the image
     |      to use a 1-bit, binary palette. The threshold defaults to 128,
     |      but can be controlled using the :attr:`threshold` parameter.
     |      
     |      .. seealso:: The :func:`greyscale` function.
     |      
     |      :param probability: A value between 0 and 1 representing the
     |       probability that the operation should be performed. For resizing,
     |       it is recommended that the probability be set to 1.
     |      :param threshold: A value between 0 and 255 which controls the
     |       threshold point at which each pixel is converted to either black
     |       or white. Any values above this threshold are converted to white, and
     |       any values below this threshold are converted to black.
     |      :type probability: Float
     |      :type threshold: Integer
     |      :return: None

里面有大量关于Augmentor用法,可以自己去查找,下面列举了一些我自己常用的方法。

# 图像旋转,按照0.5的概率,最大左旋转角度20,右旋转角度10
# 左右的角度必须在25之内
p.rotate(probability=.5, max_left_rotation=20, max_right_rotation=10)
# 图像左右、上下、随机左右上下翻转
p.flip_left_right(probability=0.5)
p.flip_top_bottom(probability=0.5)
p.flip_random(probability=1)
# 旋转90
p.rotate90(probability=0.5)
# # 图像放大缩小,按照概率为0.5,面积为原来的0.9倍
p.zoom_random(probability=.5, percentage_area=0.9)
# # 裁剪
p.crop_random(probability=.5, percentage_area=0.8)
# 亮度
p.random_brightness(probability=.2, min_factor=0.1, max_factor=0.9)

虽然这些方法都应该可以使用,但我在跑代码以后,出现了部分操作掩码图像生成以后是全黑的情况,经过排查只要有关于大小变动的好像都不能进行好掩码图的操作,所以本人直接放弃了部分操作。(我的数据集只需要进行简单的增强)

3. 图像输出

两种操作过后都会在images文件夹下生成output文件夹,生成的结果都存在里面了。

第一种生成图像存在重复图像过多和部分图像删减没了的问题,因此我用的一般都是第二种。

# 最终扩充的样本数量
p.sample(6)
# 对文件夹中每个图片进行一次操作
p.process()

比如你需要对每张图片进行一次某操作,就可以这样。

for i in range(1,5):
    if i == 1:
        p.flip_top_bottom(probability=1)
        p.process()
    if i == 2:
        p.flip_top_bottom(probability=1)
        p.flip_left_right(probability=1)
        p.process()
    if i == 3:
        p.flip_left_right(probability=1)
        p.rotate90(probability=1)
        p.process()
    if i == 4:
        p.rotate180(probability=1)
        p.process()

这样基本上就可以解决大部分的语义分割图像增强的问题了。

你可能感兴趣的:(python语义分割,python,图像处理,计算机视觉)