cocodataset / cocoapi

COCO API - Dataset @ http://cocodataset.org/
Other
6.02k stars 3.75k forks source link

the function that converts a mask to a polygon #39

Open xxxmaster opened 7 years ago

xxxmaster commented 7 years ago

Could you please share the function that converts masks to polygons, which you used to generate the segmentation annotations for COCO dataset? Because I would like to train the deepMask on my dataset, which needs to represent masks by polygons to generate the groundtruth.

Thank you.

PavlosMelissinos commented 7 years ago

DeepMask is implemented in lua and I'm not aware of any libraries for convex/concave hull computation there, so I will assume you are familiar with python.

I had the same problem and I'm afraid there is no function that works out of the box. First of all, you need to decide whether you are ok with the assumption that the shapes are convex.

If so, you can easily get the convex hull of each object segment, with e.g. scipy or skimage.

Otherwise, you need to get the outline of your shape (only points on the perimeter), e.g. with opencv findContours. This is necessary because given a set of points, there are multiple concave hulls you can draw, but only a single convex one. Therefore, you will need to create a list of perimeter points, sorted in either clockwise or counter-clockwise order. "Clockwise" in concave shapes is not very well defined anyway but I think it's ok in this case, since the outline of an object mask is a planar graph.

Technically, having these outline points should be enough, as long as they are sorted; the problem is it's a huge set of polygon vertices and will probably be computationally expensive to use as input.

You can look here or here for how to sample your points and end up with a proper polygon. Apparently it's a common GIS task.

As far as I know there isn't a more straight-forward way. Maybe you can try this one or this one (not sure whether they work, haven't tested them myself).

Good luck.

garybradski commented 7 years ago

Small tutorial in python for OpenCV (this function should do what you want): http://docs.opencv.org/trunk/d4/d73/tutorial_py_contours_begin.html

jsbroks commented 5 years ago

I've written a python library that might help others.

Here is an example of getting the polygon repersentation of a mask

import numpy as np
from imantics import Polygons, Mask

# This can be any array
array = np.ones((100, 100))

polygons = Mask(array).polygons()

print(polygons.points)
print(polygons.segmentation)

Take a look at the docs for more detail.

1chimaruGin commented 4 years ago

Check this

https://www.immersivelimit.com/create-coco-annotations-from-scratch

zhangyingbit commented 3 years ago

I have found a tool that can generate the format of coco annotation, it works for me, hope it can help~ https://github.com/waspinator/pycococreator/blob/master/pycococreatortools/pycococreatortools.py

Conghung43 commented 3 years ago

https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.ConvexHull.html You should try ConvexHull image

mob5566 commented 3 years ago

@Conghung43 A mask doesn't need to be a "convex" hull but a "concave" hull.