完整的代码如下:
import math
import time
import torch
import cv2
import albumentations
from albumentations.pytorch.transforms import ToTensorV2
import matplotlib.pyplot as plt
import matplotlib
import torchvision.transforms as transforms
deal_image = transforms.Compose([
transforms.Resize((224, 224)),
])
matplotlib.use('TkAgg')
image = cv2.imread('./idea.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image_tensor = torch.tensor(image).permute(2, 0, 1)
image_tensor = image_tensor.unsqueeze(dim=0)
deal_after = deal_image(image_tensor)
window_size = 20
B, C, H, W = deal_after.shape
only_right_shift =torch.cat((deal_after[:, :, window_size // 2:, :], deal_after[:,:,H-window_size//2:,:]),dim=2)
reverse_unsqueeze = deal_after.squeeze()
reverse_unsqueeze_only_right_shift = only_right_shift.squeeze()
reverse_unsqueeze = reverse_unsqueeze.permute(1, 2, 0)
reverse_unsqueeze_only_right_shift = reverse_unsqueeze_only_right_shift.permute(1, 2, 0)
plt.figure(figsize=(5, 5))
plt.imshow(image)
plt.pause(20)
plt.imshow(reverse_unsqueeze)
plt.pause(10)
plt.imshow(reverse_unsqueeze_only_right_shift)
plt.pause(20)
执行结果:
下边这张是原图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/26b91c62d1875d5d1f44f4e604d0d6b3.png)
下边这张没有移动但是处理成224*224的图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/34372c2f759548fe960c06dd7289b260.png)
这个是即移动又被处理成224*224的图 可以看到吊灯的位置有明显的移动而图片竖着看,最底部明显加宽
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d52abd5458287057c465c7f605276e11.png)