PIL、cv2读取类型以及转换,PIL、numpy、tensor格式以及cuda、cpu的格式转换

一、PIL,cv2读取数据图片以及之间的转换

cv2 PIL
读取 a=cv2.imread() a=Image.open()
读取类型 数组类型 PIL类型
读取尺寸排列 (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)
#cv2显示图像
img_path="E:\expression_recognition\\1.jpg"
img_cv2 = cv2.imread(img_path)  # cv2读取的是数组类型  BGR  H W C
cv2.imshow("img_cv2", img_cv2)
cv2.waitKey (0)

#PIL显示图像
img_path="E:\expression_recognition\\1.jpg"
img_PIL = Image.open(img_path)  # PIL读取的是PIL类型  RGB  W H C
img_PIL.show()

#cv2转变为PIL进行图像显示
img_path="E:\expression_recognition\\1.jpg"
img_cv2 = cv2.imread(img_path)  # cv2读取的是数组类型  BGR  H W C
img_cv2_PIL = Image.fromarray(cv2.cvtColor(img_cv2,cv2.COLOR_BGR2RGB))
img_cv2_PIL.show()

#PIL转变为cv2进行图像展示
img_path="E:\expression_recognition\\1.jpg"
img_PIL = Image.open(img_path)  # PIL读取的是PIL类型  RGB  W H C
img_PIL = numpy.array(img_PIL)#先转换为数组   H W C
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)
# 1、PIL转换为tensor的CPU、GPU格式
img_path="E:\expression_recognition\\1.jpg"
img_tensor = transforms.ToTensor()

img_PIL = Image.open(img_path)  # PIL读取的是PIL类型  RGB  W H C
img_PIL_tensor_CPU = img_tensor(img_PIL)
img_PIL_tensor_GPU = img_tensor(img_PIL).cuda()

2、数组转换为tensor类型(包含CPU和GPU)

# 1、数组转换为tensor的CPU、GPU格式
img_path="E:\expression_recognition\\1.jpg"
img_tensor = transforms.ToTensor()


img_PIL = Image.open(img_path)  # PIL读取的是PIL类型  RGB  W H C
img_array = numpy.array(img_PIL)  # PIL转换为数组类型  H W C
img_array_tensor1_CPU = img_tensor(img_array)  # 将数组格式转换tensor格式
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()   #cpu类型直接转换
img_gpu.cpu().numpy()   #gpu类型先转换为cpu类型然后再转换为数组类型

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)

# 1、数组转换为PIL格式
a2 = transforms.ToPILImage()
img_array = numpy.uint8(img_array)  # 满足类型需求
img_PIL = a2(img_array)

# 2、tensor转换为PIL格式
img_tensor = img_tensor.float()  # 满足类型需求
img_PIL = a2(img_tensor)

三、CPU tensor类型与GPU tensor类型的转换

img_gpu = cpu_img_tensor.cuda() #cpu转换为gpu
img_cpu = gpu_img_tensor.cpu()  #gpu转换为cpu

四、总结

CPU tensor GPU tensor PIL array
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)

tips:注意PIL与cv2转换要变换颜色通道。

参考:https://blog.csdn.net/hjkdh/article/details/123097434

你可能感兴趣的:(ML&DL笔记,numpy,opencv,人工智能)