import cv2
import albumentations
from albumentations.pytorch.transforms import ToTensorV2
import matplotlib.pyplot as plt
注意统一要在plt.imshow(image)后添加一个plt.waitforbuttonpress()函数才能看到图片
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)
# 变换尺寸,变换后的尺寸为320,320
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"] # 在(-180, 180)之间随机旋转
plt.figure(figsize=(5, 5))
plt.imshow(image)
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"]
# RandomBrightnessContrast默认参数亮度和对比度都是0.2,将亮度调到0.8后发现猫明显变亮
plt.figure(figsize=(5, 5))
plt.imshow(image)
image = cv2.imread('./cat3.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# shift_limit 移动因子(我看着不像是平移倒像是镜像即以某个点为中心,中心对称),设置高和宽的移动因子取值范围,如果shift_limit是一个浮点数,
# 则移动因子的取值范围为(-shift_limit, shift_limit),shift_limit的取值范围的绝对值应在[0,1]之间,默认值是(-0.0625, 0.0625).
# scale_limit 缩放因子范围,这个倒像是进行缩放的,其取值范围为[0, 1] 默认为(-0.1, 0.1)
# rotate_limit 旋转角度 默认为(-45, 45)
image = albumentations.ShiftScaleRotate(
shift_limit=0.0625, scale_limit=0.1, rotate_limit=45, p=1.0 # 对图片进行平移(translate)、缩放(scale)和旋转(roatate)
)(image=image)["image"]
plt.figure(figsize=(5, 5))
plt.imshow(image)
image = cv2.imread('./cat3.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# 这个就是标准化操作,显示出来的不是正常的图片是值缩放到[0,1]之间后的图片,一般处理完图片后会再缩放回去,
# 这里显示下就是为了体现进行了标准化操作
# [0.485, 0.456, 0.406] 均值,[0.229, 0.224, 0.225] 标准差 max_pixel_value最大像素值,图片的最大像素值为255
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)
image = cv2.imread('./cat3.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
print(type(image))
print(image.shape)
# 从这两个输出上可以看出来ToTensorV2的作用就是转换维度即将numpy的HWC转换成pytorch 的`CHW`.
image = ToTensorV2()(image=image)["image"]
print(image.shape)
# plt.figure(figsize=(5, 5)) # 转换完格式后是不能进行画图的,因为画图的格式就是numpy的HWC格式
# plt.imshow(image)
image = cv2.imread('./cat3.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = albumentations.RandomRotate90(p=1)(image=image)["image"] # 随机旋转90度
plt.figure(figsize=(5, 5))
plt.imshow(image)
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)
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)
image = cv2.imread('./cat3.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# 随机改变图片的 HUE(色相)、饱和度和值
image = albumentations.HueSaturationValue(p=1)(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.GaussNoise(var_limit=(10, 1000), p=1)(image=image)["image"]
plt.figure(figsize=(5, 5))
plt.imshow(image)
image = cv2.imread('./cat3.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# MotionBlur 这个模糊的轻些
# image = albumentations.MotionBlur(blur_limit=100, p=1)(image=image)["image"]
# MedianBlur 不支持 blur_limit 这个参数
# image = albumentations.MedianBlur(blur_limit=100, p=1)(image=image)["image"]
# 模糊图片,blur_limit的取值范围为:[3, inf),默认为(3, 7)
image = albumentations.Blur(blur_limit=100, p=1)(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.CLAHE(clip_limit=2, p=1)(image=image)["image"]
plt.figure(figsize=(5, 5))
plt.imshow(image)
image = cv2.imread('./cat3.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# 中心裁切,heigit:要裁切的高度, width:要裁切的宽度
image = albumentations.CenterCrop(height=320, width=320, p=1)(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)
print(image.shape)
# 随机大小裁切 裁切后的 height=1200, width=1200, min_max_height:在(50, 1200)这个范围中随机裁切一个尺寸,裁切下来后再放大到height
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)
image = cv2.imread('./cat3.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
print(image.shape)
# 组合操作
image = albumentations.Compose([
# 对比度受限直方图均衡
#(Contrast Limited Adaptive Histogram Equalization)
albumentations.CLAHE(),
# 随机旋转 90°
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(),
# 随机改变图片的 HUE、饱和度和值
albumentations.HueSaturationValue()
], p=1.0)(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)
print(image.shape)
# 单一操作 即在这下边这几个中选择一个进行变换
image = albumentations.OneOf([
# 对比度受限直方图均衡
#(Contrast Limited Adaptive Histogram Equalization)
albumentations.CLAHE(),
# 随机旋转 90°
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(),
# 随机改变图片的 HUE、饱和度和值
albumentations.HueSaturationValue()
], p=1.0)(image=image)['image']
print(image.shape)
plt.figure(figsize=(5, 5))
plt.imshow(image)