Skip to content
Advertisement

Converting Detectron2 instance segmentation to opencv Mat array

I am trying to get a binary image from the instance segmentation output performed using Detectron2. According to the official documentation the mask’s output format is the following:

“pred_masks”: a Tensor of shape (N, H, W), masks for each detected instance.

So i tried converting it to numpy: mask = outputs["instances"].get("pred_masks").numpy() The output was the following:

JavaScript

However the data type was boolean so i added the following line to get closer to the opencv format: array = (mask > 126) * 255

JavaScript

And that is as far as I went to. I would like to be able to visualize each mask individually as an opencv image: cv2.imshow("Mask", mask) , without having to save the image.

What I would like to achieve

Thank you in advance.

Advertisement

Answer

I hope this will give a solution for what you expecting

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement