skimage2.draw.polygon2mask#
- skimage2.draw.polygon2mask(image_shape, polygon)[source]#
Create a binary mask from a polygon.
- Parameters:
- image_shapetuple of (int, int)
The shape of the mask.
- polygonarray_like of shape (N, 2)
The polygon coordinates, where N is the number of points. The coordinates are (row, column).
- Returns:
- mask2-D ndarray of type âboolâ
The binary mask that corresponds to the input polygon.
See also
polygonGenerate coordinates of pixels inside a polygon.
Notes
This function does not do any border checking. Parts of the polygon that are outside the coordinate space defined by
image_shapeare not drawn.Examples
>>> import _skimage2 as ski2 >>> image_shape = (10, 10) >>> polygon = np.array([[1, 1], [2, 7], [8, 4]]) >>> mask = ski2.draw.polygon2mask(image_shape, polygon) >>> mask.astype(int) array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
If vertices / points of the
polygonare outside the coordinate space defined byimage_shape, only a part (or none at all) of the polygon is drawn in the mask.>>> offset = np.array([[2, -4]]) >>> ski2.draw.polygon2mask(image_shape, polygon - offset).astype(int) array([[0, 0, 0, 0, 0, 0, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])