简体   繁体   中英

Extracting a binary boundary image for object of interest in a binary object mask image extracted from an instance segmentation mask

I have an instance segmentation + 2D bounding box structure from Detectron 2 from which I converted the pred_masks into a binary object (of interest) mask.

So, here, my question is how can I convert this binary mask to a binary image wherein the entire image is black but the boundary around the object of interest in the object mask is white?

segmenter = get_pointrend_predictor()
instances = segmenter(image)["instances"]
vis = PointRendVisualizer(image, metadata=MetadataCatalog.get("coco_2017_val"))
Image.fromarray(vis.draw_instance_predictions(instances.to("cpu")).get_image())

在此处输入图片说明

instances 2 .pred_masks.shape

torch.Size([1, 224, 400])
na = instances[1].pred_masks.to('cpu').numpy()
print(na.shape)

(1, 224, 400)

na = na.reshape(224, 400)
na.shape

(224, 400)

na = np.where(na == False, 0, na)
na = np.where(na == True, 255, na)
plt.imshow(na)

在此处输入图片说明

In this specific example, I am interested in drawing a white line on the boundary of the baby elephant (which is my second instance in the instance segmentation mask object).

Unfortunately, I don't have a drawing of the boundary for the baby elephant but here's an example for the boundary of human (shown in white line): 在此处输入图片说明 ^ image reference: https://www.programmersought.com/article/52814639867/

I just figure out the way to extract edges of a bit mask.

As those depicted in the picture: 在此处输入图片说明

na = outputs['instances'][1].pred_masks.to('cpu').numpy()
na = na.reshape(1024, 683)

from detectron2.utils.visualizer import GenericMask
gm = GenericMask(na, 1024, 683)
sg = gm.polygons[0].reshape(-1,2)
print(sg)

By the code I got a list of polygon edges. It's a mask for a balloon.

To verify it, I translated the polygon into JavaScript, as in the following picture, and ran it. Obviously it works.

在此处输入图片说明

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM