Merging each instance mask back to the original image Python

Question:

I am having a bunch of mask (object is white, non-object is black) bounded by their bounding box as a separate image, and I’m trying to put them back to their original positions on the original image. What I have in mind right now is:

  1. Create a black image of the same size of the original image.
  2. Add the value of each mask with the value of the coordinate of the bounding box on the original image together.

Could anyone tell me if I am heading in the right path, is there any better way to do this?.

Below is roughly my implementation

import cv2
black_img = np.zeros((height,width)) # A image that is of the size of the original but is all black
mask = cv2.imread("mask.png")
bbox = [x1, y1, x2, y2] # Pretend that this is a valid bounding box coordinate on the original image
black_img[y1:y2, x1:x2] += mask

For example:
I have the first image which is one of my masks. Its size is of the same of the bounding box on the original image. I’m trying merge each mask back together so that I achieved something like the second image.

One of the mask:
pred

After merging all the mask:
gt

Asked By: Songg Tùng

||

Answers:

I am assuming the mask is 0 and 1’s and your image is grayscale. Also, for each small_mask, you have a corresponding bbox.

mask = np.zeros((height,width))
for small_mask, bbox in zip(masks, bboxes):
    x1, y1, x2, y2 = bbox
    mask[y1:y2, x1:x2] += small_mask
mask = ((mask>=1)*255.0).astype(np.uint8)

Now you combined all the small masks together.

The last line:
My assumption was somehow two masks may intersect. So those intersection may have values more than 1. mask >= 1 tells me that the pixels that are more than 0 are gonna be all on.

I multiplied that by 255.0 because I wanted to make it white. You won’t be able to see 1’s in a grayscale image.

(mask >= 1)*255.0 expanded the range from [0-1] to [0-255]. But this value is float which is not image type.

.astype(np.uint8) converts the float to uint8. Now you can do all the image operations without any problem. When it is float, you may face a lot of issues, like plotting, saving, all will cause some issues.

Answered By: smttsp