将RGB蒙版图像转换为可可JSON多边形格式 [英] Convert an rgb mask image to coco json polygon format
本文介绍了将RGB蒙版图像转换为可可JSON多边形格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用此处提供的PixelAnnotationTool注释了图像: https://github.com/abreheret/PixelAnnotationTool 并使用提供的字典:
I annotated images using PixelAnnotationTool provided here: https://github.com/abreheret/PixelAnnotationTool and using the provided dictionary:
{
"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中使用它们.这该怎么做?
I want to convert these RGB masks into json polygon format so that I can use them in Mask R-CNN. How to do this?
推荐答案
这是一个python函数,它将接收Mask Image对象,并返回以RGB颜色为键的子掩码字典.
Here's a python function that will take in a mask Image object and return a dictionary of sub-masks, keyed by 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
Once you have the masks you can use imantics to convert it to COCO
这篇关于将RGB蒙版图像转换为可可JSON多边形格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文