zoukankan      html  css  js  c++  java
  • 图像增强工具 albumentations学习总结

    图像增强工具 albumentations学习总结

    CONTENT

    data augmentations link description
    CenterCrop 查看结果 中心剪裁
    Crop 查看结果 指定位置剪裁
    CropNonEmptyMaskIfExists 查看结果 如果掩码为非空,则使用掩码裁剪区域,否则随机裁剪。
    ElasticTransform 查看结果 Best Practices for Convolutional Neural Networks applied to Visual Document
    Flip 查看结果 水平,垂直或水平和垂直翻转
    GridDistortion 查看结果 albumentations 中主要提供了三种非刚体变换方法:ElasticTransform、GridDistortion 和 OpticalDistortion。
    GridDropout 查看结果 以网格方式删除图像的矩形区域
    HorizontalFlip 查看结果 水平翻转
    IAAAffine 查看结果 在输入上放置规则的点网格,并通过仿射变换在这些点的附近随机移动
    IAACropAndPad 查看结果 剪裁和填充
    IAAFliplr 查看结果 左右翻转
    IAAFlipud 查看结果 上下翻转
    IAAPerspective 查看结果 对输入执行随机四点透视变换
    IAAPiecewiseAffine 查看结果 在输入端放置一个规则的点网格,并通过仿射变换随机移动这些点的邻域
    Lambda 查看结果 用户自定义图像增强
    LongestMaxSize 查看结果 如果图像最长边小于max_size, 将最长变为max_size, 并保留长宽比resize
    MaskDropout 查看结果
    OpticalDistortion 查看结果 畸变
    PadIfNeeded 查看结果 判断填充
    RandomCrop 查看结果 随机剪裁
    RandomCropNearBBox 查看结果
    RandomGridShuffle 查看结果 网格打乱图像
    RandomResizedCrop 查看结果 剪裁并resize
    RandomRotate90 查看结果 随机旋转90度
    RandomScale 查看结果 随机尺度变换
    RandomSizedBBoxSafeCrop 查看结果
    RandomSizedCrop 查看结果 随机剪裁
    Resize 查看结果 重新调整图像大小
    Rotate 查看结果 旋转
    ShiftScaleRotate 查看结果 平移、尺度加旋转变换
    SmallestMaxSize 查看结果 将短边变为maxsize, 并保持长宽比
    Transpose 查看结果 转置
    VerticalFlip 查看结果 垂直翻转

    工具函数

    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')
    

    回到顶部

    24. RandomRotate90

    回到顶部

    25. RandomScale

    回到顶部

    26. RandomSizedBBoxSafeCrop

    回到顶部

    27. RandomSizedCrop

    回到顶部

    28. Resize

    回到顶部

    29. Resize

    回到顶部

    30. Rotate

    回到顶部

    31. ShiftScaleRotate

    回到顶部

    32. SmallestMaxSize

    回到顶部

    33. Transpose

    回到顶部

    34. VerticalFlip

    回到顶部

  • 相关阅读:
    Java精选笔记_JSP技术
    Java精选笔记_JavaBean
    Java精选笔记_JSP开发模型
    Java精选笔记_XML基础
    Git_多人协作
    Git_Feature分支
    Git_Bug分支
    Git_分支管理策略
    Git_解决冲突
    Git_创建与合并分支
  • 原文地址:https://www.cnblogs.com/54hys/p/12694084.html
Copyright © 2011-2022 走看看