栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

matplotlib.pyplot鐢ㄦ硶_matplotlib.pyplot.imshow?

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

matplotlib.pyplot鐢ㄦ硶_matplotlib.pyplot.imshow?

matplotlib.pyplot.imread(path)用于读取一张图片,将图像数据变成数组array.


参数:

要读取的图像文件路径。

返回值:

如果是灰度图:返回(M,N)形状的数组,M表示高度,N表示宽度。

如果是RGB图像,返回(M, N, 3) 形状的数组,M表示高度,N表示宽度。

如果是RGBA图像,返回(M, N, 4) 形状的数组,M表示高度,N表示宽度。

此外,PNG 图像以浮点数组 (0-1) 的形式返回,所有其他格式都作为 int 型数组返回,位深由具体图像决定。

因此,在pytorch中如果需要读取文件,则需要通过 .permute(2, 0, 1) 将图像由HWC->CHW


示例
		>>> file_path="D:/Scientific Research/BRDF/Dataset/2021/subtrain/0000007;metal_lead_roughXconcrete_010;1X4.png"
		>>> import matplotlib.pyplot as plt
		>>> import torch
		>>> plt.imread(file_path)
		array([[[0.02352941, 0.02352941, 0.02352941],
		        [0.01960784, 0.01960784, 0.01960784],
		        [0.01568628, 0.01568628, 0.01568628],
		        ...,
		       
		        [0.04313726, 0.04313726, 0.04313726],
		        [0.04313726, 0.04313726, 0.04313726],
		        [0.04313726, 0.03921569, 0.03921569]]], dtype=float32)
		
		>>> full_image   = torch.Tensor(plt.imread(file_path)).permute(2, 0, 1)
		>>> print(full_image[0])
		tensor([[0.0235, 0.0196, 0.0157,  ..., 0.0510, 0.0588, 0.0549],
		        [0.0235, 0.0196, 0.0157,  ..., 0.0588, 0.0549, 0.0471],
		        [0.0431, 0.0353, 0.0235,  ..., 0.0510, 0.0549, 0.0549],
		        ...,
		        [0.0745, 0.0784, 0.0706,  ..., 0.0431, 0.0431, 0.0431],
		        [0.0745, 0.0784, 0.0784,  ..., 0.0431, 0.0431, 0.0431],
		        [0.0667, 0.0667, 0.0745,  ..., 0.0431, 0.0431, 0.0431]])
		>>> print(full_image[1])
		tensor([[0.0235, 0.0196, 0.0157,  ..., 0.0510, 0.0588, 0.0549],
		        [0.0235, 0.0196, 0.0157,  ..., 0.0549, 0.0549, 0.0471],
		        [0.0431, 0.0353, 0.0235,  ..., 0.0510, 0.0549, 0.0549],
		        ...,
		        [0.0667, 0.0706, 0.0667,  ..., 0.0431, 0.0431, 0.0392],
		        [0.0667, 0.0706, 0.0745,  ..., 0.0431, 0.0431, 0.0392],
		        [0.0588, 0.0627, 0.0706,  ..., 0.0431, 0.0431, 0.0392]])
		>>> print(full_image[2])
		tensor([[0.0235, 0.0196, 0.0157,  ..., 0.0471, 0.0549, 0.0510],
		        [0.0196, 0.0196, 0.0157,  ..., 0.0510, 0.0549, 0.0471],
		        [0.0392, 0.0314, 0.0196,  ..., 0.0471, 0.0510, 0.0510],
		        ...,
		        [0.0627, 0.0667, 0.0588,  ..., 0.0431, 0.0431, 0.0392],
		        [0.0627, 0.0667, 0.0667,  ..., 0.0431, 0.0431, 0.0392],
		        [0.0549, 0.0549, 0.0627,  ..., 0.0431, 0.0431, 0.0392]])
		>>> print(full_image.shape)
		torch.Size([3, 288, 1440])
		>>> torch.max(full_image)
		tensor(1.)
		>>> torch.min(full_image)
		tensor(0.)
		>>>

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/786653.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号