当前位置:   article > 正文

图像增强库Albumentations使用总结

albumentations使用

目录

摘要

Albumentations 的 pip 安装

基准测试结果

Spatial-level transforms(空间层次转换)

支持的列表

简单的使用案例

详细使用案例

VerticalFlip 围绕X轴垂直翻转输入。

Blur模糊输入图像

HorizontalFlip 围绕y轴水平翻转输入

Flip水平,垂直或水平和垂直翻转输入。

Transpose, 通过交换行和列来转置输入。

RandomCrop 随机裁剪

 RandomGamma 随机灰度系数

RandomRotate90 将输入随机旋转90度,N次

ShiftScaleRotate 随机平移,缩放和旋转输入。

CenterCrop 裁剪图像的中心部分

GridDistortion网格失真

ElasticTransform 弹性变换

RandomGridShuffle把图像切成网格单元随机排列。

HueSaturationValue随机更改图像的颜色,饱和度和值。

PadIfNeeded 填充图像

RGBShift,对图像RGB的每个通道随机移动值。

GaussianBlur 使用随机核大小的高斯滤波器对图像进行模糊处理

CLAHE自适应直方图均衡

ChannelShuffle随机重新排列输入RGB图像的通道。

InvertImg反色

Cutout 随机擦除

RandomFog随机雾化

GridDropout网格擦除


摘要

albumentations包是一种针对数据增强专门写的API,里面基本包含大量的数据增强手段,其特点:

1Albumentations支持所有常见的计算机视觉任务,如分类、语义分割、实例分割、目标检测和姿态估计。

2、该库提供了一个简单统一的API,用于处理所有数据类型:图像(rbg图像、灰度图像、多光谱图像)、分割掩码、边界框和关键点。

3、该库包含70多种不同的增强功能,可以从现有数据中生成新的训练样本。

4Albumentations快。我们对每个新版本进行基准测试,以确保增强功能提供最大的速度。

5、它与流行的深度学习框架(PyTorchTensorFlow)一起工作。顺便说一下,AlbumentationsPyTorch生态系统的一部分。

6、由专家写的。作者既有生产计算机视觉系统的工作经验,也有参与竞争性机器学习的经验。许多核心团队成员是Kaggle MastersGrandmasters

7、该库广泛应用于工业、深度学习研究、机器学习竞赛和开源项目。

Albumentations 的 pip 安装

pip install albumentations

基准测试结果

测试使用ImageNet验证集的前2000张图像在Intel Xeon Gold 6140 CPU运行基准测试的结果。所有输出都被转换为带有np的连续NumPy数组。uint8数据类型。表格显示了在单个核上每秒可以处理的图像数量;高越好。

albumentations
0.5.0
imgaug
0.4.0
torchvision (Pillow-SIMD backend)
0.7.0
keras
2.4.3
augmentor
0.2.8
solt
0.1.9
HorizontalFlip99092821226787323016223
VerticalFlip437422181952433919683562
Rotate3712961632760345
ShiftScaleRotate63543714728--
Brightness275111784192294182300
Contrast27561213352-3482305
BrightnessContrast2738699195-1931179
ShiftRGB27571176-348--
ShiftHSV59728458--137
Gamma2844-382--946
Grayscale5159428709-10641273
RandomCrop64175886301852103-4177420732
PadToSize5123418-574--2874
Resize51210036341036-1016977
RandomSizedCrop_64_51231919391594-15292563
Posterize2778-----
Solarize2762-----
Equalize644413--735-
Multiply27271248----
MultiplyElementwise118209----
ColorJitter3687857---

Python and library versions: Python 3.8.6 (default, Oct 13 2020, 20:37:26) [GCC 8.3.0], numpy 1.19.2, pillow-simd 7.0.0.post3, opencv-python 4.4.0.44, scikit-image 0.17.2, scipy 1.5.2.

Spatial-level transforms(空间层次转换)

空间级转换将同时改变输入图像和附加目标,如掩模、边界框和关键点。下表显示了每个转换支持哪些附加目标。

TransformImageMasksBBoxesKeypoints
Affine
CenterCrop
CoarseDropout
Crop
CropAndPad
CropNonEmptyMaskIfExists
ElasticTransform
Flip
GridDistortion
GridDropout
HorizontalFlip
IAAAffine
IAAPiecewiseAffine
Lambda
LongestMaxSize
MaskDropout
NoOp
OpticalDistortion
PadIfNeeded
Perspective
RandomCrop
RandomCropNearBBox
RandomGridShuffle
RandomResizedCrop
RandomRotate90
RandomScale
RandomSizedBBoxSafeCrop
RandomSizedCrop
Resize
Rotate
SafeRotate
ShiftScaleRotate
SmallestMaxSize
Transpose
VerticalFlip

支持的列表

简单的使用案例

  1. import albumentations as A
  2. import cv2
  3. import matplotlib.pyplot as plt
  4. # Declare an augmentation pipeline
  5. transform = A.Compose([
  6. A.RandomCrop(width=512, height=512),
  7. A.HorizontalFlip(p=0.8),
  8. A.RandomBrightnessContrast(p=0.5),
  9. ])
  10. # Read an image with OpenCV and convert it to the RGB colorspace
  11. image = cv2.imread("aa.jpg")
  12. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  13. # Augment an image
  14. transformed = transform(image=image)
  15. transformed_image = transformed["image"]
  16. plt.imshow(transformed_image)
  17. plt.show()

原始图像:

运行结果:

 

详细使用案例

  • VerticalFlip 围绕X轴垂直翻转输入。

  1. import albumentations as A
  2. import cv2
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. #解决中文显示问题
  6. plt.rcParams['font.sans-serif']=['SimHei']
  7. plt.rcParams['axes.unicode_minus'] = False
  8. # Read an image with OpenCV and convert it to the RGB colorspace
  9. image = cv2.imread("aa.jpg")
  10. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  11. # Augment an image
  12. transformed = A.VerticalFlip(always_apply=False, p=1)(image=image)
  13. transformed_image = transformed["image"]
  14. plt.subplot(1, 2, 1)
  15. plt.title('原图') #第一幅图片标题
  16. plt.imshow(image)
  17. plt.subplot(1, 2, 2)
  18. plt.title('Blur后的图像')
  19. plt.imshow(transformed_image)
  20. plt.show()

  • Blur模糊输入图像

  1. import albumentations as A
  2. import cv2
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. #解决中文显示问题
  6. plt.rcParams['font.sans-serif']=['SimHei']
  7. plt.rcParams['axes.unicode_minus'] = False
  8. # Read an image with OpenCV and convert it to the RGB colorspace
  9. image = cv2.imread("aa.jpg")
  10. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  11. # Augment an image
  12. transformed = A.Blur(blur_limit=15,always_apply=False, p=1)(image=image)
  13. transformed_image = transformed["image"]
  14. plt.subplot(1, 2, 1)
  15. plt.title('原图') #第一幅图片标题
  16. plt.imshow(image)
  17. plt.subplot(1, 2, 2)
  18. plt.title('Blur后的图像')
  19. plt.imshow(transformed_image)
  20. plt.show()

  • HorizontalFlip 围绕y轴水平翻转输入

  1. import albumentations as A
  2. import cv2
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. #解决中文显示问题
  6. plt.rcParams['font.sans-serif']=['SimHei']
  7. plt.rcParams['axes.unicode_minus'] = False
  8. # Read an image with OpenCV and convert it to the RGB colorspace
  9. image = cv2.imread("aa.jpg")
  10. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  11. # Augment an image
  12. transformed = A.HorizontalFlip(always_apply=False, p=1)(image=image)
  13. transformed_image = transformed["image"]
  14. plt.subplot(1, 2, 1)
  15. plt.title('原图') #第一幅图片标题
  16. plt.imshow(image)
  17. plt.subplot(1, 2, 2)
  18. plt.title('HorizontalFlip后的图像')
  19. plt.imshow(transformed_image)
  20. plt.show()

运行结果: 

  • Flip水平,垂直或水平和垂直翻转输入。

  1. import albumentations as A
  2. import cv2
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. #解决中文显示问题
  6. plt.rcParams['font.sans-serif']=['SimHei']
  7. plt.rcParams['axes.unicode_minus'] = False
  8. # Read an image with OpenCV and convert it to the RGB colorspace
  9. image = cv2.imread("aa.jpg")
  10. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  11. # Augment an image
  12. transformed = A.Flip(always_apply=False, p=1)(image=image)
  13. transformed_image = transformed["image"]
  14. plt.subplot(1, 2, 1)
  15. plt.title('原图') #第一幅图片标题
  16. plt.imshow(image)
  17. plt.subplot(1, 2, 2)
  18. plt.title('Flip后的图像')
  19. plt.imshow(transformed_image)
  20. plt.show()

运行结果有一定的随机性,如下图:

  • Transpose, 通过交换行和列来转置输入。

  1. import albumentations as A
  2. import cv2
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. #解决中文显示问题
  6. plt.rcParams['font.sans-serif']=['SimHei']
  7. plt.rcParams['axes.unicode_minus'] = False
  8. # Read an image with OpenCV and convert it to the RGB colorspace
  9. image = cv2.imread("aa.jpg")
  10. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  11. # Augment an image
  12. transformed = A.Transpose(always_apply=False, p=1)(image=image)
  13. transformed_image = transformed["image"]
  14. plt.subplot(1, 2, 1)
  15. plt.title('原图') #第一幅图片标题
  16. plt.imshow(image)
  17. plt.subplot(1, 2, 2)
  18. plt.title('Transpose后的图像')
  19. plt.imshow(transformed_image)
  20. plt.show()

 运行结果:

  • RandomCrop 随机裁剪

  1. import albumentations as A
  2. import cv2
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. #解决中文显示问题
  6. plt.rcParams['font.sans-serif']=['SimHei']
  7. plt.rcParams['axes.unicode_minus'] = False
  8. # Read an image with OpenCV and convert it to the RGB colorspace
  9. image = cv2.imread("aa.jpg")
  10. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  11. # Augment an image
  12. transformed = A.RandomCrop(512, 512,always_apply=False, p=1)(image=image)
  13. transformed_image = transformed["image"]
  14. plt.subplot(1, 2, 1)
  15. plt.title('原图') #第一幅图片标题
  16. plt.imshow(image)
  17. plt.subplot(1, 2, 2)
  18. plt.title('RandomCrop后的图像')
  19. plt.imshow(transformed_image)
  20. plt.show()

运行结果:

  •  RandomGamma 随机灰度系数

  1. import albumentations as A
  2. import cv2
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. #解决中文显示问题
  6. plt.rcParams['font.sans-serif']=['SimHei']
  7. plt.rcParams['axes.unicode_minus'] = False
  8. # Read an image with OpenCV and convert it to the RGB colorspace
  9. image = cv2.imread("aa.jpg")
  10. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  11. # Augment an image
  12. transformed = A.RandomGamma(gamma_limit=(20, 20), eps=None, always_apply=False, p=1)(image=image)
  13. transformed_image = transformed["image"]
  14. plt.subplot(1, 2, 1)
  15. plt.title('原图') #第一幅图片标题
  16. plt.imshow(image)
  17. plt.subplot(1, 2, 2)
  18. plt.title('RandomGamma后的图像')
  19. plt.imshow(transformed_image)
  20. plt.show()

运行结果: 

  • RandomRotate90 将输入随机旋转90度,N次

  1. import albumentations as A
  2. import cv2
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. #解决中文显示问题
  6. plt.rcParams['font.sans-serif']=['SimHei']
  7. plt.rcParams['axes.unicode_minus'] = False
  8. # Read an image with OpenCV and convert it to the RGB colorspace
  9. image = cv2.imread("aa.jpg")
  10. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  11. # Augment an image
  12. transformed = A.RandomRotate90(always_apply=False, p=1)(image=image)
  13. transformed_image = transformed["image"]
  14. plt.subplot(1, 2, 1)
  15. plt.title('原图') #第一幅图片标题
  16. plt.imshow(image)
  17. plt.subplot(1, 2, 2)
  18. plt.title('RandomRotate90后的图像')
  19. plt.imshow(transformed_image)
  20. plt.show()

运行结果: 

  • ShiftScaleRotate 随机平移,缩放和旋转输入。

  1. import albumentations as A
  2. import cv2
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. #解决中文显示问题
  6. plt.rcParams['font.sans-serif']=['SimHei']
  7. plt.rcParams['axes.unicode_minus'] = False
  8. # Read an image with OpenCV and convert it to the RGB colorspace
  9. image = cv2.imread("aa.jpg")
  10. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  11. # Augment an image
  12. transformed = A.ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.1, rotate_limit=45, interpolation=1, border_mode=4, value=None, mask_value=None, always_apply=False, p=1)(image=image)
  13. transformed_image = transformed["image"]
  14. plt.subplot(1, 2, 1)
  15. plt.title('原图') #第一幅图片标题
  16. plt.imshow(image)
  17. plt.subplot(1, 2, 2)
  18. plt.title('ShiftScaleRotate后的图像')
  19. plt.imshow(transformed_image)
  20. plt.show()

运行结果:

  • CenterCrop 裁剪图像的中心部分

  1. import albumentations as A
  2. import cv2
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. # 解决中文显示问题
  6. plt.rcParams['font.sans-serif'] = ['SimHei']
  7. plt.rcParams['axes.unicode_minus'] = False
  8. # Read an image with OpenCV and convert it to the RGB colorspace
  9. image = cv2.imread("aa.jpg")
  10. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  11. # Augment an image
  12. transformed = A.CenterCrop(256, 256, always_apply=False, p=1)(image=image)
  13. transformed_image = transformed["image"]
  14. plt.subplot(1, 2, 1)
  15. plt.title('原图') # 第一幅图片标题
  16. plt.imshow(image)
  17. plt.subplot(1, 2, 2)
  18. plt.title("CenterCrop后的图像")
  19. plt.imshow(transformed_image)
  20. plt.show()

  • GridDistortion网格失真

  1. import albumentations as A
  2. import cv2
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. # 解决中文显示问题
  6. plt.rcParams['font.sans-serif'] = ['SimHei']
  7. plt.rcParams['axes.unicode_minus'] = False
  8. # Read an image with OpenCV and convert it to the RGB colorspace
  9. image = cv2.imread("aa.jpg")
  10. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  11. # Augment an image
  12. transformed = A.GridDistortion(num_steps=10, distort_limit=0.3,border_mode=4, always_apply=False, p=1)(image=image)
  13. transformed_image = transformed["image"]
  14. plt.subplot(1, 2, 1)
  15. plt.title('原图') # 第一幅图片标题
  16. plt.imshow(image)
  17. plt.subplot(1, 2, 2)
  18. plt.title("GridDistortion后的图像")
  19. plt.imshow(transformed_image)
  20. plt.show()

运行结果: 

  • ElasticTransform 弹性变换

    1. import albumentations as A
    2. import cv2
    3. import numpy as np
    4. import matplotlib.pyplot as plt
    5. # 解决中文显示问题
    6. plt.rcParams['font.sans-serif'] = ['SimHei']
    7. plt.rcParams['axes.unicode_minus'] = False
    8. # Read an image with OpenCV and convert it to the RGB colorspace
    9. image = cv2.imread("aa.jpg")
    10. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    11. # Augment an image
    12. transformed = A.ElasticTransform(alpha=5, sigma=50, alpha_affine=50, interpolation=1, border_mode=4,always_apply=False, p=1)(image=image)
    13. transformed_image = transformed["image"]
    14. plt.subplot(1, 2, 1)
    15. plt.title('原图') # 第一幅图片标题
    16. plt.imshow(image)
    17. plt.subplot(1, 2, 2)
    18. plt.title("ElasticTransform后的图像")
    19. plt.imshow(transformed_image)
    20. plt.show()

    运行结果:

  • RandomGridShuffle把图像切成网格单元随机排列。

    1. import albumentations as A
    2. import cv2
    3. import numpy as np
    4. import matplotlib.pyplot as plt
    5. # 解决中文显示问题
    6. plt.rcParams['font.sans-serif'] = ['SimHei']
    7. plt.rcParams['axes.unicode_minus'] = False
    8. # Read an image with OpenCV and convert it to the RGB colorspace
    9. image = cv2.imread("aa.jpg")
    10. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    11. # Augment an image
    12. transformed = A.RandomGridShuffle(grid=(3, 3), always_apply=False, p=1) (image=image)
    13. transformed_image = transformed["image"]
    14. plt.subplot(1, 2, 1)
    15. plt.title('原图') # 第一幅图片标题
    16. plt.imshow(image)
    17. plt.subplot(1, 2, 2)
    18. plt.title("RandomGridShuffle后的图像")
    19. plt.imshow(transformed_image)
    20. plt.show()

    运行结果:

  • HueSaturationValue随机更改图像的颜色,饱和度和值。

  1. import albumentations as A
  2. import cv2
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. # 解决中文显示问题
  6. plt.rcParams['font.sans-serif'] = ['SimHei']
  7. plt.rcParams['axes.unicode_minus'] = False
  8. # Read an image with OpenCV and convert it to the RGB colorspace
  9. image = cv2.imread("aa.jpg")
  10. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  11. # Augment an image
  12. transformed = A.HueSaturationValue(hue_shift_limit=20, sat_shift_limit=30, val_shift_limit=20, always_apply=False, p=1)(image=image)
  13. transformed_image = transformed["image"]
  14. plt.subplot(1, 2, 1)
  15. plt.title('原图') # 第一幅图片标题
  16. plt.imshow(image)
  17. plt.subplot(1, 2, 2)
  18. plt.title("HueSaturationValue后的图像")
  19. plt.imshow(transformed_image)
  20. plt.show()

运行结果:

 

  • PadIfNeeded 填充图像

  1. import albumentations as A
  2. import cv2
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. # 解决中文显示问题
  6. plt.rcParams['font.sans-serif'] = ['SimHei']
  7. plt.rcParams['axes.unicode_minus'] = False
  8. # Read an image with OpenCV and convert it to the RGB colorspace
  9. image = cv2.imread("aa.jpg")
  10. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  11. # Augment an image
  12. transformed = A.PadIfNeeded(min_height=2048, min_width=2048, border_mode=4, always_apply=False, p=1)(image=image)
  13. transformed_image = transformed["image"]
  14. plt.subplot(1, 2, 1)
  15. plt.title('原图') # 第一幅图片标题
  16. plt.imshow(image)
  17. plt.subplot(1, 2, 2)
  18. plt.title("PadIfNeeded后的图像")
  19. plt.imshow(transformed_image)
  20. plt.show()

运行结果:

  • RGBShift,对图像RGB的每个通道随机移动值。

  1. import albumentations as A
  2. import cv2
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. # 解决中文显示问题
  6. plt.rcParams['font.sans-serif'] = ['SimHei']
  7. plt.rcParams['axes.unicode_minus'] = False
  8. # Read an image with OpenCV and convert it to the RGB colorspace
  9. image = cv2.imread("aa.jpg")
  10. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  11. # Augment an image
  12. transformed = A.RGBShift(r_shift_limit=10, g_shift_limit=20, b_shift_limit=20, always_apply=False, p=1)(image=image)
  13. transformed_image = transformed["image"]
  14. plt.subplot(1, 2, 1)
  15. plt.title('原图') # 第一幅图片标题
  16. plt.imshow(image)
  17. plt.subplot(1, 2, 2)
  18. plt.title("RGBShift后的图像")
  19. plt.imshow(transformed_image)
  20. plt.show()

  • GaussianBlur 使用随机核大小的高斯滤波器对图像进行模糊处理

  1. import albumentations as A
  2. import cv2
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. # 解决中文显示问题
  6. plt.rcParams['font.sans-serif'] = ['SimHei']
  7. plt.rcParams['axes.unicode_minus'] = False
  8. # Read an image with OpenCV and convert it to the RGB colorspace
  9. image = cv2.imread("aa.jpg")
  10. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  11. # Augment an image
  12. transformed = A.GaussianBlur(blur_limit=11, always_apply=False, p=1)(image=image)
  13. transformed_image = transformed["image"]
  14. plt.subplot(1, 2, 1)
  15. plt.title('原图') # 第一幅图片标题
  16. plt.imshow(image)
  17. plt.subplot(1, 2, 2)
  18. plt.title("GaussianBlur后的图像")
  19. plt.imshow(transformed_image)
  20. plt.show()

 运行结果:

  • CLAHE自适应直方图均衡

  1. import albumentations as A
  2. import cv2
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. # 解决中文显示问题
  6. plt.rcParams['font.sans-serif'] = ['SimHei']
  7. plt.rcParams['axes.unicode_minus'] = False
  8. # Read an image with OpenCV and convert it to the RGB colorspace
  9. image = cv2.imread("aa.jpg")
  10. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  11. # Augment an image
  12. transformed = A.CLAHE(clip_limit=4.0, tile_grid_size=(8, 8), always_apply=False, p=0.5)(image=image)
  13. transformed_image = transformed["image"]
  14. plt.subplot(1, 2, 1)
  15. plt.title('原图') # 第一幅图片标题
  16. plt.imshow(image)
  17. plt.subplot(1, 2, 2)
  18. plt.title("CLAHE后的图像")
  19. plt.imshow(transformed_image)
  20. plt.show()

运行结果:

  • ChannelShuffle随机重新排列输入RGB图像的通道。

  1. import albumentations as A
  2. import cv2
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. # 解决中文显示问题
  6. plt.rcParams['font.sans-serif'] = ['SimHei']
  7. plt.rcParams['axes.unicode_minus'] = False
  8. # Read an image with OpenCV and convert it to the RGB colorspace
  9. image = cv2.imread("aa.jpg")
  10. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  11. # Augment an image
  12. transformed = A.ChannelShuffle(always_apply=False, p=0.5)(image=image)
  13. transformed_image = transformed["image"]
  14. plt.subplot(1, 2, 1)
  15. plt.title('原图') # 第一幅图片标题
  16. plt.imshow(image)
  17. plt.subplot(1, 2, 2)
  18. plt.title("ChannelShuffle后的图像")
  19. plt.imshow(transformed_image)
  20. plt.show()

运行结果:

 

  • InvertImg反色

  1. import albumentations as A
  2. import cv2
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. # 解决中文显示问题
  6. plt.rcParams['font.sans-serif'] = ['SimHei']
  7. plt.rcParams['axes.unicode_minus'] = False
  8. # Read an image with OpenCV and convert it to the RGB colorspace
  9. image = cv2.imread("aa.jpg")
  10. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  11. # Augment an image
  12. transformed = A.InvertImg(always_apply=False, p=0.5)(image=image)
  13. transformed_image = transformed["image"]
  14. plt.subplot(1, 2, 1)
  15. plt.title('原图') # 第一幅图片标题
  16. plt.imshow(image)
  17. plt.subplot(1, 2, 2)
  18. plt.title("InvertImg后的图像")
  19. plt.imshow(transformed_image)
  20. plt.show()

  • Cutout 随机擦除

  1. import albumentations as A
  2. import cv2
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. # 解决中文显示问题
  6. plt.rcParams['font.sans-serif'] = ['SimHei']
  7. plt.rcParams['axes.unicode_minus'] = False
  8. # Read an image with OpenCV and convert it to the RGB colorspace
  9. image = cv2.imread("aa.jpg")
  10. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  11. # Augment an image
  12. transformed = A.Cutout(num_holes=20, max_h_size=20, max_w_size=20, fill_value=0, always_apply=False, p=1)(image=image)
  13. transformed_image = transformed["image"]
  14. plt.subplot(1, 2, 1)
  15. plt.title('原图') # 第一幅图片标题
  16. plt.imshow(image)
  17. plt.subplot(1, 2, 2)
  18. plt.title("Cutout后的图像")
  19. plt.imshow(transformed_image)
  20. plt.show()

运行结果:

  • RandomFog随机雾化

  1. import albumentations as A
  2. import cv2
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. # 解决中文显示问题
  6. plt.rcParams['font.sans-serif'] = ['SimHei']
  7. plt.rcParams['axes.unicode_minus'] = False
  8. # Read an image with OpenCV and convert it to the RGB colorspace
  9. image = cv2.imread("aa.jpg")
  10. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  11. # Augment an image
  12. transformed = A.RandomFog(fog_coef_lower=0.3, fog_coef_upper=1, alpha_coef=0.08, always_apply=False, p=1)(image=image)
  13. transformed_image = transformed["image"]
  14. plt.subplot(1, 2, 1)
  15. plt.title('原图') # 第一幅图片标题
  16. plt.imshow(image)
  17. plt.subplot(1, 2, 2)
  18. plt.title("RandomFog后的图像")
  19. plt.imshow(transformed_image)
  20. plt.show()

运行结果:

 

  • GridDropout网格擦除

  1. import albumentations as A
  2. import cv2
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. # 解决中文显示问题
  6. plt.rcParams['font.sans-serif'] = ['SimHei']
  7. plt.rcParams['axes.unicode_minus'] = False
  8. # Read an image with OpenCV and convert it to the RGB colorspace
  9. image = cv2.imread("aa.jpg")
  10. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  11. # Augment an image
  12. transformed = A.GridDropout(ratio=0.5, unit_size_min=None, unit_size_max=None, holes_number_x=None, holes_number_y=None,
  13. shift_x=0, shift_y=0, always_apply=False, p=0.5)(image=image)
  14. transformed_image = transformed["image"]
  15. plt.subplot(1, 2, 1)
  16. plt.title('原图') # 第一幅图片标题
  17. plt.imshow(image)
  18. plt.subplot(1, 2, 2)
  19. plt.title("GridDropout后的图像")
  20. plt.imshow(transformed_image)
  21. plt.show()

运行结果:

组合变换(Compose)

变换不仅可以单独使用,还可以将这些组合起来,这就需要用到 Compose 类,该类继承自 BaseComposeCompose 类含有以下参数:

  • transforms:转换类的数组,list类型
  • bbox_params:用于 bounding boxes 转换的参数,BboxPoarams 类型
  • keypoint_params:用于 keypoints 转换的参数, KeypointParams 类型
  • additional_targets:key新target 名字,value 为旧 target 名字的 dict,如 {'image2': 'image'},dict 类型
  • p:使用这些变换的概率,默认值为 1.0
    1. image3 = Compose([
    2. # 对比度受限直方图均衡
    3. #(Contrast Limited Adaptive Histogram Equalization)
    4. CLAHE(),
    5. # 随机旋转 90°
    6. RandomRotate90(),
    7. # 转置
    8. Transpose(),
    9. # 随机仿射变换
    10. ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.50, rotate_limit=45, p=.75),
    11. # 模糊
    12. Blur(blur_limit=3),
    13. # 光学畸变
    14. OpticalDistortion(),
    15. # 网格畸变
    16. GridDistortion(),
    17. # 随机改变图片的 HUE、饱和度和值
    18. HueSaturationValue()
    19. ], p=1.0)(image=image)['image']

    随机选择(OneOf)

  • 它同Compose一样,都是做组合的,都有概率。区别就在于:Compose组合下的变换是要挨着顺序做的,而OneOf组合里面的变换是系统自动选择其中一个来做,而这里的概率参数p是指选定后的变换被做的概率。例:
    1. image4 = Compose([
    2. RandomRotate90(),
    3. # 翻转
    4. Flip(),
    5. Transpose(),
    6. OneOf([
    7. # 高斯噪点
    8. IAAAdditiveGaussianNoise(),
    9. GaussNoise(),
    10. ], p=0.2),
    11. OneOf([
    12. # 模糊相关操作
    13. MotionBlur(p=.2),
    14. MedianBlur(blur_limit=3, p=0.1),
    15. Blur(blur_limit=3, p=0.1),
    16. ], p=0.2),
    17. ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.2, rotate_limit=45, p=0.2),
    18. OneOf([
    19. # 畸变相关操作
    20. OpticalDistortion(p=0.3),
    21. GridDistortion(p=.1),
    22. IAAPiecewiseAffine(p=0.3),
    23. ], p=0.2),
    24. OneOf([
    25. # 锐化、浮雕等操作
    26. CLAHE(clip_limit=2),
    27. IAASharpen(),
    28. IAAEmboss(),
    29. RandomBrightnessContrast(),
    30. ], p=0.3),
    31. HueSaturationValue(p=0.3),
    32. ], p=1.0)(image=image)['image']

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/211711
推荐阅读
相关标签
  

闽ICP备14008679号