Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中显示原始图像像素而不是遮罩_Python_Python 3.x_Numpy_Matplotlib_Python Imaging Library - Fatal编程技术网

在python中显示原始图像像素而不是遮罩

在python中显示原始图像像素而不是遮罩,python,python-3.x,numpy,matplotlib,python-imaging-library,Python,Python 3.x,Numpy,Matplotlib,Python Imaging Library,我有一个深入的学习模型,当绘制成这样时,它会返回给我一个数组 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]) (上面的最后一行只是在打印

我有一个深入的学习模型,当绘制成这样时,它会返回给我一个数组

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

我想在出现遮罩的原始图像中显示彩色像素,并将所有其他颜色都变为黑色(或模糊或其他颜色,我将选择),如何做到这一点?

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

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

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

我能够逆转这一点,实现我想要的结果

mask=labels[:-pad_x]==0
resizedOrig=cv2。调整大小(帧,(512384))

调整大小[面具]=0

你的问题是什么?请检查帖子的结尾行?我仍然不明白,所以你看到有一个黄色的狗和猫面具,而不是黄色的。我需要显示真实的狗和猫,希望它有意义?现在我明白了是的,标签数组在面具所在的位置包含大于0的值。我尝试了您的解决方案
newImage=np.zeros(img.shape)newImage[mask]=img[mask]
但我得到了以下错误
indexer:布尔索引与维度0上的索引数组不匹配;维度是358,但对应的布尔维度是512,其中img是
img=plt.imread(“imgs/image2.jpg”)
当我检查时,它们都有相同的维度
print(img.shape,newImage.shape)
(358500,3)(358500,3)
请稍候,我实际上是在尝试在原始图像上创建一个掩码,而不是在调整大小的图像上创建一个掩码,所以在尝试了
newImage=np.zeros(resized2.shape)newImage[mask]=resized2[mask]
之后,我通过一条消息
将输入数据剪裁到imshow的有效范围,其中包含RGB数据([0..1]表示浮点数或[0..255]表示整数)