图像增强工具 albumentations
学习总结
CONTENT
工具函数
import numpy as np
import cv2
import matplotlib.pyplot as plt
import albumentations as albu
import os,sys
'''
data augmentation util: albumentations
reference: https://github.com/albumentations-team/albumentations#documentation
'''
def aug(img, aug_func):
return aug_func(**{'image':img})['image']
def aug_compose(img, aug_func_list):
strong_aug = albu.Compose(aug_func_list, p=1)
return strong_aug(**{'image':img})['image']
def aug_show(img, aug_func, save_fig_name):
plt.figure(figsize=(16,9))
for i in range(8):
plt.subplot(2, 4, i+1)
img_aug = aug(img.copy(), aug_func)
plt.imshow(img_aug)
os.chdir(os.path.join(cur_root,'pics'))
plt.savefig(f'{save_fig_name}.png', dpi=120)
plt.show()
原图

1. CenterCrop
def center_crop(img):
height, width = img.shape[:2]
plt.figure(figsize=(16,9))
for i in range(8):
plt.subplot(4, 2, i+1)
crop_height, crop_width = np.random.randint(100, height), np.random.randint(100, width)
print(crop_height, crop_width)
img_crop = aug(img.copy(), albu.CenterCrop(crop_height, crop_width,p=1))
plt.imshow(img_crop)
plt.show()
plt.savefig('center_crop.png', dpi=300)

回到顶部
2. Crop
def crop(img):
height, width = img.shape[:2]
plt.figure(figsize=(16,9))
for i in range(8):
plt.subplot(2, 4, i+1)
x_min, y_min = np.random.randint(24, 120), np.random.randint(16, 80)
x_max, y_max = np.random.randint(x_min, width), np.random.randint(y_min, height)
img_crop = aug(img.copy(), albu.Crop(x_min, y_min, x_max, y_max, p=1.0))
plt.imshow(img_crop)
os.chdir(os.path.join(cur_root,'pics'))
plt.savefig('crop.png', dpi=120)
plt.show()

回到顶部
3. CropNonEmptyMaskIfExists
回到顶部
- alpha、sigma:高斯过滤参数,float类型
- alpha_affine:范围为 (-alpha_affine, alpha_affine),float 类型
- interpolation、border_mode、value、mask_value:与其他类含义一样
- approximate:是否应平滑具有固定大小核的替换映射(displacement map),若启用此选项,在大图上会有两倍的速度提升,boolean类型。
- p:使用此转换的概率,默认值为 0.5
(1) 首先需要对图像中的每个像素点(x,y)产生两个-1~1之间的随机数,Δx(x,y)和Δy(x,y),分别表示该像素点的x方向和y方向的移动距离;
(2) 生成一个以0为均值,以σ为标准差的高斯核k_nn,并用前面的随机数与之做卷积,并将结果作用于原图像
一般来说,alpha越小,sigma越大,产生的偏差越小,和原图越接近。
参考链接
aug_show(img, albu.ElasticTransform(alpha=1, sigma=50, alpha_affine=50), 'elastic_transform')

回到顶部
5. Flip
aug_show(img, albu.Flip(p=0.5), 'flip')

回到顶部
6. GridDistortion
- num_steps:在每一条边上网格单元的数量,默认值为 5,int 类型
- distort_limit:如果是单值,那么会被转成 (-distort_limit, distort_limit),默认值为 (-0.03, 0.03),float或float数组类型
- interpolation、border_mode、value、mask_value:与其他类含义一样
- p:使用此转换的概率,默认值为 0.5
aug_show(img, albu.GridDistortion(p=1, border_mode = cv2.BORDER_CONSTANT), 'grid_distortion')

回到顶部
7. GridDropout
aug_show(img, albu.GridDropout(), 'grid_dropout')

回到顶部
8. HorizontalFlip
aug_show(img, albu.HorizontalFlip(), 'horizontal_flip')

回到顶部
9. IAAAffine
aug_show(img, albu.IAAAffine(p=1, scale=0.8, translate_percent=0.8, rotate=30), 'iaa_affine')

回到顶部
10. IAACropAndPad
def iaa_crop_and_pad(img):
plt.figure(figsize=(16,9))
for i in range(8):
plt.subplot(2, 4, i+1)
img_crop = aug(img.copy(), albu.IAACropAndPad(p=1, percent= np.random.randint(1, 20)/100))
plt.imshow(img_crop)
os.chdir(os.path.join(cur_root,'pics'))
plt.savefig('iaa_crop_and_pad.png', dpi=120)
plt.show()

回到顶部
11. IAAFliplr
aug_show(img, albu.IAAFliplr(), 'iaa_fliplr')

回到顶部
12. IAAFlipud
aug_show(img, albu.IAAFlipud(), 'iaa_flipud')

回到顶部
13. IAAPerspective
aug_show(img, albu.IAAPerspective(), 'iaa_perspective')

回到顶部
14. IAAPiecewiseAffine
aug_show(img, albu.IAAPiecewiseAffine(), 'iaa_piecewise_affine')

回到顶部
15. Lambda
def random_brightness_or_gamma(image, **kwargs):
seed = np.random.randint(0,2)
aug_func = albu.RandomBrightness() if seed else albu.RandomGamma()
return aug_func(**{'image': image})['image']
aug_show(img, albu.Lambda(image=random_brightness), 'lambda')

回到顶部
16. LongestMaxSize
aug_show(img, albu.LongestMaxSize(max_size=1024), 'longest_max_size')

回到顶部
17. MaskDropout
回到顶部
18. OpticalDistortion
aug_show(img, albu.OpticalDistortion(distort_limit=0.25, shift_limit=0.25, border_mode= cv2.BORDER_CONSTANT, p=1), 'optical_distortion')

回到顶部
19. PadIfNeeded
aug_show(img, albu.PadIfNeeded(min_height=300, min_width=400, border_mode= cv2.BORDER_CONSTANT, p=1), 'pad_if_needed')

回到顶部
20. RandomCrop
aug_show(img, albu.RandomCrop(height=120, width=160, p=1), 'random_crop')

回到顶部
21. RandomCropNearBBox
回到顶部
22. RandomGridShuffle
aug_show(img, albu.RandomGridShuffle(grid=(4,4), p=1), 'random_grid_shuffle')

回到顶部
23. RandomResizedCrop
aug_show(img, albu.RandomResizedCrop(height=240, width=320, scale=(0.3, 1.0), ratio=(0.75, 1.25)), 'random_resized_crop')

回到顶部
24. RandomRotate90

回到顶部
25. RandomScale

回到顶部
26. RandomSizedBBoxSafeCrop
回到顶部
27. RandomSizedCrop

回到顶部
28. Resize

回到顶部
29. Resize

回到顶部
30. Rotate


回到顶部
31. ShiftScaleRotate

回到顶部
32. SmallestMaxSize

回到顶部
33. Transpose

回到顶部
34. VerticalFlip

回到顶部
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)