3个python库的图像增强

3个python库的图像增强

原文链接:https://mp.weixin.qq.com/s/eokBnpckCY-bCO5BHI7TeQ

图像数据增强,通过使用一个或者多个增强技术来生成用于训练的各种图像,从而增加现有数据集的多样性。

使用各种技术来增强图像数据,包括:
1、使用几何变换(例如翻转、剪裁、旋转、缩放)等增强图像数据;
2、通过使用颜色转换来增强图像数据,例如调整亮度、暗度、锐度和饱和度等;
3、通过随机擦除,混合图像来增强图像数据。

imgaug

iimgaug可以在机器学习实验中增强图像,适用于各种增强技术,它有一个简单而强大的界面,可以增强图像、地表、边界框、热图和分割图等。

#导入扩充数据所需要的所有的必要包
import imageio
import imgaug as ia
import imgaug.augmenters as iaa
import ipyplot
#导入图像实例
input_img = imageio.imread(r"C:\\Users\\Administrator\\Desktop\\bus.jpg")
#1、图像翻转 
#水平翻转
hflip = iaa.Fliplr(p=1.0)
input_hf = hflip.augment_image(input_img)
#垂直翻转
vflip = iaa.Flipud(p=1.0)
input_vf = vflip.augment_image(input_img)
images_list = [input_img, input_hf, input_vf]
labels = ['Original', 'Horizontally flipped', 'Vertically flipped']
ipyplot.plot_images(images_list,labels=labels,img_width=180)
#2、图像旋转
# 通过以度为单位定义旋转,我们可以旋转图像
rot1 = iaa.Affine(rotate=(-20,20))
input_rot1 = rot1.augment_image(input_img)
images_list = [input_img, input_rot1]
labels = ['Original', 'Rotated Image']
ipyplot.plot_images(images_list,labels=labels,img_width=180)
#3、图像剪裁
#从图像的侧面移除像素的行或者列,从全尺寸中提取较小尺寸的子图像。
crop1 = iaa.Crop(percent=(0,0.3))
input_crop1 = crop1.augment_image(input_img)
images_list = [input_img, input_crop1]
labels = ['Original', 'Cropped Image']
ipyplot.plot_images(images_list,labels=labels,img_width=180)
#4、为图像增加噪声
noise = iaa.AdditiveGaussianNoise(10,40)
input_noise = noise.augment_image(input_img)
images_list = [input_img, input_noise]
labels = ['Original', 'Gaussian Noise Image']
ipyplot.plot_images(images_list,labels=labels,img_width=180)
#5、图像剪切
shear = iaa.Affine(shear=(-40,40))
input_shear = shear.augment_image(input_img)
images_list = [input_img, input_shear]
labels = ['Original', 'Image shearing']
ipyplot.plot_images(images_list,labels=labels,img_width=180)
#6、图像对比度 :通过缩放像素值来调整图像对比度
#contrast = iaa.GammaContrast((0.5,2.0))
contrast_sig = iaa.SigmoidContrast(gain=(5,10),cutoff=(0.4,0.6))
constrast_lin = iaa.LinearContrast(0.6,0.4)
#input_contrast = contrast.augment_image(input_img)
sigmoid_contrast = contrast_sig.augment_image(input_img)
linear_contrast = constrast_lin.augment_image(input_img)
images_list = [input_img, contrast_sig,constrast_lin]
labels = ['Original','sigmoid contrast','linear contrast']
ipyplot.plot_images(images_list,labels=labels,img_width=180)
#7、图像转换:通过位移场在局部移动像素来变换图像,增强器的参数是alpha和sigma,位移的强度由alpha控制,其中较大的值表示像素鱼洞的更远
#位移的平滑度由sigma控制,其中较大的值会导致更平滑的图案
elastic = iaa.ElasticTransformation(alpha=60.0,sigma=4.0)
#polar首先在极坐标表示中应用裁剪和填充,然后再将其扭转回笛卡尔表示。这个增强器可以为图像添加额外的像素
polar = iaa.WithPolarWarping(iaa.CropAndPad(percent=(-0.2,0.7)))
#“Jigsaw”增强以类似于拼图模式的方式移动图片内的单元格。
jigsaw =iaa.Jigsaw(nb_rows=20,nb_cols=15,max_steps=(3,7))
input_ealstic = elastic.augment_image(input_img)
input_polar = polar.augment_image(input_img)
input_jigsaw = jigsaw.augment_image(input_img)
images_list = [input_img, input_ealstic,input_polar,]
labels = ['Original','sigmoid contrast','linear contrast','input_jigsaw']
ipyplot.plot_images(images_list,labels=labels,img_width=180)
#8、图像上的边界框
from imgaug.augmentables.bbs import BoundingBox,BoundingBoxesOnImage
bbs = BoundingBoxesOnImage(BoundingBox(x1=40,x2=550,y1=40,y2=780),shape=input_img.shape)
ia.imshow(bbs.draw_on_image(input_img))

Albumentations

Albumentations 可以执行所有典型的计算机视觉任务,包括分类、语义分割、实例分割、对象识别和姿势估计。该库包含 70 多种不同的增强功能。

#水平翻转
transform = A.HorizontalFlip(p=0.5)
augmented_image = transform(image=input_img)['image']
plt.figure(figsize=(4, 4))
plt.axis('off')
plt.imshow(augmented_image)
#垂直翻转
transform = A.VerticalFlip(p=1)
augmented_image = transform(image=input_img)['image']
plt.figure(figsize=(4, 4))
plt.axis('off')
plt.imshow(augmented_image)
#图像缩放和旋转
transform = A.ShiftScaleRotate(p=0.5)
random.seed(7) 
augmented_image = transform(image=input_img)['image']
plt.figure(figsize=(4, 4))
plt.axis('off')
plt.imshow(augmented_image)
#图像随机重排
from albumentations.augmentations.transforms import ChannelShuffle
transform = ChannelShuffle(p=1.0)
random.seed(7) 
augmented_image = transform(image=input_img)['image']
plt.figure(figsize=(4, 4))
plt.axis('off')
plt.imshow(augmented_image)
#图像曝光
from albumentations.augmentations.transforms import Solarize
transform = Solarize(threshold=200,  p=1.0)
augmented_image = transform(image=input_img)['image']
plt.figure(figsize=(4, 4))
plt.axis('off')
plt.imshow(augmented_image)
#图像反转
from albumentations.augmentations.transforms import InvertImg
transform = InvertImg(p=1.0)
augmented_image = transform(image=input_img)['image']
plt.figure(figsize=(4, 4))
plt.axis('off')
plt.imshow(augmented_image)
#使用 Compose 增强管道:我们将使用各种增强功能,例如转置、模糊、失真等。
transform = A.Compose([
    A.RandomRotate90(),
    A.Transpose(),
    A.ShiftScaleRotate(shift_limit=0.08, scale_limit=0.5, rotate_limit=5, p=.8),
    A.Blur(blur_limit=7),
    A.GridDistortion(),
])
random.seed(2) 
augmented_image = transform(image=input_img)['image']
plt.figure(figsize=(4, 4))
plt.axis('off')
plt.imshow(augmented_image)

SOLT

SOLT 是一个深度学习数据增强库,支持图像、分割掩码、标签和关键点。

import solt
import solt.transforms as slt
h, w, c = input_img.shape
img = input_img[:w]
stream = solt.Stream([
    slt.Rotate(angle_range=(-90, 90), p=1, padding='r'),
    slt.Flip(axis=1, p=0.5),
    slt.Flip(axis=0, p=0.5),
    slt.Shear(range_x=0.3, range_y=0.8, p=0.5, padding='r'),
    slt.Scale(range_x=(0.8, 1.3), padding='r', range_y=(0.8, 1.3), same=False, p=0.5),
    slt.Pad((w, h), 'r'),
    slt.Crop((w, w), 'r'),
    slt.Blur(k_size=7, blur_type='m'),
    solt.SelectiveStream([
        slt.CutOut(40, p=1),
        slt.CutOut(50, p=1),
        slt.CutOut(10, p=1),
        solt.Stream(),
        solt.Stream(),
    ], n=3),
], ignore_fast_mode=True)
fig = plt.figure(figsize=(17,17))
n_augs = 10
random.seed(2)
for i in range(n_augs):
    img_aug = stream({'image': img}, return_torch=False, ).data[0].squeeze()
    ax = fig.add_subplot(1,n_augs,i+1)
    if i == 0:
        ax.imshow(img)
    else:
        ax.imshow(img_aug)
    ax.set_xticks([])
    ax.set_yticks([])
plt.show()
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值