将rgb掩码图像转换为coco-json多边形形式

2024-06-16 12:12:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用此处提供的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中使用它们。怎么做?在


Tags: namehttps图像githubcomid字典color
1条回答
网友
1楼 · 发布于 2024-06-16 12:12:21

下面是一个python函数,它接收一个mask Image对象并返回一个由RGB color键控的子掩码字典。在

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

相关问题 更多 >