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



