简体   繁体   English

在从实例分割掩码中提取的二值对象掩码图像中提取感兴趣对象的二值边界图像

[英]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.我有一个来自Detectron 2的实例分割 + 2D 边界框结构,我从中将 pred_masks 转换为二进制对象(感兴趣的)掩码。

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实例2 .pred_masks.shape

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

(1, 224, 400) (1, 224, 400)

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

(224, 400) (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/ ^ 图片参考: 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.为了验证它,我将多边形翻译成 JavaScript,如下图所示,然后运行它。 Obviously it works.显然它有效。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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