目录
Augmentor安装
pip install Augmentor - https://pypi.douban.com/simple
使用Augmentor
注意:图片的名字一定要和掩码的名字相同(包括后缀名),否则不能对掩码增强,生成的增强图像和掩码图像都在Pipeline管道指定文件下的output文件夹下
import Augmentor
def augment_data():
# 不需要读取图像,在管道中给定路径就行,增强的操作只需在管道添加
p = Augmentor.Pipeline('./images') # 真实图像的路径
p.ground_truth('./labels') # 掩码图像的路径 名字一定要相同,否则不能生成mask的增强
# 图像旋转,按照0.5的概率,最大左旋转角度20,右旋转角度10
# 左右的角度必须在25之内
p.rotate(probability=.5, max_left_rotation=20, max_right_rotation=10)
# 图像左右翻转
p.flip_left_right(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)
# 最终扩充的样本数量
p.sample(10)
将增强后的图像和掩码分开
import os
def split_image_mask():
# 原始图像的文件夹
orgin_dir = './augment_data/images'
# mask图像的文件夹
mask_dir = './augment_data/labels'
if not os.path.exists(orgin_dir):
os.makedirs(orgin_dir)
if not os.path.exists(mask_dir):
os.makedirs(mask_dir)
output_dir = './images/output' # 上面生成的增强图像都在这个目录下
all_img_list = os.listdir(output_dir)
count = 1
for img_name in all_img_list:
"""找到对应的img和mask"""
if img_name.startswith('_groundtruth_'):
# 将掩码移到mask_dir文件夹下
img_path = img_name.split('_', maxsplit=4)[-1]
os.rename(os.path.join(output_dir, img_name), os.path.join(mask_dir, str(count) + '.jpg'))
# 图像移动到orgin_dir下
os.rename(os.path.join(output_dir, 'images_original_' + img_path),
os.path.join(orgin_dir, str(count) + '.jpg'))
count += 1