参考链接: torchvision.transforms
参考链接: 透视变换原理实例代码详解
参考链接: 图像几何变换之透视变换
打开并显示图片文件:
from torchvision import transforms
from matplotlib import pyplot as plt
import numpy as np
import torch
from PIL import Image
img = Image.open("KJ.jpg")
img.show()
plt.imshow(img)
plt.show()
运行结果展示:
PIL image转为tensor类型:
from torchvision import transforms
from matplotlib import pyplot as plt
import numpy as np
import torch
from PIL import Image
img = Image.open("KJ.jpg")
to_tensor = transforms.ToTensor()
img_t = to_tensor(img) # 注意:这里会自动调整通道维度的位置,即通道维度放到最开头
print('img_t.shape:',img_t.shape) # img_t.shape: torch.Size([3, 374, 500])
print('type(img_t):',type(img_t)) # type(img_t): <class 'torch.Tensor'>
print(img_t.shape, img_t.dtype) # torch.Size([3, 374, 500]) torch.float32
plt.imshow(img_t.permute(1, 2, 0)) # 需要将通道所对应的维度放到最后
plt.show()
运行结果:
中心裁剪加随机旋转:
from torchvision import transforms
from matplotlib import pyplot as plt
import numpy as np
import torch
from PIL import Image
img = Image.open("KJ.jpg")
trans4cxq = transforms.Compose([
transforms.CenterCrop((374,400)),
transforms.RandomRotation([-180,180]),
transforms.ToTensor(),
])
img_tr = trans4cxq(img)
plt.imshow(img_tr.permute(1, 2, 0)) # 需要将通道所对应的维度放到最后
plt.show()
运行结果展示:
透视变换:
from torchvision import transforms
from matplotlib import pyplot as plt
import numpy as np
import torch
from PIL import Image
img = Image.open("KJ.jpg")
scale = 5.0
img = Image.open("poker.png")
img_func = transforms.functional.perspective(
img,
[[63,72], [162,30], [270,144], [151,218]],
[[10*scale,20*scale], [30*scale,20*scale], [30*scale,40*scale], [10*scale,40*scale]],
interpolation=3)
img_func.show()
plt.imshow(img_func)
plt.show()
所使用的图片和运行结果展示:
使用函数来实现随机旋转,并且添加填充:
from torchvision import transforms
from matplotlib import pyplot as plt
import numpy as np
import torch
from PIL import Image
img = Image.open("KJ.jpg")
img = Image.open("KJ.jpg")
img_func = transforms.functional.rotate(
img, 45,
resample=False,
expand=True,
center=None)
img_func.show()
plt.imshow(img_func)
plt.show()
运行效果:
完整实验代码:
from torchvision import transforms
from matplotlib import pyplot as plt
import numpy as np
import torch
from PIL import Image
img = Image.open("KJ.jpg")
# img.show()
# plt.imshow(img)
# plt.show()
# to_tensor = transforms.ToTensor()
# img_t = to_tensor(img) # 注意:这里会自动调整通道维度的位置,即通道维度放到最开头
# print('img_t.shape:',img_t.shape) # img_t.shape: torch.Size([3, 374, 500])
# print('type(img_t):',type(img_t)) # type(img_t): <class 'torch.Tensor'>
# print(img_t.shape, img_t.dtype) # torch.Size([3, 374, 500]) torch.float32
# plt.imshow(img_t.permute(1, 2, 0)) # 需要将通道所对应的维度放到最后
# plt.show()
# trans4cxq = transforms.Compose([
# transforms.CenterCrop((374,400)),
# transforms.RandomRotation([-180,180]),
# transforms.ToTensor(),
# ])
# img_tr = trans4cxq(img)
# plt.imshow(img_tr.permute(1, 2, 0)) # 需要将通道所对应的维度放到最后
# plt.show()
# scale = 5.0
# img = Image.open("poker.png")
# img_func = transforms.functional.perspective(
# img,
# [[63,72], [162,30], [270,144], [151,218]],
# [[10*scale,20*scale], [30*scale,20*scale], [30*scale,40*scale], [10*scale,40*scale]],
# interpolation=3)
# img_func.show()
# plt.imshow(img_func)
# plt.show()
# scale = 100.0
# img = Image.open("cxq.jpg")
# img_func = transforms.functional.perspective(
# img,
# [[3097,3015], [1649,3405], [591,1393], [1761,1477]],
# [[10*scale,20*scale], [30*scale,20*scale], [30*scale,40*scale], [10*scale,40*scale]],
# interpolation=3)
# img_func.show()
img = Image.open("KJ.jpg")
img_func = transforms.functional.rotate(
img, 45,
resample=False,
expand=True,
center=None)
img_func.show()
plt.imshow(img_func)
plt.show()
实验所使用的图片文件: