PIL,cv2读取类型,以及PIL,numpy,tensor格式以及cuda,cpu的格式转换
- 一、PIL,cv2读取数据图片以及之间的转换
- 二、PIL,数组类型以及tensor类型的转换
* - 1、PIL转换为tensor类型(包含CPU和GPU)
- 2、数组转换为tensor类型(包含CPU和GPU)
- 3、tensor类型转换为数组类型
- 4、tensor、数组类型转换为PIL类型
- 三、CPU tensor类型与GPU tensor类型的转换
- 四、对二和三进行一个总结
一、PIL,cv2读取数据图片以及之间的转换
这里先列个表格方便理解清楚:
cv2PIL读取a=cv2.imread()a=Image.open()读取类型数组类型PIL类型读取颜色通道BGRRGB(这里需要注意的是当图像格式为RGBA时,PIL读取的是RGBA)读取尺寸排列(H,W,C)(W,H,C)显示图片cv2.imshow(“a”, a)
cv2.waitKey (0)
a.show()相互之间转换显示Image.fromarray(cv2.cvtColor(img,
cv2.COLOR_BGR2RGB))a=numpy.array(a)#先转换为数组
a=cv2.cvtColor(a,cv2.COLOR_RGB2BGR)
转变颜色通道转换为数组类型cv2读取的就是数组类型a = numpy.array(a)
直接来看代码:
img_path="E:\expression_recognition\\1.jpg"
img_cv2 = cv2.imread(img_path)
cv2.imshow("img_cv2", img_cv2)
cv2.waitKey (0)
img_path="E:\expression_recognition\\1.jpg"
img_PIL = Image.open(img_path)
img_PIL.show()
img_path="E:\expression_recognition\\1.jpg"
img_cv2 = cv2.imread(img_path)
img_cv2_PIL = Image.fromarray(cv2.cvtColor(img_cv2,cv2.COLOR_BGR2RGB))
img_cv2_PIL.show()
img_path="E:\expression_recognition\\1.jpg"
img_PIL = Image.open(img_path)
img_PIL = numpy.array(img_PIL)
img_PIL_cv2 = cv2.cvtColor(img_PIL,cv2.COLOR_RGB2BGR)
cv2.imshow("img_PIL_cv2",img_PIL_cv2)
cv2.waitKey (0)
都会显示为如下图片。
二、PIL,数组类型以及tensor类型的转换
1、PIL转换为tensor类型(包含CPU和GPU)
主要有两种方式:
transforms.ToTensor()torch.from_numpy能转换的格式PIL和数组格式只能转换数组格式具体转换过程a = transforms.ToTensor()
a(img_PIL)
a(img_array)torch.from_numpy(img_array)
注意:PIL变为数组是由(W H C)转变为(H W C)
下面是具体的代码实例:
img_path="E:\expression_recognition\\1.jpg"
img_tensor = transforms.ToTensor()
img_PIL = Image.open(img_path)
img_PIL_tensor_CPU = img_tensor(img_PIL)
img_PIL_tensor_GPU = img_tensor(img_PIL).cuda()
2、数组转换为tensor类型(包含CPU和GPU)
img_path="E:\expression_recognition\\1.jpg"
img_tensor = transforms.ToTensor()
img_PIL = Image.open(img_path)
img_array = numpy.array(img_PIL)
img_array_tensor1_CPU = img_tensor(img_array)
img_array_tensor1_GPU = img_tensor(img_array).cuda()
img_array_tensor2_CPU = torch.from_numpy(img_array)
img_array_tensor2_GPU = torch.from_numpy(img_array).cuda()
3、tensor类型转换为数组类型
注意在tensor类型转换为数组类型中,tensor类型只能是cpu tensor类型
img_cpu.numpy()
img_gpu.cpu().numpy()
4、tensor、数组类型转换为PIL类型
tensor(可以是GPU也可以是CPU)转换为PIL格式数组转换为PIL格式第一步img_array = numpy.uint8(img_array)img_tensor = img_tensor.float()第二步a = transforms.ToPILImage()
img_PIL = a(img_array)a = transforms.ToPILImage()
img_PIL = a(img_tensor)
img_path="E:\expression_recognition\\1.jpg"
img = Image.open(img_path)
img_array = numpy.array(img)
a1 = transforms.ToTensor()
img_tensor = a1(img)
a2 = transforms.ToPILImage()
img_array = numpy.uint8(img_array)
img_PIL = a2(img_array)
img_tensor = img_tensor.float()
img_PIL = a2(img_tensor)
三、CPU tensor类型与GPU tensor类型的转换
img_gpu = cpu_img_tensor.cuda()
img_cpu = gpu_img_tensor.cpu()
四、对二和三进行一个总结
CPU tensorGPU tensorPILarray
CPU tensor
(cpu_img_tensor)cpu_img_tensor.cuda()a = transforms.ToPILImage()
img_tensor = img_tensor.float()
img_PIL = a(img_tensor)cpu_img_tensor.numpy()
GPU tensor
(gpu_img_tensor)gpu_img_tensor.cpu()同cpu tensor的转换方式gpu_img_tensor.cpu().numpy()
PIL
(img_PIL)a = transforms.ToTensor()
a(img_PIL)a = transforms.ToTensor()
a(img_PIL).cuda()numpy.array(img_PIL)
array
(img_array)a = transforms.ToTensor()
a(img_array)
torch.from_numpy(img_array)a = transforms.ToTensor()
a(img_array).cuda()
torch.from_numpy(img_array).cuda()a = transforms.ToPILImage()
img_array=numpy.uint8(img_array)
img_PIL = a2(img_array)注意PIL与cv2转换要变换颜色通道。
Original: https://blog.csdn.net/hjkdh/article/details/123097434
Author: 燃烧吧哥们
Title: PIL,cv2读取类型及转换,以及PIL,numpy,tensor格式以及cuda,cpu的格式转换
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/532435/
转载文章受原作者版权保护。转载请注明原作者出处!