# albumentationsの関数を確かめる

まずは画像を読み込む。

``````import cv2
import matplotlib.pyplot as plt
import albumentations as A

``````

たとえばA.CoarseDropoutがどういう機能か確かめたいなら、

``````plt.figure(figsize=(20, 3))

for i in range(20):
_img =  A.CoarseDropout(max_holes=16, max_height=32, max_width=32, p=1)(image=img)["image"]
plt.subplot(2, 10, i + 1);
plt.imshow(_img)
``````

ShiftScaleRotateを確かめるなら、

``````plt.figure(figsize=(20, 3))

for i in range(20):
_img =  A.ShiftScaleRotate(p=1.0)(image=img)["image"]
plt.subplot(2, 10, i + 1);
plt.imshow(_img)
``````

RandomBlightnessContrastなら

``````plt.figure(figsize=(20, 3))

for i in range(20):
_img =  A.RandomBrightnessContrast(brightness_limit = (-0.2,0.2), contrast_limit = (-0.1,0.1), p=1)(image=img)["image"]
plt.subplot(2, 10, i + 1);
plt.imshow(_img)
``````

# albumentationを自作

このサイトがわかりやすかった

https://nonbiri-tereka.hatenablog.com/entry/2020/05/18/090641

たとえば画像の左上、右上、左下、右下をクリップするのは、

``````from albumentations.core.transforms_interface import ImageOnlyTransform

class CornerCrop(ImageOnlyTransform):
def __init__(self, always_apply=False, p=0.5, height=128, width=128, mode="left_top"):
super(CornerCrop, self).__init__(always_apply=always_apply, p=p)
self.mode = mode
self.h = height
self.w = width

def apply(self, img, **params):

h, w, c = img.shape

if (self.h >= h) or (self.w >= w):
return img

if self.mode == "left_top":
img = img[:self.h, :self.w]
elif self.mode == "right_top":
img = img[:self.h, -self.w:]
elif self.mode == "left_bottom":
img = img[-self.h:, :self.w]
elif self.mode == "right_bottom":
img = img[-self.h:, -self.w:]

return img
``````
``````plt.figure(figsize=(20, 3))

size = 384

_img = CornerCrop(height=size, width=size, mode="left_top", p=1.0)(image=img)["image"]
plt.subplot(1, 4, 1); plt.imshow(_img)

_img = CornerCrop(height=size, width=size, mode="right_top", p=1.0)(image=img)["image"]
plt.subplot(1, 4, 2); plt.imshow(_img)

_img = CornerCrop(height=size, width=size, mode="left_bottom", p=1.0)(image=img)["image"]
plt.subplot(1, 4, 3); plt.imshow(_img)

_img = CornerCrop(height=size, width=size, mode="right_bottom", p=1.0)(image=img)["image"]
plt.subplot(1, 4, 4); plt.imshow(_img)

plt.show()
``````