Numpy/Scipy with masks and RGB images

前端 未结 1 1089
广开言路
广开言路 2021-01-03 15:31

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

相关标签:
1条回答
  • 2021-01-03 15:56

    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)
    
    0 讨论(0)
提交回复
热议问题