当前位置:   article > 正文

图像格式转换_np.transpose(img, (2, 0, 1))

np.transpose(img, (2, 0, 1))

tensor–>numpy img = img.numpy()
numpy–>tensor
tensor = transforms.ToTensor()
tensor_img = tensor(img)

tensor–>PIL
CHW–>HWC img = np.transpose(img, (1,2,0))

HWC–>CHW img = np.transpose(img, (2,0,1))

查看tensor格式图像 tensor–>numpy

img,tar = test_set[0]
print(type(img))  # <class 'torch.Tensor'>
img = img.numpy()
print(type(img)) # <class 'numpy.ndarray'>
print(img.shape) # (3, 32, 32)
img = np.transpose(img, (1,2,0))
print(img.shape) # (32, 32, 3)

# opencv中的颜色通道顺序是BGR而PIL、torch里面的图像颜色通道是RGB
img=cv2.cvtColor(mat,cv2.COLOR_BGR2RGB)
cv2.imshow('image', img)
cv2.waitKey(0)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

查看tensor格式图像 tensor–>PIL

from torchvision import transforms
unloader = transforms.ToPILImage()
image = original_tensor.cpu().clone()  # clone the tensor
image = image.squeeze(0)  # remove the fake batch dimension
image = unloader(image)
image.save('example.jpg')

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/823840
推荐阅读
相关标签
  

闽ICP备14008679号