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读取数据图片以及之间的转换

这里先列个表格方便理解清楚:

cv2 PIL
读取 a=cv2.imread() a=Image.open()
读取类型 数组类型 PIL类型
读取颜色通道 BGR RGB(这里需要注意的是当图像格式为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)

直接来看代码:

#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)

注意:PIL变为数组是由(W H C)转变为(H W C)
下面是具体的代码实例:

# 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)

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

PIL,cv2读取类型及转换,以及PIL,numpy,tensor格式以及cuda,cpu的格式转换相关推荐

  1. cv2读取np的矩阵图片,numpy数组clip和astype,查看数据类型array.dtype

    clip(a, a_min, a_max, out=None) 功能,将数组中的数据在(a_min, a_max)范围之外的数据切割在,这个范围直接,小于a_min的值修改为a_min ,大于 a_m ...

  2. python cv2读取图片后进行通道变换以及PIL阅读图像的通道转换

    读取的 图片的格式是HxWx3,像素值在0-1之间 img = cv2.imread('b.png') print(img.shape) img = img.permute(2,0,1) #如果进行H ...

  3. CV2/PIL/Matplotlib读取图片注意事项

    1.问题描述 我们在使用CV2/PIL/Matplotlib进行读取图像数据时,很容易在一个文件中混用这些读取.显示方式,但在这个过程中,我们没有过多去看这3种方式的区别,以至于混用时造成不可见的错误 ...

  4. cv2 和 matplotlib.pyplot 和 PIL.Image 读取图片方式对比【Python读取图片】

    文章目录 import matplotlib.pyplot as plt 和 cv2 读取图像对比 PIL 和 cv2 读取图片对比 mxnet.image.imread 读取图片 PIL 和 tor ...

  5. Python 随笔:用 PIL 库读取图像文件像素长宽大小

    Python 随笔:用 PIL 库读取图像文件像素宽高大小 1.前言 安装 PIL 库(全称是pillow),所以安装命令上的名称是pillow pip install pillow 2. 使用pil ...

  6. ERROR: from PIL import Image ImportError: No module named PIL

    ERROR: from PIL import Image ImportError: No module named PIL 到 http://www.pythonware.com/products/p ...

  7. “Could not import PIL.Image. The use of array_to_img requires PIL.”错误的解决办法

    基于dataset\training_set数据,根据提供的结构,建立CNN模型,识别图片中的猫/狗,计算预测准确率: 1.识别图片中的猫/狗.计算dataset\test_set测试数据预测准确率 ...

  8. Python之PIL之绘图:基于Python的PIL库绘制各种图形、添加文字等

    Python之PIL之绘图:基于Python的PIL库绘制各种图形.添加文字等 目录 一.绘制各种形状各种案例 1.绘制矩形 2.绘制圆形.弧线形

  9. python调用usb摄像头_Python通过cv2读取多个USB摄像头

    本文实例为大家分享了Python通过cv2读取多个USB摄像头的具体代码,供大家参考,具体内容如下 通过 cv2 可以轻易的拿到摄像头数据. 比如以下几步就能打开摄像头显示,并通过 q 键保存图片 i ...

最新文章

  1. ajax invoke error,配置全局的异常捕获时,走ajax请求下面报错
  2. 计算机网络职称考试,职称计算机考试Internet基础知识:计算机网络组成
  3. T-SQL 控制流语句
  4. 修改 cmd 字体为 Consolas
  5. mysql俩个表之间关联语法_MySQL多表关联SQL语句调优
  6. android引导用户打开位置权限_想升级 App?先要个权限吧!!!
  7. 基于Qt的A*算法可视化分析
  8. arduino uno r3引脚图_Arduino入门: 安装和熟悉Arduino IDE
  9. POJ 1979 红与黑
  10. revit2016注册表删除_Revit怎么卸载,如何把revit彻底卸载删除干净重新安装的方法?【转载】...
  11. pdf怎么转换成word格式不变?
  12. ESP32|基于ESP32制作的低成本、可拓展性高的NES游戏机(1)(开源ESP32 NES模拟器)-效果演示及介绍
  13. EasyExcel导出设置表头字体样式和批注
  14. 用计算机探索规律反思,规律的背后——用计算器探索规律教学反思
  15. html中如何设置动画效果,css3如何设置动画?
  16. buct寒假集训——lca
  17. 直观理解Beta分布
  18. 计算机软件 大shen
  19. VS2008,C++,镜子反射 光线反射游戏
  20. 光伏逆变器企业的生存逻辑

热门文章

  1. java 安全登陆系统设计,基于JAVA的安全管理信息系统设计
  2. macbook卡在进度条开不了机_【macbook开机时进度条加载不进去怎么办?】-看准网...
  3. dataframe求两列的相乘,再将输出为新的一列
  4. python中complex函数输出结果表示_下面代码的执行结果是
  5. LeetCode之换酒问题(一千五百一十八)
  6. 递归 - java实现
  7. 无人机光伏电站检测系统
  8. 【数据采集】基于FPGA通用数据采集测试系统
  9. 前端实现炫酷进度条插件
  10. 机器学习实战读书总结