【问题标题】:Use PIL to convert a grayscale image to a (1, H, W) numpy array使用 PIL 将灰度图像转换为 (1, H, W) numpy 数组
【发布时间】:2017-12-01 16:23:49
【问题描述】:

使用 PIL 将 RGB 图像转换为 (H, W, 3) numpy 数组非常快。

im = np.array(PIL.open(path))

但是,我找不到将灰度图像转换为 (H, W, 1) 数组的快速方法。我尝试了两种方法,但它们都比上面慢得多:

im = np.array(PIL.open(path)) # return an (H, W) array
im = np.expand_dims(im, axis=0)
im = im.astype(int)

这种方法也很慢:

img = PIL.open(path)
im = np.array(img.getdata()).reshape(img.size[1], img.size[0], 1)

请指教...

【问题讨论】:

  • 使用int dtype 作为最终dtype 而不是uint8 或类似的有什么特殊原因吗?如果降低精度/无符号 dtype 没问题,我们可以优化很多。
  • @Divakar 感谢您的回答和帮助。灰度文件的数据类型(值)为int(0~255)...
  • 听起来像uint8。所以,我建议在这种情况下使用dtype=np.uint8
  • 另外,np.expand_dims(im, axis=0) 在开头附加新轴,所以它的(1,H,W),而第二个代码在末尾这样做:(H,W,1)。哪个是正确的?
  • (1, H, W) 正是我想要的(只需编辑标题)。

标签: python numpy python-imaging-library


【解决方案1】:

您可以使用np.asarray() 获取数组视图,然后使用None/np.newaxis 附加一个新轴,然后使用类型转换将copy 设置为False(以防您从相同的dtype 转换以保存记忆)-

im = np.asarray(PIL.open(path))
im_out = im[None].astype(dtype=int, copy=False)

这会在开头附加新轴,从而将(1,H,W) 作为输出数组形状。为此,为了最终获得(H,W,1) 的数组形状,请执行以下操作:im[...,None] 而不是im[None]


更简单的方法是 -

im_out = np.asarray(img, dtype=int)[None]

如果输入已经在uint8 dtype 中,并且我们想要一个相同dtype 的输出数组,请使用dtype=np.uint8,这应该很快。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-15
    • 2017-06-25
    • 2018-12-19
    • 2021-04-02
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 2010-09-27
    相关资源
    最近更新 更多