记录一些常用的前处理,每次都重写也是很费劲。。
```python
import cv2
import numpy as np
# 不失真resize 在resize时加上黑边
def distortionless_resize(img_org,target_size = (320,320)):
h,w = img_org.shape[:2]
if h==w:
new_img = cv2.resize(img_org,target_size)
elif h>w:
new_img = np.zeros((h,h,3),np.uint8)
new_img[:,int((h-w)//2):int((h-w)//2+w),:]=img_org
new_img = cv2.resize(new_img,target_size)
else:
new_img = np.zeros((w,w,3),np.uint8)
new_img[int((w-h)//2):int((w-h)//2+h),:,:]=img_org
new_img = cv2.resize(new_img,target_size)
cv2.imwrite('test.jpg',new_img)
return new_img
# 转为tensor
def img_numpy2tensor(img):
img -= (104, 117, 123)
img = img.transpose(2, 0, 1)
img = torch.from_numpy(img).unsqueeze(0)
img = img.to(device)
return img
使用transform方法处理
import torchvision.transforms as transforms
import cv2
import numpy as np
# 使用transform
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5,0.5, 0.5))])
def pre_deal(img):
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = transform(img)
img = img.unsqueeze(0)
return img
加个后处理方法
使用gan生成图片后的保存代码
简版
import torchvision
torchvision.utils.save_image(img, imgPath,normalize=True)
如果没有vision
那就自己动手写个呗
import numpy as np
import cv2
...
#此处略去生成过程 假设img就是生成的图像 img shape = (3,256,256)
img = np.transpose(img,(1,2,0))
# (img/255 - mean) /std - 1 ???
# 如果使用了normalize
img = (img + 1) / 2.0 * 255.0
img = np.clip(img,0,255)
# torch 方法 img = (img.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
# 如果没有使用的话
# img *= 255
img.astype(np.uint8)
抄一个b乎上的代码
import torchvision.transforms as transforms
import cv2
img_path = "./panda.jpg"
train_transformer = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize(256),
# transforms.RandomResizedCrop(224,scale=(0.5,1.0)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
##numpy.ndarray
img = cv2.imread(img_path) # 读取图像
img1 = train_transformer(img)