import cv2 import torchvision.transforms as transforms import numpy as np import torch import matplotlib.pyplot as plt from PIL import Image
'''PIL image''' image = Image.open("./food-11/0000.jpg") print(image) # <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=384x512 at 0x7F1CFE47DD60> image2 = transforms.ToTensor()(image) # to simplify computation print(image2) # tensor.data[0, 1] print(image2.dtype) # tensor.float32 print(image2.shape) # tensor.Size([3, 512, 384]) # if you want opencv to support tensor image, you should know below # opencv support numpy format, dtype is uint8, pixle range [0, 255] # when use ToTensor, tensor range is [0, 1], dtype is FloatTensor # channel: PIL && torch:RGB opencv:BGR # dimensional: torch: CHW numpy: HWC # show tensor tensor_ = image2 array_ = tensor_.numpy() # convert tensor to numpy max = array_.max() array_ = array_*255/max# expand pixel from [0, 1] to [0, 255], then normalization array_convert = np.uint8(array_) # opencv only support uint8 numpy.array print("array_convert:", array_convert.shape) # array_convert: (3, 512, 384) array_convert = array_convert.transpose(1, 2, 0) # array_convert: (512, 384, 3) cv2.imshow("image", array_convert) # different from the original image cv2.waitKey() array_convert = cv2.cvtColor(array_convert, cv2.COLOR_RGB2BGR) # convert RGB to BGR cv2.imshow("image2", array_convert) # the same to the original image cv2.waitKey()