图像增强工具 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
4. ElasticTransform
- 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()
# print(aug_func)
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')