图片数据增强

使用torchvision 的transforms 进行图片增强

import torch
from torchvision import transforms
import numpy as np 
import  PIL.Image as Image
import matplotlib.pyplot as plt  
def imshow(img_path, transform):  

    img = Image.open(img_path)  
    fig,ax = plt.subplots(1, 2, figsize=(15, 4))  
    ax[0].set_title(f'Original image {img.size}')  
    ax[0].imshow(img)  
    img = transform(img)  
    ax[1].set_title(f'Transformed image {img.size}')  
    ax[1].imshow(img)
path="D:/Desktop/lenna.png"

Resize Rescale

#此函数用于将图像的高度和宽度调整为我们想要的特定大小。

tranform = transforms.Resize((224, 224))
imshow(path,tranform)

 Cropping

#使用 CenterCrop 来返回一个中心裁剪的图像。
tranform = transforms.CenterCrop((224, 224))
imshow(path,tranform)

RandomResizedCrop 

 #裁剪和调整大小。
tranform = transforms.RandomResizedCrop((100, 300))
imshow(path,tranform)

 

Flipping 

#水平或垂直翻转图像
tranform = transforms.RandomHorizontalFlip()
imshow(path,tranform)

Padding 

#在图像的所有边缘上按指定的数量填充
tranform = transforms.Pad((50,50,50,50))
imshow(path,tranform)

Rotation 

#图像随机施加旋转角度
tranform = transforms.RandomRotation(45)
imshow(path,tranform)

Random Affine 

#保持中心不变的变换
transform = transforms.RandomAffine(1, translate=(0.5, 0.5), scale=(1, 1), shear=(1,1), fillcolor=(256,256,256))  
imshow(path,tranform)

Gaussian Blur 

#图像将使用高斯模糊进行模糊处理。

transform = transforms.GaussianBlur(7, 3)  
imshow(path, transform)

Grayscale 

transform = transforms.Grayscale(num_output_channels=3)  
imshow(path, transform)

 Brightness

#改变图像的亮度当与原始图像对比时,生成的图像变暗或变亮。

transform = transforms.ColorJitter(brightness=2)  
imshow(path, transform)

 Contrast

#图像最暗和最亮部分之间的区别程度被称为对比度。图像的对比度也可以作为增强进行调整。
transform = transforms.ColorJitter(contrast=2)  
imshow(path, transform)

Saturation 

#饱和度
tranformr=transforms.ColorJitter(saturation=20)
imshow(path, transform)

Hue

#色调被定义为图片中颜色的深浅。
tranformr=transforms.ColorJitter(hue=2)
imshow(path, transform)

 

猜你喜欢

转载自blog.csdn.net/weixin_43852823/article/details/127652984