繁体   English   中英

替换Matplot图像中的像素

[英]Replacing pixels in matplot image

我有两个图像作为NDArray

  1. 原始图片
  2. 预测蒙版。

例如,我想“遮盖”面具不是6的区域

为了简单起见,我将它们粘贴为3x3的小图像,每个像元都是像素的RGB值。

原版的

[
    [1,1,1], [1,5,1], [1,1,1]
    [3,3,3], [3,3,3], [3,3,3]
    [1,1,1], [5,2,1], [1,1,1]
]

预测

[
    [0, 0, 0]
    [6, 6, 6]
    [1, 2, 3]
]

为此,我只是遍历预测并用[0,0,0]替换原始单元格,以清除不需要的单元格

for rowIndex, predictedPointRow in enumerate(predict):
    for colIndex, predPoint in enumerate(predictedPointRow):
        if predPoint is not 6:
            img[rowIndex][colIndex] = [0, 0, 0]

这是痛苦的缓慢但是。 有更好的方法吗?

谢谢,

你可以做类似的事情

img = np.array([[[1,1,1], [1,5,1], [1,1,1]],[[3,3,3], [3,3,3], [3,3,3]],[[1,1,1], [5,2,1], [1,1,1]]])
predict = np.array([[0,0,0],[6,6,6],[1,2,3]])
img[predict!=6] = [0,0,0]

您可以使用布尔值或“掩码”索引数组

mask = (predict != 6)   # create a 2D boolean array, which can be used for indexing
img[mask] = [0,0,0]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM