python - Filling in a concave polygon represented as a binary matrix -
in task, represent concave polygon matrix of ones , zeros, 1 means given point belongs polygon. instance, following simple square , u-shaped polygon:
0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 1 0 0 1 1 0 1 1 0 0 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 however, incomplete representation, in which: (1) boundary points included, , (2) internal points missing. example, in following enlarged version of u-shaped polygon, elements @ positions (1,1), (1,6), (3,1), ..., (3,6)* "unfilled". goal fill them (i.e., change value 1).
1 1 1 0 0 1 1 1 1 0 1 0 0 1 0 1 1 1 1 1 1 1 0 1 1 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 do know if there's easy way in python/numpy?
*(row, column), starting counting top left corner
this known problem in image processing can solved using morphological operators.
with that, can use scipy's binary_fill_holes fill holes in mask:
>>> import numpy np >>> scipy.ndimage import binary_fill_holes >>> data = np.array([[1, 1, 1, 0, 0, 1, 1, 1], [1, 0, 1, 0, 0, 1, 0, 1], [1, 1, 1, 1, 1, 1, 0, 1], [1, 0, 0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1, 1]]) >>> filled = binary_fill_holes(data).astype(int) >>> filled array([[1, 1, 1, 0, 0, 1, 1, 1], [1, 1, 1, 0, 0, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1]])
Comments
Post a Comment