OSU-NLP-Group/MagicBrush

HOw to get binary mask?

shileims opened this issue · 2 comments

Hi Author,
How to get binary mask from the dataset? I think the dataset provides the masked image, not binary mask. And also, mask may have different resolutions from images. Therefore, it is difficult to extract the binary mask. Would you publish the original binary mask? Thanks

Hi

Thanks for your interest! I don't think binary mask is widely needed so far. We released the original masked images for providing more image information.

You can easily transform them into binary via loading, resizing to the original image, and transforming it into binary mask via this code:

# Convert RGBA image to 0-1 mask, used in mask-guided image editing models.
def rgba_to_01_mask(image_rgba: Image.Image, reverse: bool = False, return_type: str = "numpy"):
    """
    Convert an RGBA image to a binary mask with values in the range [0, 1], where 0 represents transparent areas
    and 1 represents non-transparent areas. The resulting mask has a shape of (1, H, W).

    :param image_rgba: An RGBA image in PIL format.
    :param reverse: If True, reverse the mask, making transparent areas 1 and non-transparent areas 0.
    :param return_type: Specifies the return type. "numpy" returns a NumPy array, "PIL" returns a PIL Image.

    :return: The binary mask as a NumPy array or a PIL Image in RGB format.
    """
    alpha_channel = np.array(image_rgba)[:, :, 3]
    image_bw = (alpha_channel != 255).astype(np.uint8)
    if reverse:
        image_bw = 1 - image_bw
    mask = image_bw[np.newaxis, :, :]
    if return_type == "numpy":
        return mask
    else: # return PIL image
        mat = np.reshape(mask,(mask.shape[1],mask.shape[2]))
        mask = Image.fromarray(np.uint8(mat * 255) , 'L').convert('RGB')
        return mask

Hope it helps!

Best,
Kai

Close for inactivity for now, feel free to re-open it if you have any further questions.