I\'m trying to create a mask for an RGB image using skikit learn. I want to create a mask selecting only pixels which are equal to [0,10,0], ie 10 on green channel. And then
You could get that 2D
mask with ALL reduction along the last axis -
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.
So, the step(s) to using mask
to show the masked image or overlay would depend on the viewer.
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 -
image *= mask[...,None]
Or create a copy with a choosing mechanism using 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 -
np.repeat(mask[...,None],3,axis=2)