计算机视觉下的数据增强代码实现

2024-01-09 21:32:13

数据增强的实现
使用经典的pytorch框架下的torchvision.transformers对计算机视觉进行增强的代码实现。
使用下面的图像进行数据增强,相应的效果图如下所示!
战斗机导包

import os
import PIL.Image as Image
from torchvision import transforms as transforms
import torchvision.transforms.functional as TF

2.1中心裁剪

代码

# torchvision实现中心裁剪

# 取出图像,使用PIL格式
def read_PIL(image_path):
    image = Image.open(image_path)
    return image
# 中心裁剪
def center_crop(image):
    CenterCrop = transforms.CenterCrop(size=(1000,900))
    cropped_image = CenterCrop(image)
    return cropped_image
im = read_PIL(r'./data/airplane.jpg')
print(im.size)
output_image_dir = r'./data/result'
os.makedirs(output_image_dir,exist_ok=True)
#中心裁剪
center_cropped_image = center_crop(im)
center_cropped_image.save(os.path.join(output_image_dir,'center_cropped_image.jpg'))

中心裁剪后的图像
在这里插入图片描述

2.2随机裁剪

def random_crop(image):
    RandomCrop = transforms.RandomCrop(size=(1000,900))
    random_image = RandomCrop(image)
    return random_image
#随机裁剪
random_cropped_image = random_crop(im)
random_cropped_image.save(os.path.join(output_image_dir,'random_cropped_image.jpg'))

在这里插入图片描述

2.3缩放

def resize_crop(image):
    ResizeCrop = transforms.Resize(size=(150,200))
    resize_image = ResizeCrop(image)
    return resize_image
#缩放
resize_cropped_image = resize_crop(im)
resize_cropped_image.save(os.path.join(output_image_dir,'resize_cropped_image.jpg'))

在这里插入图片描述

2.4水平翻转

def horizontal_crop(image):
    HorizontalCrop = transforms.RandomHorizontalFlip()
    horizontal_image = HorizontalCrop(image)
    return horizontal_image
#水平翻转
horizontal_cropped_image = horizontal_crop(im)
horizontal_cropped_image.save(os.path.join(output_image_dir,'horizontal_cropped_image.jpg'))
horizontal_cropped_image

2.5垂直翻转

def randomVertical_crop(image):
    RandomVerticalCrop = transforms.RandomVerticalFlip()
    randomVertical_image = RandomVerticalCrop(image)
    return randomVertical_image
#垂直翻转
randomVertical_cropped_image = randomVertical_crop(im)
randomVertical_cropped_image.save(os.path.join(output_image_dir,'randomVertical_cropped_image.jpg'))
randomVertical_cropped_image

2.6随机角度旋转

def randomRotation_crop(image):
    RandomRotationCrop = transforms.RandomRotation(degrees=(10,80))
    randomRotation_image = RandomRotationCrop(image)
    return randomRotation_image
#随机角度旋转
randomRotation_cropped_image = randomRotation_crop(im)
randomRotation_cropped_image.save(os.path.join(output_image_dir,'randomRotation_cropped_image.jpg'))
randomRotation_cropped_image

在这里插入图片描述

2.7色度、对比度、亮度、饱和度对比

def colorJitter_crop(image):
    im = transforms.ColorJitter(brightness=1)(image)
    im = transforms.ColorJitter(contrast=1)(im)
    im = transforms.ColorJitter(saturation=1)(im)
    im = transforms.ColorJitter(hue=1)(im)
    return im
#色度、对比度、亮度、饱和度对比
colorJitter_cropped_image = colorJitter_crop(im)
colorJitter_cropped_image.save(os.path.join(output_image_dir,'colorJitter_cropped_image.jpg'))
colorJitter_cropped_image

在这里插入图片描述

2.8随机灰度化

def randomGrayscale_crop(image):
    randomGrayscale_image = transforms.RandomGrayscale(p=0.5)(image)
    return randomGrayscale_image
#随机灰度化
randomGrayscale_cropped_image = randomGrayscale_crop(im)
randomGrayscale_cropped_image.save(os.path.join(output_image_dir,'randomGrayscale_cropped_image.jpg'))
randomGrayscale_cropped_image

在这里插入图片描述

2.9加入padding

def pad_crop(image):
    pad = transforms.Pad((0,(image.size[0]-image.size[1])//2))(image)
    return pad
#padding
pad_image = pad_crop(im)
pad_image.save(os.path.join(output_image_dir,'pad_image.jpg'))
pad_image

在这里插入图片描述

2.10指定区域擦除

def erase_crop(image,position,size):
    img = TF.to_tensor(image)
    erase_image = TF.to_pil_image(TF.erase(img=img,i=position[0],j=position[1],h=size[0],w=size[1],v=1))
    return erase_image
#指定区域擦除
erase_image = erase_crop(im,(200,400),(200,300))
erase_image.save(os.path.join(output_image_dir,'erase_image.jpg'))
erase_image

在这里插入图片描述

2.11伽玛变换

def gamma_crop(image,gamma_value):
    gamma_image = TF.adjust_gamma(img=image,gamma=gamma_value)
    return gamma_image
#随机裁剪
gamma_image = gamma_crop(im,0.2)
gamma_image.save(os.path.join(output_image_dir,'gamma_image.jpg'))
gamma_image

在这里插入图片描述

文章来源:https://blog.csdn.net/qq_37977007/article/details/135484263
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。