阅读 205

AugLy图像增强方法(常用的图像增强方法)

0 前言

Augly是由facebook ai开源的一款用于数据增强的python库,它支持音频、图像、视频、文本4种形式的数据,提供了超过100种数据增强功能,可以直接对输入的内容进行多种处理。对于图像而言,提供了裁剪、选择图像、图像加文字、增加饱和度、亮度调整等100多种数据增强的方式,pip安装,开箱即用,很方便我们用来对图像进行处理。可以说功能非常的强大了,不仅可以用来离线数据增强,还可以在torch的训练中进行在线的数据增强,具体怎么使用,可以参考官方给出的例子。

目前本文给出以下增强的方法

  • 图像模糊

  • 改变图像亮度

  • 改变图像高宽比

  • 颜色晃动

  • 对比度增强

  • 按比例裁剪

  • 改变图像质量

  • 转灰度

  • 水平翻转

  • 图片上方创建文字

  • 改变图像的不透明度

  • 图片添加表情

  • 两幅图像重叠

  • 网页覆盖

  • 图像中间加横条

  • 图片添加文字

  • 图像边界填充

  • 填充短边,成为正方形

  • 透视变换

  • 马赛克

  • 随机噪声

  • 图像旋转

  • 改变图像饱和度

  • 改变图像分辨率

  • 锐化

  • 随机像素比任意变化

  • 垂直翻转

1 Augly安装

!pip install augly 复制代码

Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Requirement already satisfied: augly in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (0.1.9) Requirement already satisfied: Pillow>=8.2.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from augly) (8.3.2) Requirement already satisfied: numpy>=1.19.5 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from augly) (1.21.2) Requirement already satisfied: nlpaug==1.1.3 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from augly) (1.1.3) Requirement already satisfied: python-magic>=0.4.22 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from augly) (0.4.24) Requirement already satisfied: regex>=2021.4.4 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from augly) (2021.9.30) Requirement already satisfied: iopath>=0.1.8 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from augly) (0.1.9) Requirement already satisfied: portalocker in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from iopath>=0.1.8->augly) (2.3.2) Requirement already satisfied: tqdm in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from iopath>=0.1.8->augly) (4.36.1) 复制代码

2 常用的图像增强接口

import augly.image as imaugs import PIL.Image as Image image_path = "day.jpeg" aug_image = Image.open(image_path) aug_image.show() 复制代码

output_4_0.png

# 图像模糊 output_path = "work/blur.jpeg" aug_image = Image.open(image_path) aug_image = imaugs.blur(aug_image,output_path=output_path) aug_image.show() 复制代码

output_5_0.png

# 改变图像亮度 output_path = "work/brightness.jpeg"             # 输出图片路径 aug_image = Image.open(image_path) aug_image = imaugs.brightness(aug_image,factor=1.5,output_path=output_path)     # 修改factor参数调整亮度 aug_image.show() 复制代码

output_6_0.png

# 改变图像高宽比 output_path = "work/change_aspect_ratio.jpeg" aug_image = Image.open(image_path) aug_image = imaugs.change_aspect_ratio(aug_image,ratio=2,output_path=output_path) aug_image.show() 复制代码

output_7_0.png

# 颜色晃动 output_path = "work/color_jitter.jpeg" aug_image = Image.open(image_path) aug_image = imaugs.color_jitter(aug_image,brightness_factor=1.0,contrast_factor=1.0,saturation_factor=1.5,output_path=output_path) aug_image.show() 复制代码

output_8_0.png

# 对比度增强 output_path = "work/contrast.jpeg" aug_image = Image.open(image_path) aug_image = imaugs.contrast(aug_image,factor=1.5,output_path=output_path) aug_image.show() 复制代码

output_9_0.png

# 按比例裁剪 output_path = "work/crop.jpeg" aug_image = Image.open(image_path) aug_image = imaugs.crop(aug_image,x1=0.1,y1=0.1,x2=0.9,y2=0.9,output_path=output_path) aug_image.show() 复制代码

output_10_0.png

# 改变图像质量 output_path = "work/encoding_quality.jpeg" aug_image = Image.open(image_path) aug_image = imaugs.encoding_quality(aug_image,output_path=output_path) aug_image.show() 复制代码

output_11_0.png

# 转灰度 output_path = "work/grayscale.jpeg" aug_image = Image.open(image_path) aug_image = imaugs.grayscale(aug_image,output_path=output_path) aug_image.show() 复制代码

output_12_0.png

# 水平翻转 output_path = "work/hflip.jpeg" aug_image = Image.open(image_path) aug_image = imaugs.hflip(aug_image,output_path=output_path) aug_image.show() 复制代码

output_13_0.png

# 图片上方创建文字 output_path = "work/meme_format.jpeg" aug_image = Image.open(image_path) aug_image = imaugs.meme_format(aug_image,text="hello world",caption_height=50,output_path=output_path) aug_image.show() 复制代码

output_14_0.png

# 改变图像的不透明度 output_path = "work/opacity.jpeg" aug_image = Image.open(image_path) aug_image = imaugs.opacity(aug_image,level=0.5,output_path=output_path) aug_image.show() 复制代码

output_15_0.png

# 图片添加表情 output_path = "work/overlay_emoji.jpeg" aug_image = Image.open(image_path) aug_image = imaugs.overlay_emoji(aug_image,output_path=output_path) aug_image.show() 复制代码

output_16_0.png

# 两幅图像重叠 image_path2 = "day1.jpeg" output_path = "work/overlay_image.jpeg" aug_image1 = Image.open(image_path) aug_image2 = Image.open(image_path2) aug_image = imaugs.overlay_image(aug_image1,image_path2,output_path=output_path) aug_image.show() 复制代码

output_17_0.png

# 网页覆盖 output_path = "work/overlay_onto_screenshot.jpeg" aug_image = Image.open(image_path) aug_image = imaugs.overlay_onto_screenshot(aug_image,output_path=output_path) aug_image.show() 复制代码

output_18_0.png

# 图像中间加横条 output_path = "work/overlay_stripes.jpeg" aug_image = Image.open(image_path) aug_image = imaugs.overlay_stripes(aug_image,line_width=0.1,output_path=output_path) aug_image.show() 复制代码

output_19_0.png

# 图片添加文字 output_path = "work/overlay_text.jpeg" aug_image = Image.open(image_path) aug_image = imaugs.overlay_text(aug_image,output_path=output_path) aug_image.show() 复制代码

output_20_0.png

# 图像边界填充 output_path = "work/pad.jpeg" aug_image = Image.open(image_path) aug_image = imaugs.pad(aug_image,w_factor=0.25,h_factor=0.25,output_path=output_path) aug_image.show() 复制代码

output_21_0.png

# 填充短边,成为正方形 output_path = "work/pad_square.jpeg" aug_image = Image.open(image_path) aug_image = imaugs.pad_square(aug_image,color=(255,255,255),output_path=output_path) aug_image.show() 复制代码

output_22_0.png

# 透视变换 output_path = "work/perspective_transform.jpeg" aug_image = Image.open(image_path) aug_image = imaugs.perspective_transform(aug_image,output_path=output_path) aug_image.show() 复制代码

output_23_0.png

# 马赛克 output_path = "work/pixelization.jpeg" aug_image = Image.open(image_path) aug_image = imaugs.pixelization(aug_image,ratio=0.5,output_path=output_path) aug_image.show() 复制代码

output_24_0.png

# 随机噪声 output_path = "work/random_noise.jpeg" aug_image = Image.open(image_path) aug_image = imaugs.random_noise(aug_image,output_path=output_path) aug_image.show() 复制代码

output_25_0.png

# 图像旋转 output_path = "work/rotate.jpeg" aug_image = Image.open(image_path) aug_image = imaugs.rotate(aug_image,degrees=10,output_path=output_path) aug_image.show() 复制代码

output_26_0.png

# 改变图像饱和度 output_path = "work/saturation.jpeg" aug_image = Image.open(image_path) aug_image = imaugs.saturation(aug_image,factor=1.5,output_path=output_path) aug_image.show() 复制代码

output_27_0.png

# 改变图像分辨率 output_path = "work/scale.jpeg" aug_image = Image.open(image_path) aug_image = imaugs.scale(aug_image,factor=0.8,output_path=output_path) aug_image.show() 复制代码

output_28_0.png

# 锐化 output_path = "work/sharpen.jpeg" aug_image = Image.open(image_path) aug_image = imaugs.sharpen(aug_image,factor=1.5,output_path=output_path) aug_image.show() 复制代码

output_29_0.png

# 随机像素比任意变化 output_path = "work/shuffle_pixels.jpeg" aug_image = Image.open(image_path) aug_image = imaugs.shuffle_pixels(aug_image,factor=0.1,output_path=output_path) aug_image.show() 复制代码

output_30_0.png

# 垂直翻转 output_path = "work/vflip.jpeg" aug_image = Image.open(image_path) aug_image = imaugs.vflip(aug_image,output_path=output_path) aug_image.show() 复制代码

output_31_0.png


作者:Livingbody
链接:https://juejin.cn/post/7056296801179009055


文章分类
人工智能
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐