Applying a 2D numpy mask to grayscale image

Viewed 3811

I have a 256×256 px 2D mask with pixel values of either 0 or 255, and a matching grayscale image. Both have shapes (256, 256).

I'd like to apply the mask on the grayscale image so the resulting image only contains pixels from the original grayscale image, where corresponding mask values are 255 (the original grayscale pixel values should be preserved).

I'm not being able to do this using

img_clean = img_grayscale[mask]

as it adds a new dimension to the array (as mentioned in numpy's docs).

How do I resolve this?

3 Answers
Related