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.