bundles / skimage 0.26.1rc0.dev0 / skimage / draw / draw / polygon
function
skimage.draw.draw:polygon
source: /_skimage2/draw/draw.py:444
Signature
def polygon(r, c, shape = None)Summary
Generate coordinates of pixels inside a polygon.
Parameters
r: (N,) array_likeRow coordinates of the polygon's vertices.
c: (N,) array_likeColumn coordinates of the polygon's vertices.
shape: tuple, optionalImage 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, cc: ndarray of intPixel coordinates of polygon. May be used to directly index into an array, e.g.
img[rr, cc] = 1.
Notes
This function ensures that rr and cc don't contain negative values. Pixels of the polygon that whose coordinates are smaller 0, are not drawn.
Examples
import skimage as ski r = np.array([1, 2, 8]) c = np.array([1, 7, 4]) rr, cc = ski.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 `shape` is defined and vertices / points of the `polygon` are
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 = ski.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]])
See also
- polygon2mask
Create a binary mask from a polygon.
Aliases
skimage.draw.polygon