• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Python图像读写方法对比

python 搞代码 4年前 (2022-01-09) 17次浏览 已收录 0个评论

1  实验标准

因为训练使用的框架是Pytorch,因此读取的实验标准如下:

1、读取分辨率都为1920×1080的5张图片(png格式一张,jpg格式四张)并保存到数组。

2、将读取的数组转换为维度顺序为CxHxW的Pytorch张量,并保存到显存中(我使用GPU训练),其中三个通道的顺序为RGB。

3、记录各个方法在以上操作中所耗费的时间。因为png格式的图片大小差不多是质量有微小差异的jpg格式的10倍,所以数据集通常不会用png来保存,就不比较这两种格式的读取时间差异了。

写入的实验标准如下:

1、将5张1920×1080的5张图像对应的Pytorch张量转换为对应方法可使用的数据类型数组。

2、以jpg格式保存五张图片。

3、记录各个方法保存图片所耗费的时间。

2  实验情况

2.1  cv2

因为有GPU,所以cv2读取图片有两种方式:

1、先把图片都读取为一个numpy数组,再转换成保存在GPU中的pytorch张量。

2、初始化一个保存在GPU中的pytorch张量,然后将每张图直接复制进这个张量中。

第一种方式实验代码如下:

import os, torch
import cv2 as cv 
import numpy as np 
from time import time 
 
read_path = 'D:test'
write_path = 'D:test\\write\\'
 
# cv2读取 1
start_t = time()
imgs = np.zeros([5, 1080, 1920, 3])
for img, i in zip(os.listdir(read_path), range(5)): 
 img = cv.imread(filename=os.path.join(read_path, img))
 imgs[i] = img 
imgs = torch.tensor(imgs).to('cuda')[...,[2,1,0]].permute([0,3,1,2])/255 
print('cv2 读取时间1:', time() - start_t) 
# cv2保存
start_t = time()
imgs = (imgs.permute([0,2,3,1])[...,[2,1,0]]*255).cpu().numpy()
for i in range(imgs.shape[0]): 
 cv.imwrite(write_path + str(i) + '.jpg', imgs[i])
print('cv2 保存时间:', time() - start_t)

 实验结果:

cv2 读取时间1: 0.39693760871887207
cv2 保存时间: 0.3560612201690674

第二种方式实验代码如下:

import os, torch
import cv2 as cv 
import numpy as np 
from time import time 
 
read_path = 'D:test'
write_path = 'D:test\\write\\'
 
 
# cv2读取 2
start_t = time()
imgs = torch.zeros([5, 1080, 1920, 3], device='cuda')
for img, i in zip(os.listdir(read_path), range(5)): 
 img = torch.tensor(cv.imread(filename=os.path.join(read_path, img)), device='cuda')
 imgs[i] = img  
imgs = imgs[...,[2,1,0]].permute([0,3,1,2])/255 
print('cv2 读取时间2:', time() - start_t) 
# cv2保存
start_t = time()
imgs = (imgs.permute([0,2,3,1])[...,[2,1,0]]*255).cpu().numpy()
for i in range(imgs.shape[0]): 
 cv.imwrite(write_path + str(i) + '.jpg', imgs[i])
print('cv2 保存时间:', time() - start_t)

实验结果:

cv2 读取时间2: 0.23636841773986816
cv2 保存时间: 0.3066873550415039

2.2  matplotlib

同样两种读取方式,第一种代码如下:

import os, torch 
import numpy as np
import matplotlib.pyplot as plt 
from time import time 
 
read_path = 'D:test'
write_path = 'D:test\\write\\'
 
# matplotlib 读取 1
start_t = time()
imgs = np.zeros([5, 1080, 1920, 3])
for img, i in zip(os.listdir(read_path), range(5)): 
 img = plt.imread(os.path.join(read_path, img)) 
 imgs[i] = img  
i<b>本文来源gao@!dai!ma.com搞$$代^@码!网</b>mgs = torch.tensor(imgs).to('cuda').permute([0,3,1,2])/255 
print('matplotlib 读取时间1:', time() - start_t) 
# matplotlib 保存
start_t = time()
imgs = (imgs.permute([0,2,3,1])).cpu().numpy()
for i in range(imgs.shape[0]): 
 plt.imsave(write_path + str(i) + '.jpg', imgs[i])
print('matplotlib 保存时间:', time() - start_t)

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Python图像读写方法对比

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址