简体   繁体   中英

How to display a ground truth image segmentation mask image in python?

I tried this piece of code

from skimage import io
temp = io.imread(mask_input_path)
plt.imshow(temp)

This displays it a normal image, hence the output is black.

图像绘图

Just like a normal image. If your entire mask is black that means desired object is not present in your image.

But to select only masked area you need 2 extra lines on code

import matplotlib.pyplot as plt
input_img = plt.imread('img.jpg')
mask_img  = plt.imread('mask.jpg')

# select only masked area below
masked = input_img.copy()
masked[mask_img == 0 ] = 0

fig, axes = plt.subplots(1, 3, figsize=(16, 12))
ax = axes.flatten()

ax[0].imshow(input_img, cmap="gray")
ax[0].set_axis_off()
ax[0].set_title("Original Imput Image", fontsize=12)

ax[1].imshow(mask_img, cmap="gray")
ax[1].set_axis_off()
ax[1].set_title("Mask", fontsize=12)

ax[2].imshow(masked, cmap="gray")
ax[2].set_axis_off()
ax[2].set_title("Masked", fontsize=12)

plt.show()

在此处输入图片说明

Actually using

masked[mask_img < 30 ] = 0

gives slightly better results because mask values are not exactly zero in my case

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM