圖像增強工具 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

回到頂部


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM