Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the function that converts a mask to a polygon #39

Open
xxxmaster opened this issue Mar 6, 2017 · 7 comments
Open

the function that converts a mask to a polygon #39

xxxmaster opened this issue Mar 6, 2017 · 7 comments

Comments

@xxxmaster
Copy link

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
Copy link

PavlosMelissinos commented Mar 16, 2017

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
Copy link

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
Copy link

jsbroks commented Jan 23, 2019

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
Copy link

Check this

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

@zhangyingbit
Copy link

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
Copy link

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

@mob5566
Copy link

mob5566 commented May 15, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants