Discussions>Get binary mask for every object in instance segmentation>

Get binary mask for every object in instance segmentation

I'm doing instance segmentation and want to get a binary mask for every object. Every bounding box contains multiple objects with different colors.

How to get a binary mask for every bounding box, which corresponds to the object? 
Here are the examples.


3 votesJO297.00
2 Answers

There is another way to do that.
You can compute real, ground truth bounding boxes for every object by using each color (each color object is inside of it's bounding box). After that, when you detect a bounding box, search through all ground truth bounding boxes and calculate IoU between them.
Take the color of the bounding box, which has the maximum IoU and generate binary mask using that particular color.


You can take only the one, which has the biggest area. You can split every color by channels and compute the maximum area. It will be something like this

import cv2
import numpy as np

mask = cv2.imread(mask_path)
# Let's find unique colors of the mask
unique_colors = np.unique(mask.reshape(-1, mask.shape[2]), axis=0)
masks = np.zeros(mask.shape)

for color in unique_colors:
    object_mask = np.where(mask == color, 1, 0)
    object_mask = np.prod(object_mask, axis=-1)
    masks = np.dstack([object_mask, masks])

Now every channel of the mask will corresponde to a binary mask of each object (color). After this you will have to compute the max area.

Remember our first channel contains black mask because of initialization, also first color of unique_colors could have been black (0, 0, 0).

masks = masks[:, :, 2:]

# Because each pixel is 1 or 0, the area can be computed like this
areas = np.sum(masks, axis=(0, 1))
max_area_arg = np.argmax(areas)
mask = masks[:, :, max_area_arg]  # This is the mask you want
Couldn't find what you were looking for?and we will find an expert to answer.
How helpful was this page?