【问题标题】:How to draw rectangles on a binary image?如何在二进制图像上绘制矩形?
【发布时间】:2020-08-26 17:01:28
【问题描述】:

我正在尝试使用PIL.ImageDraw 在二进制图像上绘制矩形。这就是我目前所拥有的:

npim = f #binary src image (np array)
plt.imshow(npim)

plt.imshow(np.array(npim))
pp = Image.fromarray(npim,'1') #PIL object to draw on

plt.figure()
plt.imshow(np.array(pp))

ix = ImageDraw.Draw(pp)
ix.rectangle([(0,0),(100,100)],outline=1) #draw rectangle of 1's on the image

plt.figure()
plt.imshow(np.array(pp))

这三个图的结果:

如何在此二进制图像上使用PIL.ImageDraw 绘制矩形,同时保留其下方的图像?似乎当我将数组转换回 Image 对象时,图像没有被保留。

转换为 RGB 和灰度同样失败,它们可能会显示矩形但背景中没有图像。

编辑:这是原始图像,而不是 Matplotlib 图。

【问题讨论】:

  • 请显示您的实际输入和输出图像,而不是带有边框和刻度线的 matplotlib 印象。谢谢。
  • @MarkSetchell 我已经包含了原始输入图像

标签: python image-processing python-imaging-library


【解决方案1】:

这里的陷阱是image mode 1的用法:

pp = Image.fromarray(npim, '1')

您的输入图像不是真正的“二值图像”,而是通常的 8 位灰度图像。所以,只要坚持对应的image mode L,你就会得到预期的输出:

pp = Image.fromarray(npim, 'L')

实际上,如果npim已经存储为单通道uint8数组,你可以完全省略图像模式,它仍然有效:

pp = Image.fromarray(npim)

关于图像模式1的一些想法,也许也可以看看this earlier answer from me。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-22
    • 1970-01-01
    • 1970-01-01
    • 2012-08-28
    • 2020-05-08
    • 2018-09-13
    • 1970-01-01
    相关资源
    最近更新 更多