【问题标题】:Convert an rgb mask image to coco json polygon format将 rgb 蒙版图像转换为 coco json 多边形格式
【发布时间】:2019-07-15 01:44:22
【问题描述】:

我使用此处提供的 PixelAnnotationTool 注释图像:https://github.com/abreheret/PixelAnnotationTool 并使用提供的字典:

{
    "labels": {
        "unlabeled": {
            "categorie": "void",
            "color": [
                0,
                0,
                0
            ],
            "id": 0,
            "id_categorie": 0,
            "name": "unlabeled"
        },
        "bicycle_motorcycle": {
            "categorie": "bicycle_motorcycle",
            "color": [
                119,
                11,
                32
            ],
            "id": 1,
            "id_categorie": 1,
            "name": "bicycle_motorcycle"
        },
        "bus": {
            "categorie": "bus",
            "color": [
                102,
                51,
                0
            ],
            "id": 2,
            "id_categorie": 2,
            "name": "bus"
        },

.... }

我想将这些 RGB 掩码转换为 json 多边形格式,以便我可以在 Mask R-CNN 中使用它们。这个怎么做?

【问题讨论】:

  • 确定需要提供图片吗?

标签: python image image-processing deep-learning mask


【解决方案1】:

这是一个 python 函数,它将接受一个蒙版 Image 对象并返回一个子蒙版字典,以 RGB 颜色为键。

from PIL import Image # (pip install Pillow)

def create_sub_masks(mask_image):
    width, height = mask_image.size

    # Initialize a dictionary of sub-masks indexed by RGB colors
    sub_masks = {}
    for x in range(width):
        for y in range(height):
            # Get the RGB values of the pixel
            pixel = mask_image.getpixel((x,y))[:3]

            # If the pixel is not black...
            if pixel != (0, 0, 0):
                # Check to see if we've created a sub-mask...
                pixel_str = str(pixel)
                sub_mask = sub_masks.get(pixel_str)
                if sub_mask is None:
                   # Create a sub-mask (one bit per pixel) and add to the dictionary
                    # Note: we add 1 pixel of padding in each direction
                    # because the contours module doesn't handle cases
                    # where pixels bleed to the edge of the image
                    sub_masks[pixel_str] = Image.new('1', (width+2, height+2))

                # Set the pixel value to 1 (default is 0), accounting for padding
                sub_masks[pixel_str].putpixel((x+1, y+1), 1)

    return sub_masks

一旦你有了面具,你就可以使用imantics将它转换成COCO

【讨论】:

  • 我应该如何使用 imantics 将 sub_masks 转换为 coco?似乎无法在 repo 上找到示例或文档。提前致谢
猜你喜欢
  • 2021-06-23
  • 2021-12-06
  • 1970-01-01
  • 2021-09-06
  • 2023-03-29
  • 2020-02-29
  • 2021-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多