如何使用 Numpy 和 Matplotlib(opencv 或 scikit 图像,以防不可能)在 RGB 图像之上叠加灰度蒙版
How to overlay Grayscale Mask on top of RGB image using Numpy and Matplotlib ( opencv or scikit image in case not possible)
我有 2 张来自 Carvana Image Dataset 的图像,其中图像为 jpg
,遮罩为 gif
。我已将蒙版转换为 0 或 1 的灰度,现在想将其覆盖在图像上以查看这 3 个原始蒙版,使用 matplotlib
并排叠加。正确的做法是什么?
from PIL import Image
def get_pair(image_path, mask_path):
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)
return image, mask
一种方法可以是:
image, mask = data[0]
image = image / 255
mask = np.stack((mask,)*3, axis=-1)
blended = image * mask
plt.imshow(blended)
但它只显示汽车和其他所有东西都是黑色的
下面是2张图片
我想将这 3 个绘制为:
您的期望可能有误。
... but it shows only the car and everything else as black
二进制掩码通常是这样操作的。
以下自包含代码(相应地保存了上面的图像)可能会解释发生了什么。注意 blended1 = ...
附近的评论
from PIL import Image
import numpy as np
from matplotlib import pyplot as plt
def get_pair(image_path, mask_path):
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)
return image, mask
img, mask = get_pair("img.jpg", "mask.gif")
print(f"{img.shape=}") # -> img.shape=(1280, 1918, 3)
print(f"{mask.shape=}") # -> img.shape=(1280, 1918)
mask2 = np.stack((mask,)*3, axis=-1)
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
blended1 = img*mask2
# set every pixel to (1, 1, 1) (white) where mask is 1 and
# keep every pixel unchanged where mask is 0
blended2 = np.clip(img+mask2, 0, 1)
fig, axx = plt.subplots(1, 4, figsize=(8, 18))
for ax, arr, title in zip(axx,
[img, mask, blended1, blended2],
["original", "mask", "blended1", "blended2"]):
ax.imshow(arr)
ax.axis("off")
ax.set_title(title)
plt.show()
生成的图像:
我有 2 张来自 Carvana Image Dataset 的图像,其中图像为 jpg
,遮罩为 gif
。我已将蒙版转换为 0 或 1 的灰度,现在想将其覆盖在图像上以查看这 3 个原始蒙版,使用 matplotlib
并排叠加。正确的做法是什么?
from PIL import Image
def get_pair(image_path, mask_path):
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)
return image, mask
一种方法可以是:
image, mask = data[0]
image = image / 255
mask = np.stack((mask,)*3, axis=-1)
blended = image * mask
plt.imshow(blended)
但它只显示汽车和其他所有东西都是黑色的
下面是2张图片
我想将这 3 个绘制为:
您的期望可能有误。
... but it shows only the car and everything else as black
二进制掩码通常是这样操作的。
以下自包含代码(相应地保存了上面的图像)可能会解释发生了什么。注意 blended1 = ...
from PIL import Image
import numpy as np
from matplotlib import pyplot as plt
def get_pair(image_path, mask_path):
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)
return image, mask
img, mask = get_pair("img.jpg", "mask.gif")
print(f"{img.shape=}") # -> img.shape=(1280, 1918, 3)
print(f"{mask.shape=}") # -> img.shape=(1280, 1918)
mask2 = np.stack((mask,)*3, axis=-1)
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
blended1 = img*mask2
# set every pixel to (1, 1, 1) (white) where mask is 1 and
# keep every pixel unchanged where mask is 0
blended2 = np.clip(img+mask2, 0, 1)
fig, axx = plt.subplots(1, 4, figsize=(8, 18))
for ax, arr, title in zip(axx,
[img, mask, blended1, blended2],
["original", "mask", "blended1", "blended2"]):
ax.imshow(arr)
ax.axis("off")
ax.set_title(title)
plt.show()
生成的图像: