简体   繁体   中英

Get coordinates of polygon outline and polygon mask given vertex coordinates

Given a matrix containing a polygon mask (here a small and simplistic case):

array([[0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 1, 0, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 1, 1, 1, 1, 1, 0],
       [0, 1, 1, 1, 1, 1, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 0, 0, 0, 0, 0]])

The outline is extracted with skimage.segmentation.find_boundaries() , giving:

array([[0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 1, 0, 0, 0],
       [0, 0, 1, 0, 1, 0, 0],
       [0, 1, 0, 0, 0, 1, 0],
       [0, 1, 0, 0, 0, 1, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 0, 0, 0, 0, 0]])

The outline's [row,column] (ie [y,x] ) coordinates are then extracted giving:

outline = array([[2,2],[1,2],[1,3],[2,4],[3,5],[4,5],[5,4],[5,3],[5,2],[4,1],[3,1]])

These coordinates are then pruned to a minimal set that define the polygon (ie the vertices), giving:

vertices = array([[2,2],[1,2],[1,3],[3,5],[4,5],[5,4],[5,2],[4,1],[3,1]])

(Which corresponds to:)

array([[0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 1, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 0],
       [0, 1, 0, 0, 0, 1, 0],
       [0, 1, 0, 0, 0, 1, 0],
       [0, 0, 1, 0, 1, 0, 0],
       [0, 0, 0, 0, 0, 0, 0]])

Is there a fast way using numpy/scipy/skimage/etc to get the outline coordinates (the array outline above) given the vertex coordinates (the array vertices above)?
Further, after getting back the outline coordinates, is there a good numpy/scipy/skimage way to get back the coordinates of all points in the original polygon mask?

Given 2 vertices in a polygon v1, v2 we can get all the points p which are part of the line from v1 to v2 using a line rasterization algorithm. A very fast algorithm for this is Bresenham's line drawing algorithm . After this you can apply this algorithm for each pair of adject vertices in the polygon. Although, I can not guarantee that the outline will be exactly the one in the original polygon since the rasterization algorithm will give the best set of points for the given line, not the one you have in the original algorithm (consider them compression errors).

For the filling algorithm, they are called polygon rasterization algorithm, but I can't help you here since I don't know which are best/fastest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM