Albumentations 在计算机视觉的图像增强中使用频率很高,无论是分类人物、语义分割人物、目标检测任务等等都很常用。但是由于变换方法较多,没有一个十分直观的可视化来观看变换以后的结果。
以下由 Featurize 的 Dave 同学整理出来方便大家查询。
import cv2
import albumentations as A
import matplotlib.pyplot as plt
img = cv2.cvtColor(cv2.imread('kitten_small.jpg'), cv2.COLOR_BGR2RGB)
plt.imshow(img);
Affine
将仿射变换应用于图像的增强。这主要是 OpenCV 中相应类和函数的包装。
f, ax = plt.subplots(1,2, figsize=(12, 12))
ax[0].imshow(img);
ax[1].imshow(A.Affine(p=1)(image=img)['image']);
ElasticTransform
图像的弹性变形,是基于https://gist.github.com/ernestum/601cdf56d2b424757de5 。
f, ax = plt.subplots(1,2, figsize=(12, 12))
ax[0].imshow(img);
ax[1].imshow(A.ElasticTransform(p=1)(image=img)['image']);
Flip
水平、垂直或同时翻转输入图像。
f, ax = plt.subplots(1,2, figsize=(12, 12))
ax[0].imshow(img);
ax[1].imshow(A.Flip(p=1)(image=img)['image']);
GridDistortion
对图像进行网格失真变换。
f, ax = plt.subplots(1,2, figsize=(12, 12))
ax[0].imshow(img);
ax[1].imshow(A.GridDistortion(p=1)(image=img)['image']);
HorizontalFlip
水平翻转输入图像。
f, ax = plt.subplots(1,2, figsize=(12, 12))
ax[0].imshow(img);
ax[1].imshow(A.HorizontalFlip(p=1)(image=img)['image']);
VerticalFlip
围绕 x 轴垂直翻转图像输入。
f, ax = plt.subplots(1,2, figsize=(12, 12))
ax[0].imshow(img);
ax[1].imshow(A.VerticalFlip(p=1)(image=img)['image']);
OpticalDistortion
对图像进行光学畸变转换。
f, ax = plt.subplots(1,2, figsize=(12, 12))
ax[0].imshow(img);
ax[1].imshow(A.OpticalDistortion(p=1)(image=img)['image']);
PadIfNeeded
如果图像尺寸小于该转换的指定尺寸,则填充图像的边缘至指定尺寸。
f, ax = plt.subplots(1,2, figsize=(12, 12))
ax[0].imshow(img);
ax[1].imshow(A.PadIfNeeded(min_height=256, min_width=256, border_mode=0, value=[255,255,255], p=1)(image=img)['image']);
Perspective
执行图像输入的随机四点透视变换。
f, ax = plt.subplots(1,2, figsize=(12, 12))
ax[0].imshow(img);
ax[1].imshow(A.Perspective(p=1)(image=img)['image']);
PiecewiseAffine
应用在本地邻域之间不同的仿射变换。
这种增强在图像上放置了一个规则的点网格,并通过仿射变换随机移动这些点的邻域。这会导致局部失真。
这主要是 scikit-image 的 PiecewiseAffine 的包装。另请参阅仿射以了解类似的技术。
注意:这个变换非常慢。尝试改用 ElasticTransformation,它至少快 10 倍。
f, ax = plt.subplots(1,2, figsize=(12, 12))
ax[0].imshow(img);
ax[1].imshow(A.PiecewiseAffine(p=1)(image=img)['image']);
ShiftScaleRotate
对图像输入进行随机应用仿射变换:平移、缩放和旋转。
f, ax = plt.subplots(1,2, figsize=(12, 12))
ax[0].imshow(img);
ax[1].imshow(A.ShiftScaleRotate(p=1, border_mode=0, value=[255,255,255])(image=img)['image']);
Transpose
对图像输入通过交换行和列来转置。
f, ax = plt.subplots(1,2, figsize=(12, 12))
ax[0].imshow(img);
ax[1].imshow(A.Transpose(p=1)(image=img)['image']);
RandomRotate90
将输入随机旋转 90 度零次或多次。
f, ax = plt.subplots(1,2, figsize=(12, 12))
ax[0].imshow(img);
ax[1].imshow(A.RandomRotate90(p=1)(image=img)['image']);
Rotate
对图像进行固定旋转。
f, ax = plt.subplots(1,2, figsize=(12, 12))
ax[0].imshow(img);
ax[1].imshow(A.Rotate(45, border_mode=0, value=[255,255,255], p=1)(image=img)['image']);
SafeRotate
将输入框内的输入旋转一个从均匀分布中随机选择的角度。
生成的图像中可能有伪影。旋转后,图像可能具有不同的纵横比,调整大小后,它会以图像的原始纵横比恢复其原始形状。由于这些原因,我们可能会看到一些伪影。
f, ax = plt.subplots(1,2, figsize=(12, 12))
ax[0].imshow(img);
ax[1].imshow(A.SafeRotate(45, border_mode=0, value=[255,255,255], p=1)(image=img)['image']);
CenterCrop
裁剪图像的中心部分。
f, ax = plt.subplots(1,2, figsize=(12, 12))
ax[0].imshow(img);
ax[1].imshow(A.CenterCrop(height=128, width=128, p=1)(image=img)['image']);
Crop
裁剪图像的固定部分。
f, ax = plt.subplots(1,2, figsize=(12, 12))
ax[0].imshow(img);
ax[1].imshow(A.Crop(x_min=0, y_min=0, x_max=140, y_max=156, p=1)(image=img)['image']);
CropAndPad
按像素数量或图像大小的比例裁剪并填充图像。
f, ax = plt.subplots(1,2, figsize=(12, 12))
ax[0].imshow(img);
ax[1].imshow(A.CropAndPad(percent=0.4, pad_cval=0, p=1)(image=img)['image']);
RandomCrop
对图像进行随机裁剪。
f, ax = plt.subplots(1,2, figsize=(12, 12))
ax[0].imshow(img);
ax[1].imshow(A.RandomCrop(height=128, width=128, p=1)(image=img)['image']);
RandomCropFromBorders
从边缘对图像进行随机裁剪。
f, ax = plt.subplots(1,2, figsize=(12, 12))
ax[0].imshow(img);
ax[1].imshow(A.RandomCropFromBorders(p=1)(image=img)['image']);
RandomResizedCrop
对图像进行随机裁剪以后再调整到指定大小。
f, ax = plt.subplots(1,2, figsize=(12, 12))
ax[0].imshow(img);
ax[1].imshow(A.RandomResizedCrop(256,256,p=1)(image=img)['image']);
RandomSizedCrop
裁剪输入图像的随机部分并将其重新缩放到某个大小。
f, ax = plt.subplots(1,2, figsize=(12, 12))
ax[0].imshow(img);
ax[1].imshow(A.RandomSizedCrop(min_max_height=[100, 160], height=256, width=256, p=1)(image=img)['image']);