[SOLVED] How to overlay Grayscale Mask on top of RGB image using Numpy and Matplotlib ( opencv or scikit image in case not possible)

Issue

I have 2 images from Carvana Image Dataset where image is `jpg` and mask is `gif`. I have converted the mask as grayscale as 0 or 1 and now want to overlay it over the image to see these 3 original, mask, superimposed side by side using `matplotlib`. What is the right way to do this?

``````from PIL import Image

image = np.array(Image.open(image_path).convert('RGB'))
mask = np.array(Image.open(mask_path).convert('L'), dtype = np.float32) # Mask should be Grayscale so each value is either 0 or 255
mask[mask == 255.0] = 1.0 # whereever there is 255, convert it to 1: (1 == 255 == White)
``````

One way could be:

``````
image = image / 255

plt.imshow(blended)
``````

but it shows only the car and everything else as black

Below are the 2 images

and I want to plot these 3 as:

Solution

There might be a misconception in what you expect.

… but it shows only the car and everything else as black

This is how a binary mask usually operates.

The following selfcontained code (with the images from above saved accordingly) might explain what happens. Note the comments near `blended1 = ...`

``````
from PIL import Image
import numpy as np
from matplotlib import pyplot as plt

image = np.array(Image.open(image_path).convert('RGB'))
mask = np.array(Image.open(mask_path).convert('L'), dtype = np.float32) # Mask should be Grayscale so each value is either 0 or 255
mask[mask == 255.0] = 1.0 # whereever there is 255, convert it to 1: (1 == 255 == White)

print(f"{img.shape=}")  #      -> img.shape=(1280, 1918, 3)

print(f"{mask2.shape=}")  # -> img.shape=(1280, 1918, 3)

# rescale image
img = img /255

# set every pixel to (0, 0, 0) (black) where mask is 0 and
# keep every pixel unchanged where mask is 1
# this is how a mask is usually applied

# set every pixel to (1, 1, 1) (white) where mask is 1 and
# keep every pixel unchanged where mask is 0

fig, axx = plt.subplots(1, 4, figsize=(8, 18))

for ax, arr, title in zip(axx,