python图像增强工具imgaug

1.简介:

https://github.com/aleju/imgaug
imgaug是一个封装好的用来进行图像augmentation的python库

2.增强操作类型

Fliplr:水平翻转
Flipud:纵向翻转
CropAndPad:裁剪图片
Affine:变形操作

Superpixels:生成随机数量的超像素区域,对原图进行替换,直观效果是原图部分区域变得模糊

各种blur:模糊,对应几种滤波操作

sharp:字面意思,锐化

emboss:压印浮凸字体(或图案); 凹凸印

EdgeDetect:边缘检测

DirectedEdgeDetect:边缘检测,只检测某些方向的,直观来看和上面的比检测出来的数目会少很多

AdditiveGaussianNoise:增加高斯噪声

DropOut:随机丢弃像素

CoarseDropout:随机丢弃某位置某通道像素

Invert:有一定几率将batch中的图片像素取反(或者特定通道取反)

Add:像素值成比例增加/减小(特指亮度)

AddToHueAndSaturation:增加色相、饱和度

Multiply:每个像素随机乘一个数(各不相图),造成局部变亮、局部变暗

ContrastNormalization:调整对比度,0.5表示和128的差值部分会处以2降低对比度

FrequencyNoiseAlpha:参数需要两个增强函数,本函数会混合两个增强函数增强后的结果

Grayscale:灰度图和原图的混合(1意味着全灰度)

3.代码示例

import numpy as np
import imgaug as ia
import imgaug.augmenters as iaa

images = np.random.randint(0, 255, (16, 128, 128, 3), dtype=np.uint8)

.
sometimes = lambda aug: iaa.Sometimes(0.5, aug)

seq = iaa.Sequential(
[
# apply the following augmenters to most images
iaa.Fliplr(0.5), # horizontally flip 50% of all images
iaa.Flipud(0.2), # vertically flip 20% of all images
# crop images by -5% to 10% of their height/width
sometimes(iaa.CropAndPad(
percent=(-0.05, 0.1),
pad_mode=ia.ALL,
pad_cval=(0, 255)
)),
sometimes(iaa.Affine(
scale={“x”: (0.8, 1.2), “y”: (0.8, 1.2)}, # scale images to 80-120% of their size, individually per axis
translate_percent={“x”: (-0.2, 0.2), “y”: (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
rotate=(-45, 45), # rotate by -45 to +45 degrees
shear=(-16, 16), # shear by -16 to +16 degrees
order=[0, 1], # use nearest neighbour or bilinear interpolation (fast)
cval=(0, 255), # if mode is constant, use a cval between 0 and 255
mode=ia.ALL # use any of scikit-image’s warping modes (see 2nd image from the top for examples)
)),
# execute 0 to 5 of the following (less important) augmenters per image
# don’t execute all of them, as that would often be way too strong
iaa.SomeOf((0, 5),
[
sometimes(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), # convert images into their superpixel representation
iaa.OneOf([
iaa.GaussianBlur((0, 3.0)), # blur images with a sigma between 0 and 3.0
iaa.AverageBlur(k=(2, 7)), # blur image using local means with kernel sizes between 2 and 7
iaa.MedianBlur(k=(3, 11)), # blur image using local medians with kernel sizes between 2 and 7
]),
iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), # sharpen images
iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), # emboss images
# search either for all edges or for directed edges,
# blend the result with the original image using a blobby mask
iaa.SimplexNoiseAlpha(iaa.OneOf([
iaa.EdgeDetect(alpha=(0.5, 1.0)),
iaa.DirectedEdgeDetect(alpha=(0.5, 1.0), direction=(0.0, 1.0)),
])),
iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5), # add gaussian noise to images
iaa.OneOf([
iaa.Dropout((0.01, 0.1), per_channel=0.5), # randomly remove up to 10% of the pixels
iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2),
]),
iaa.Invert(0.05, per_channel=True), # invert color channels
iaa.Add((-10, 10), per_channel=0.5), # change brightness of images (by -10 to 10 of original value)
iaa.AddToHueAndSaturation((-20, 20)), # change hue and saturation
# either change the brightness of the whole image (sometimes
# per channel) or change the brightness of subareas
iaa.OneOf([
iaa.Multiply((0.5, 1.5), per_channel=0.5),
iaa.FrequencyNoiseAlpha(
exponent=(-4, 0),
first=iaa.Multiply((0.5, 1.5), per_channel=True),
second=iaa.LinearContrast((0.5, 2.0))
)
]),
iaa.LinearContrast((0.5, 2.0), per_channel=0.5), # improve or worsen the contrast
iaa.Grayscale(alpha=(0.0, 1.0)),
sometimes(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)), # move pixels locally around (with random strengths)
sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))), # sometimes move parts of the image around
sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1)))
],
random_order=True
)
],
random_order=True
)
images_aug = seq(images=images)

猜你喜欢

转载自blog.csdn.net/zephyr_wang/article/details/107507738