代码及效果如下:
先导包
import cv2
import albumentations
from albumentations.pytorch.transforms import ToTensorV2
import matplotlib.pyplot as plt
原图:
image = cv2.imread('./cat3.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(5, 5))
plt.imshow(image)

水平翻转
image = cv2.imread('./cat3.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image1 = albumentations.HorizontalFlip(always_apply=True ,p=1.0)(image=image)['image']
plt.figure(figsize=(5, 5))
plt.imshow(image1)

变换尺寸
image = cv2.imread('./cat3.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
print(image.shape)
image = albumentations.Resize(320, 320)(image=image)["image"]
print(image.shape)
plt.figure(figsize=(5, 5))
plt.imshow(image)

垂直翻转
image = cv2.imread('./cat3.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = albumentations.VerticalFlip(p=1.0)(image=image)["image"]
plt.figure(figsize=(5, 5))
plt.imshow(image)

旋转
image = cv2.imread('./cat3.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = albumentations.Rotate(limit=180, p=1)(image=image)["image"]
plt.figure(figsize=(5, 5))
plt.imshow(image)

RandomBrightnessContrast
image = cv2.imread('./cat3.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = albumentations.RandomBrightnessContrast(brightness_limit=0.8, contrast_limit=.2, brightness_by_max=True,p=1)(image=image)["image"]
plt.figure(figsize=(5, 5))
plt.imshow(image)

ShiftScaleRotate
image = cv2.imread('./cat3.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = albumentations.ShiftScaleRotate(
shift_limit=0.0625, scale_limit=0.1, rotate_limit=45, p=1.0
)(image=image)["image"]
plt.figure(figsize=(5, 5))
plt.imshow(image)

Normalize
image = cv2.imread('./cat3.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = albumentations.Normalize(
[0.485, 0.456, 0.406], [0.229, 0.224, 0.225],
max_pixel_value=255.0, always_apply=True
)(image=image)["image"]
plt.figure(figsize=(5, 5))
plt.imshow(image)

ToTensorV2
image = cv2.imread('./cat3.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
print(type(image))
print(image.shape)
image = ToTensorV2()(image=image)["image"]
print(image.shape)

RandomRotate90
image = cv2.imread('./cat3.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = albumentations.RandomRotate90(p=1)(image=image)["image"]
plt.figure(figsize=(5, 5))
plt.imshow(image)

Transpose
image = cv2.imread('./cat3.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = albumentations.Transpose(p=1)(image=image)["image"]
plt.figure(figsize=(5, 5))
plt.imshow(image)

GridDistortion
image = cv2.imread('./cat3.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = albumentations.GridDistortion(p=1)(image=image)["image"]
plt.figure(figsize=(5, 5))
plt.imshow(image)

HueSaturationValue
image = cv2.imread('./cat3.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = albumentations.HueSaturationValue(p=1)(image=image)["image"]
plt.figure(figsize=(5, 5))
plt.imshow(image)

GaussNoise
image = cv2.imread('./cat3.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = albumentations.GaussNoise(var_limit=(10, 1000), p=1)(image=image)["image"]
plt.figure(figsize=(5, 5))
plt.imshow(image)

Blur
image = cv2.imread('./cat3.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = albumentations.Blur(blur_limit=100, p=1)(image=image)["image"]
plt.figure(figsize=(5, 5))
plt.imshow(image)

CLAHE
image = cv2.imread('./cat3.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = albumentations.CLAHE(clip_limit=2, p=1)(image=image)["image"]
plt.figure(figsize=(5, 5))
plt.imshow(image)

CenterCrop
image = cv2.imread('./cat3.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = albumentations.CenterCrop(height=320, width=320, p=1)(image=image)["image"]
print(image.shape)
plt.figure(figsize=(5, 5))
plt.imshow(image)

RandomSizedCrop
image = cv2.imread('./cat3.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
print(image.shape)
image = albumentations.RandomSizedCrop(min_max_height=(50, 1200), height=1200, width=1200, p=1)(image=image)["image"]
print(image.shape)
plt.figure(figsize=(5, 5))
plt.imshow(image)

Compose
image = cv2.imread('./cat3.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
print(image.shape)
image = albumentations.Compose([
albumentations.CLAHE(),
albumentations.RandomRotate90(),
albumentations.Transpose(),
albumentations.ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.50, rotate_limit=45, p=.75),
albumentations.Blur(blur_limit=3),
albumentations.OpticalDistortion(),
albumentations.GridDistortion(),
albumentations.HueSaturationValue()
], p=1.0)(image=image)['image']
print(image.shape)
plt.figure(figsize=(5, 5))
plt.imshow(image)

OneOf
image = cv2.imread('./cat3.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
print(image.shape)
image = albumentations.OneOf([
albumentations.CLAHE(),
albumentations.RandomRotate90(),
albumentations.Transpose(),
albumentations.ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.50, rotate_limit=45, p=.75),
albumentations.Blur(blur_limit=3),
albumentations.OpticalDistortion(),
albumentations.GridDistortion(),
albumentations.HueSaturationValue()
], p=1.0)(image=image)['image']
print(image.shape)
plt.figure(figsize=(5, 5))
plt.imshow(image)
