Skip to content

bug: polygon notation from mask  #48

@ghost

Description

the algorithm that is used to create the polygon notation from a mask sometimes creates single-point polygons.
When using pycocotools' annToMask, this throws an exception:

import imantics as imt
import numpy as np
from pycocotools.coco import COCO
import json

mask = [[0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 0, 0], [0, 1, 1, 1, 0, 0], [1, 1, 1, 0, 0, 0], [1, 1, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0]]
mask = np.array(mask, dtype='uint8')

dataset = imt.Dataset('dataset')
catt = imt.Category('dummy', id=1)

maskk = imt.Mask(mask)

img = np.reshape(np.array([mask, mask, mask]), [6, 6, 3])
imgg = imt.Image(img, path='hello/world.png')
dataset.add(imgg)

ann = imt.Annotation.from_mask(maskk, image=imgg, category=catt)

dataset.add(ann)
with open('dummy.json', 'w') as f:
f.write(json.dumps(dataset.coco()))
coco = COCO('dummy.json')
new_mask = coco.annToMask(coco.anns[1])

by rejecting those single(or two)-point polygons, this can be solved:
just edit the polygon- method in the Mask-Class and replace:
polygons = [polygon.flatten() for polygon in polygons]
with
polygons = [polygon.flatten() for polygon in polygons if polygon.shape[0] > 2]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions