Pytoch之torchvision.transforms图像变换实例
transforms.CenterCrop(size)
将给定的PIL.Image进行中心切割,得到给定的size,size可以是tuple,(target_height,target_width)。size也可以是一个Integer,在这种情况下,切出来的图片的形状是正方形。
size可以为int,也可以为float
#定义中心切割 centerCrop=transforms.CenterCrop((img.size[0]/2,img.size[1]/2)) imgccrop=centerCrop(img) transforms.RandomCrop(size,padding=0)
切割中心点的位置随机选取。size可以是tuple也可以是Integer。
但是如果是Tuple,只能是int型的不能是float
#要求目标size必须为整数 randomCrop1=transforms.RandomCrop((int(img.size[0]/2),int(img.size[1]/2)))#padding默认为False randomCrop2=transforms.RandomCrop((int(img.size[0]/2),int(img.size[1]/2)),padding=10) imgrcrop1=randomCrop1(img) imgrcrop2=randomCrop2(img)
transforms.RandomHorizontalFlip
随机水平翻转给定的PIL.Image,概率为0.5。即:一半的概率翻转,一半的概率不翻转。
#随机将图片旋转180° randomFlip=transforms.RandomHorizontalFlip() imgf=randomFlip(img)
transforms.RandomSizedCrop(size,interpolation=2)
先将给定的PIL.Image随机切,然后再resize成给定的size大小。
size只能是Int,不能是float或tuple
#参数需为一个整数,不能是float或者tuple randomcut=transforms.RandomResizedCrop(100) imgc=randomcut(img)
transforms.Pad(padding,fill=0)
将给定的PIL.Image的所有边用给定的padvalue填充。padding:要填充多少像素fill
可以用于给图片加边框2333
#加边界框 pad=transforms.Pad(padding=10,fill=0) pad_img=pad(img) pad_img.save("block.jpg")
以上这篇Pytoch之torchvision.transforms图像变换实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。