简体   繁体   English

Numpy / Scipy带有蒙版和RGB图像

[英]Numpy/Scipy with masks and RGB images

I'm trying to create a mask for an RGB image using skikit learn. 我正在尝试使用skikit learn为RGB图像创建一个蒙版。 I want to create a mask selecting only pixels which are equal to [0,10,0], ie 10 on green channel. 我想创建一个掩码,只选择等于[0,10,0]的像素,即绿色通道上的10。 And then show only those pixels. 然后只显示那些像素。 This should be straight-forward, akin to http://scikit-image.org/docs/dev/user_guide/numpy_images.html , but I'm struggling. 这应该是直截了当的,类似于http://scikit-image.org/docs/dev/user_guide/numpy_images.html ,但我很挣扎。

If image is a loaded jpg, I can do 如果图像是一个加载的jpg,我可以做

mask = image == [0,10,0]
image = image[mask]
viewer = ImageViewer(image)
viewer.show()

However, I get: 但是,我得到:

TypeError: Invalid dimensions for image data

If I then print(mask), I realise that instead of a series of True and False for each pixel, I'm getting: 如果我然后打印(蒙版),我意识到,对于每个像素而不是一系列的真和假,我得到:

[[ True False  True]
  [ True False  True]
  [ True False  True]
  ..., 
  [ True False  True]
  [ True False  True]
  [ True False  True]]

Note that the 1st pixel in my image is black. 请注意,我图像中的第一个像素是黑色的。 So it appears what it's doing is comparing [0,0,0] with [0,10,0], and instead of raising False, it raises True,False,True. 所以看起来它正在做的是将[0,0,0]与[0,10,0]进行比较,而不是提高False,它会提升True,False,True。

This then appears to fail as a mask, because I have 3 masks for each pixel, I suppose! 然后这似乎作为掩码失败,因为我想每个像素有3个掩码!

Does anyone know a simple way to get this masking working with RGB images? 有没有人知道一种简单的方法来使用RGB图像进行掩模处理?

Thanks 谢谢

You could get that 2D mask with ALL reduction along the last axis - 您可以沿着最后一个轴获得所有减少的2D蒙版 -

mask = (image == [0,10,0]).all(-1)

Then, image[mask] would be (N,3) shaped array of only [0,10,0] values, where N is number of pixels which were of that specific RGB triplet. 然后, image[mask]将是仅仅[0,10,0]值的(N,3)形阵列,其中N是该特定RGB三元组的像素数。

So, the step(s) to using mask to show the masked image or overlay would depend on the viewer. 因此,使用mask来显示蒙版图像或叠加的步骤将取决于观看者。


For an in-situ edit in the image, such that we would mask out everything that's not of that specific RGB triplet, we could multiply with the mask - 对于图像中的原位编辑,这样我们可以掩盖不属于特定RGB三元组的所有内容,我们可以使用掩码 -

image *= mask[...,None]

Or create a copy with a choosing mechanism using np.where - 或者使用np.where创建一个带有选择机制的np.where -

image_overlayed = np.where(mask[...,None], image, 0)

To get a 3D mask (if that's what needed with viewer), we could replicate the mask along the channels as well - 要获得3D蒙版(如果这是观众所需要的),我们也可以沿着通道复制蒙版 -

np.repeat(mask[...,None],3,axis=2)

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

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