将RGB面膜转换为JSON for Mask R CNN

发布于 2025-02-06 20:39:19 字数 3028 浏览 2 评论 0原文

我有这个面具图像。

在此图像中,我想创建一个包含不同类的多边形信息的JSON文件。这里不同的颜色表示不同的类。

从此图像中,我想创建一个相应的JSON文件。我需要帮助。

我已经从此图像中计算了子掩码。我也从Stackoverflow那里获得了帮助。

代码如下:

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

因此,在此之后,我要写:

img = Image.open('sample.jpg')
im2 = create_sub_masks(img)

我想要一个JSON文件,其中:

{
  "version": "5.0.1",
  "flags": {},
  "shapes": [
    {
      "label": "window",
      "points": [
        [
          7.132653061224492,
          62.755102040816325
        ],
        [
          16.316326530612244,
          62.244897959183675
        ],
        [
          31.112244897959187,
          63.26530612244898
        ],
        [
          30.091836734693878,
          89.79591836734694
        ],
        [
          17.846938775510203,
          90.05102040816327
        ],
        [
          6.622448979591837,
          89.54081632653062
        ]
      ],
      "group_id": null,
      "shape_type": "polygon",
      "flags": {}
    },
        "label": "window",
      "points": [
        [
          7.132653061224492,
          62.755102040816325
        ],
        [
          16.316326530612244,
          62.244897959183675
        ],
        [
          31.112244897959187,
          63.26530612244898
        ],
        [
          30.091836734693878,
          89.79591836734694
        ],
        [
          17.846938775510203,
          90.05102040816327
        ],
        [
          6.622448979591837,
          89.54081632653062
        ]
      ],
      "group_id": null,
      "shape_type": "polygon",
      "flags": {}
    },
    ...........

在此JSON文件中,将根据我给定的颜色列表创建标签。

I have this mask image.

enter image description here

From this image, I want to create a JSON file containing the polygon information of different classes. Here different color indicates different classes.

From this image, I want to create a corresponding JSON file. I need help.

I have calculated the sub mask from this image. I took help from the stackoverflow for that as well.

The code is below:

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

So after this I am writing:

img = Image.open('sample.jpg')
im2 = create_sub_masks(img)

I want a JSON file where:

{
  "version": "5.0.1",
  "flags": {},
  "shapes": [
    {
      "label": "window",
      "points": [
        [
          7.132653061224492,
          62.755102040816325
        ],
        [
          16.316326530612244,
          62.244897959183675
        ],
        [
          31.112244897959187,
          63.26530612244898
        ],
        [
          30.091836734693878,
          89.79591836734694
        ],
        [
          17.846938775510203,
          90.05102040816327
        ],
        [
          6.622448979591837,
          89.54081632653062
        ]
      ],
      "group_id": null,
      "shape_type": "polygon",
      "flags": {}
    },
        "label": "window",
      "points": [
        [
          7.132653061224492,
          62.755102040816325
        ],
        [
          16.316326530612244,
          62.244897959183675
        ],
        [
          31.112244897959187,
          63.26530612244898
        ],
        [
          30.091836734693878,
          89.79591836734694
        ],
        [
          17.846938775510203,
          90.05102040816327
        ],
        [
          6.622448979591837,
          89.54081632653062
        ]
      ],
      "group_id": null,
      "shape_type": "polygon",
      "flags": {}
    },
    ...........

In this JSON file, the labels will be created based on my given colour list.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文