不同Python库(Opencv、PIL、Matplotlib)下图像读写的差别、联系与互转:

1. opencv

a. 读取:

img = cv2.imread(path)

读入的类型

默认是一个BGR模式的彩色图

转换类型

使用cv2.cvtColor(img, cv.COLOR_BGR2RGB)将颜色模式转化成RGB。

opencv转Tensor

torch.from_numpy(img)将图像转成tensor。

b. 保存:

cv2.imwrite(path, img)

c. 展示:

cv2.imshow(name, img)

cv2.waitKey(0)

cv2.destroyAllWindows()

d.读取gif格式的图像:

gif = cv2.VideoCapture(path)得到一个opencv的视频对象,然后使用

ret,frame = gif.read()读取该视频对象的一帧。

2. PIL

a. 读取图像:

img = PIL.Image.open(path)

读入类型

PIL.xxxImageFile类型

PIL转为array

np.asarray(img) 

array转PIL

Image.fromarray(array) 

颜色模式转换

img.convert(model)

model包括:‘1’:二值图,‘L’:灰度图,‘P’:

8位彩色图像,‘RGB’:

24位彩色图像(每个通道8位),‘RGBA’:alpha通道(不透明度)

b. 保存图像:

img.save(path)

c. 展示图像:

img.show()

3. matplotlib

a. 读取图像:

img = plt.imread()

读入的图像类型

numpy.array,且读取图像时直接进行了归一化操作

b. 保存图像:

plt.imsave(path,img)

img.imsave(path)不带坐标轴

plt.savefig(path)带坐标轴

c. 绘制图像:

plt.imshow(img)

plt.show()

4.OpenCV、Matplotlib、PIL互转:

image.png

5. pytorch如何处理数据集

1.opecv读取并处理,

2.使用matplotlib绘制图像并保存为PIL.Image,

3.torchvision.transforms.ToTensor()(img)转化为tensor

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值