【问题标题】:How to expand and fill third dim of black and white image如何扩展和填充黑白图像的第三个暗淡
【发布时间】:2019-09-16 17:00:19
【问题描述】:

我有一个 (224,224) 形状的黑白图像,但我想要 (224,224,3),所以我需要扩展暗淡,但不能使用空值,所以 np.expand_dimsnp.atleast_3d 不能帮不了我。我怎样才能正确地做到这一点?谢谢。

我用什么:

from PIL import Image
img = Image.open('data/'+link)
rsize = img.resize((224,224))
rsizeArr = np.asarray(rsize)

【问题讨论】:

  • 您想在额外维度中添加什么?如果你想提供均匀间隔的 xy 值,那么我认为 np.meshgrid 就是你要找的。​​span>

标签: python numpy computer-vision python-imaging-library rgb


【解决方案1】:

当我们使用numpy.dstack() 时,我们不必手动扩展维度,它会处理这项工作并将其沿第三轴堆叠,这正是我们想要的。

In [4]: grayscale = np.random.random_sample((224,224))

# make it RGB by stacking the grayscale image along depth dimension 3 times
In [5]: rgb = np.dstack([grayscale]*3)

In [6]: rgb.shape
Out[6]: (224, 224, 3)

对于您的具体情况,应该是:

rsize_rgb = np.dstack([rsize]*3)

无论出于何种原因,如果您仍想将灰度图像的尺寸扩展 1,然后使其成为 RGB 图像,则可以使用numpy.concatenate(),如下所示:

In [9]: rgb = np.concatenate([grayscale[..., np.newaxis]]*3, axis=2)
In [10]: rgb.shape
Out[10]: (224, 224, 3)

对于您的具体情况,它将是:

rsize_rgb = np.concatenate([rsize[..., np.newaxis]]*3, axis=2)

【讨论】:

    猜你喜欢
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    • 2016-06-25
    • 1970-01-01
    • 2019-03-15
    相关资源
    最近更新 更多