【问题标题】:Show original image pixels instead of mask in python在python中显示原始图像像素而不是掩码
【发布时间】:2018-12-22 11:33:05
【问题描述】:

我有一个深度学习模型,它返回给我一个数组,当它像这样绘制时

res = deeplab_model.predict(np.expand_dims(resized2,0))
labels = np.argmax(res.squeeze(),-1) #remove single dimension values, gives the indices of maximum values in the array  
plt.imshow(labels[:-pad_x])

(上面的最后一行只是在绘制之前删除了一些不清楚的线)

看起来像这样

原图是这样的

当我做的时候

print(labels[labels>0])
print(labels.shape)
print(len(labels))

我明白了

[12 12 12 ... 12 12 12]
(512, 512)
512

我想在原始图像中显示蒙版出现的彩色像素并将其他所有内容变为黑色(或模糊或我将选择的其他颜色),我该怎么做?

【问题讨论】:

  • 你有什么问题?
  • 请检查帖子的最后几行?
  • 我还是不明白
  • 所以你看到有一个黄色的狗和猫的面具而不是那个黄色我需要展示真正的狗和猫希望它有意义吗?
  • 现在我明白了

标签: python python-3.x numpy matplotlib python-imaging-library


【解决方案1】:

这里的标签数组是如何工作的并不完全清楚。假设它在猫和狗所在的位置包含大于零的值,您可以使用以下内容创建蒙版图像,

mask = lables > 0
newimage = np.zeros(image.shape)
newimage[mask] = image[mask]

我根据原始图像创建了一个零图像,并将原始像素设置为标签大于零的位置。

【讨论】:

  • 是的,标签数组在掩码所在的位置包含大于 0 的值。我试过你的解决方案newImage = np.zeros(img.shape) newImage[mask] = img[mask],但我得到这个错误IndexError: boolean index did not match indexed array along dimension 0; dimension is 358 but corresponding boolean dimension is 512 where img is img = plt.imread("imgs/image2.jpg") 当我检查它们都有相同的尺寸print(img.shape, newImage.shape)(358, 500, 3) (358, 500, 3)
  • 等一下,我实际上是在尝试在原始图像上创建一个蒙版而不是调整大小的蒙版,所以在尝试newImage = np.zeros(resized2.shape) newImage[mask] = resized2[mask] 之后,我得到了这张图像imgur.com/a/Q3eszkJ 并带有一条消息Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers)
【解决方案2】:

我能够扭转这种局面并实现我想要的目标

mask = labels[:-pad_x] == 0 resizedOrig = cv2.resize(frame, (512,384)) resizedOrig[mask] = 0

【讨论】:

    猜你喜欢
    • 2022-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多