简体   繁体   English

使用 MaskRcnn 从 model.detect() 之后的图像中获取预测掩码的 x,y 坐标

[英]Getting the x,y coordinates of the predicted masks from the images after model.detect() with MaskRcnn

How to extract the x,y coordinates of the polygons from the predicted masks with Mask Rcnn after detection.如何在检测后使用 Mask Rcnn 从预测的掩码中提取多边形的 x,y 坐标。 My implementation for object detection is this我对 object 检测的实现是这样的

results = model.detect([image], verbose=1)

I can get the ROIs, masks,class_ids,class names and confidence scores with the following code.我可以使用以下代码获取 ROI、掩码、class_ids、class 名称和置信度分数。 But I need to get the x, y coordinate values of each polygon of the predicted masks.但我需要获取预测掩码的每个多边形的 x、y 坐标值。

ax = get_ax(1) r = results[0] visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'], dataset.class_names, r['scores'], ax=ax)

I have found written code in display_instances() function in visualize.py file.我在 visual.py 文件中的 display_instances() function 中找到了书面代码。 I have made modification on this code and made separate function which is below:我已对此代码进行了修改,并单独制作了 function,如下所示:

from skimage.measure import find_contours
import numpy as np


def save_co_ordinates(image, boxes, masks, class_ids, class_names):
    image = image.split("/")[-1]
    image_data = []

    for i in range(boxes.shape[0]):

        class_id = class_ids[i]
        label = class_names[class_id]

        mask = masks[:, :, i]
        padded_mask = np.zeros(
            (mask.shape[0] + 2, mask.shape[1] + 2), dtype=np.uint8)
        padded_mask[1:-1, 1:-1] = mask
        contours = find_contours(padded_mask, 0.5)
        for verts in contours:
            verts = np.fliplr(verts) - 1
            list_co_ordinates = np.moveaxis(verts, 1, 0).tolist()

            region = {"shape_attributes": {"all_points_x": list_co_ordinates[0],
                                           "all_points_y": list_co_ordinates[1]},
                      "region_attributes": {"name": {label: True}}}
            image_data.append(region)
    data = {"filename": image, "regions": image_data}
    return data

save_co_ordinates("C:/path/apple.jpg", r1['rois'], r1['masks'], r1['class_ids'], get_detections())

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

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