skimage2.draw.polygon#
- skimage2.draw.polygon(r, c, shape=None)[source]#
Generate coordinates of pixels inside a polygon.
- Parameters:
- r(N,) array_like
Row coordinates of the polygon’s vertices.
- c(N,) array_like
Column coordinates of the polygon’s vertices.
- shapetuple, optional
Image shape which is used to determine the maximum extent of output pixel coordinates. This is useful for polygons that exceed the image size. If None, the full extent of the polygon is used. Must be at least length 2. Only the first two values are used to determine the extent of the input image.
- Returns:
- rr, ccndarray of int
Pixel coordinates of polygon. May be used to directly index into an array, e.g.
img[rr, cc] = 1.
See also
polygon2maskCreate a binary mask from a polygon.
Notes
This function ensures that
rrandccdon’t contain negative values. Pixels of the polygon that whose coordinates are smaller 0, are not drawn.Examples
>>> import _skimage2 as ski2 >>> r = np.array([1, 2, 8]) >>> c = np.array([1, 7, 4]) >>> rr, cc = ski2.draw.polygon(r, c) >>> img = np.zeros((10, 10), dtype=int) >>> img[rr, cc] = 1 >>> img 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 the image
shapeis defined and vertices / points of thepolygonare outside this coordinate space, only a part (or none at all) of the polygon’s pixels is returned. Shifting the polygon’s vertices by an offset can be used to move the polygon around and potentially draw an arbitrary sub-region of the polygon.>>> offset = (2, -4) >>> rr, cc = ski2.draw.polygon(r - offset[0], c - offset[1], shape=img.shape) >>> img = np.zeros((10, 10), dtype=int) >>> img[rr, cc] = 1 >>> img 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]])