draw
¶skimage.draw.line (r0, c0, r1, c1) |
Generate line pixel coordinates. |
skimage.draw.line_aa (r0, c0, r1, c1) |
Generate anti-aliased line pixel coordinates. |
skimage.draw.bezier_curve (r0, c0, r1, c1, …) |
Generate Bezier curve coordinates. |
skimage.draw.polygon (r, c[, shape]) |
Generate coordinates of pixels within polygon. |
skimage.draw.polygon_perimeter (r, c[, …]) |
Generate polygon perimeter coordinates. |
skimage.draw.ellipse (r, c, r_radius, c_radius) |
Generate coordinates of pixels within ellipse. |
skimage.draw.ellipse_perimeter (r, c, …[, …]) |
Generate ellipse perimeter coordinates. |
skimage.draw.ellipsoid (a, b, c[, spacing, …]) |
Generates ellipsoid with semimajor axes aligned with grid dimensions on grid with specified spacing. |
skimage.draw.ellipsoid_stats (a, b, c) |
Calculates analytical surface area and volume for ellipsoid with semimajor axes aligned with grid dimensions of specified spacing. |
skimage.draw.circle (r, c, radius[, shape]) |
Generate coordinates of pixels within circle. |
skimage.draw.circle_perimeter (r, c, radius) |
Generate circle perimeter coordinates. |
skimage.draw.circle_perimeter_aa (r, c, radius) |
Generate anti-aliased circle perimeter coordinates. |
skimage.draw.set_color (image, coords, color) |
Set pixel color in the image at the given coordinates. |
skimage.draw.random_shapes (image_shape, …) |
Generate an image with random shapes, labeled with bounding boxes. |
skimage.draw.rectangle (start[, end, extent, …]) |
Generate coordinates of pixels within a rectangle. |
skimage.draw.
line
(r0, c0, r1, c1)[source]¶Generate line pixel coordinates.
Parameters: | r0, c0 : int
r1, c1 : int
|
---|---|
Returns: | rr, cc : (N,) ndarray of int
|
Notes
Anti-aliased line generator is available with line_aa.
Examples
>>> from skimage.draw import line
>>> img = np.zeros((10, 10), dtype=np.uint8)
>>> rr, cc = line(1, 1, 8, 8)
>>> 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, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
skimage.draw.
line_aa
(r0, c0, r1, c1)[source]¶Generate anti-aliased line pixel coordinates.
Parameters: | r0, c0 : int
r1, c1 : int
|
---|---|
Returns: | rr, cc, val : (N,) ndarray (int, int, float)
|
References
[R87] | A Rasterizing Algorithm for Drawing Curves, A. Zingl, 2012 http://members.chello.at/easyfilter/Bresenham.pdf |
Examples
>>> from skimage.draw import line_aa
>>> img = np.zeros((10, 10), dtype=np.uint8)
>>> rr, cc, val = line_aa(1, 1, 8, 8)
>>> img[rr, cc] = val * 255
>>> img
array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 255, 74, 0, 0, 0, 0, 0, 0, 0],
[ 0, 74, 255, 74, 0, 0, 0, 0, 0, 0],
[ 0, 0, 74, 255, 74, 0, 0, 0, 0, 0],
[ 0, 0, 0, 74, 255, 74, 0, 0, 0, 0],
[ 0, 0, 0, 0, 74, 255, 74, 0, 0, 0],
[ 0, 0, 0, 0, 0, 74, 255, 74, 0, 0],
[ 0, 0, 0, 0, 0, 0, 74, 255, 74, 0],
[ 0, 0, 0, 0, 0, 0, 0, 74, 255, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
skimage.draw.
bezier_curve
(r0, c0, r1, c1, r2, c2, weight, shape=None)[source]¶Generate Bezier curve coordinates.
Parameters: | r0, c0 : int
r1, c1 : int
r2, c2 : int
weight : double
shape : tuple, optional
|
---|---|
Returns: | rr, cc : (N,) ndarray of int
|
Notes
The algorithm is the rational quadratic algorithm presented in reference [R88].
References
[R88] | (1, 2) A Rasterizing Algorithm for Drawing Curves, A. Zingl, 2012 http://members.chello.at/easyfilter/Bresenham.pdf |
Examples
>>> import numpy as np
>>> from skimage.draw import bezier_curve
>>> img = np.zeros((10, 10), dtype=np.uint8)
>>> rr, cc = bezier_curve(1, 5, 5, -2, 8, 8, 2)
>>> img[rr, cc] = 1
>>> img
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 0, 0, 0, 0, 0],
[0, 0, 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, 1, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
skimage.draw.
polygon
(r, c, shape=None)[source]¶Generate coordinates of pixels within polygon.
Parameters: | r : (N,) ndarray
c : (N,) ndarray
shape : tuple, optional
|
---|---|
Returns: | rr, cc : ndarray of int
|
Examples
>>> from skimage.draw import polygon
>>> img = np.zeros((10, 10), dtype=np.uint8)
>>> r = np.array([1, 2, 8, 1])
>>> c = np.array([1, 7, 4, 1])
>>> rr, cc = polygon(r, c)
>>> img[rr, cc] = 1
>>> img
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 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, 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]], dtype=uint8)
skimage.draw.
polygon_perimeter
(r, c, shape=None, clip=False)[source]¶Generate polygon perimeter coordinates.
Parameters: | r : (N,) ndarray
c : (N,) ndarray
shape : tuple, optional
clip : bool, optional
|
---|---|
Returns: | rr, cc : ndarray of int
|
Examples
>>> from skimage.draw import polygon_perimeter
>>> img = np.zeros((10, 10), dtype=np.uint8)
>>> rr, cc = polygon_perimeter([5, -1, 5, 10],
... [-1, 5, 11, 5],
... shape=img.shape, clip=True)
>>> img[rr, cc] = 1
>>> img
array([[0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 1, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 1, 1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 1, 0, 0, 0, 1, 1, 0],
[0, 0, 0, 0, 1, 1, 1, 0, 0, 0]], dtype=uint8)
skimage.draw.
ellipse
(r, c, r_radius, c_radius, shape=None, rotation=0.0)[source]¶Generate coordinates of pixels within ellipse.
Parameters: | r, c : double
r_radius, c_radius : double
shape : tuple, optional
rotation : float, optional (default 0.)
|
---|---|
Returns: | rr, cc : ndarray of int
|
Notes
The ellipse equation:
((x * cos(alpha) + y * sin(alpha)) / x_radius) ** 2 +
((x * sin(alpha) - y * cos(alpha)) / y_radius) ** 2 = 1
Note that the positions of ellipse without specified shape can have
also, negative values, as this is correct on the plane. On the other hand
using these ellipse positions for an image afterwards may lead to appearing
on the other side of image, because image[-1, -1] = image[end-1, end-1]
>>> rr, cc = ellipse(1, 2, 3, 6)
>>> img = np.zeros((6, 12), dtype=np.uint8)
>>> img[rr, cc] = 1
>>> img
array([[1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1]], dtype=uint8)
Examples
>>> from skimage.draw import ellipse
>>> img = np.zeros((10, 12), dtype=np.uint8)
>>> rr, cc = ellipse(5, 6, 3, 5, rotation=np.deg2rad(30))
>>> img[rr, cc] = 1
>>> img
array([[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, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
skimage.draw.
ellipse_perimeter
(r, c, r_radius, c_radius, orientation=0, shape=None)[source]¶Generate ellipse perimeter coordinates.
Parameters: | r, c : int
r_radius, c_radius : int
orientation : double, optional
shape : tuple, optional
|
---|---|
Returns: | rr, cc : (N,) ndarray of int
|
References
[R89] | A Rasterizing Algorithm for Drawing Curves, A. Zingl, 2012 http://members.chello.at/easyfilter/Bresenham.pdf |
Examples
>>> from skimage.draw import ellipse_perimeter
>>> img = np.zeros((10, 10), dtype=np.uint8)
>>> rr, cc = ellipse_perimeter(5, 5, 3, 4)
>>> img[rr, cc] = 1
>>> img
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 1, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
Note that the positions of ellipse without specified shape can have
also, negative values, as this is correct on the plane. On the other hand
using these ellipse positions for an image afterwards may lead to appearing
on the other side of image, because image[-1, -1] = image[end-1, end-1]
>>> rr, cc = ellipse_perimeter(2, 3, 4, 5)
>>> img = np.zeros((9, 12), dtype=np.uint8)
>>> img[rr, cc] = 1
>>> img
array([[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]], dtype=uint8)
skimage.draw.
ellipsoid
(a, b, c, spacing=(1.0, 1.0, 1.0), levelset=False)[source]¶Generates ellipsoid with semimajor axes aligned with grid dimensions on grid with specified spacing.
Parameters: | a : float
b : float
c : float
spacing : tuple of floats, length 3
levelset : bool
|
---|---|
Returns: | ellip : (N, M, P) array
|
skimage.draw.
ellipsoid_stats
(a, b, c)[source]¶Calculates analytical surface area and volume for ellipsoid with semimajor axes aligned with grid dimensions of specified spacing.
Parameters: | a : float
b : float
c : float
|
---|---|
Returns: | vol : float
surf : float
|
skimage.draw.
circle
(r, c, radius, shape=None)[source]¶Generate coordinates of pixels within circle.
Parameters: | r, c : double
radius : double
shape : tuple, optional
|
---|---|
Returns: | rr, cc : ndarray of int
|
Examples
>>> from skimage.draw import circle
>>> img = np.zeros((10, 10), dtype=np.uint8)
>>> rr, cc = circle(4, 4, 5)
>>> img[rr, cc] = 1
>>> img
array([[0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
skimage.draw.
circle_perimeter
(r, c, radius, method='bresenham', shape=None)[source]¶Generate circle perimeter coordinates.
Parameters: | r, c : int
radius: int
method : {‘bresenham’, ‘andres’}, optional
shape : tuple, optional
|
---|---|
Returns: | rr, cc : (N,) ndarray of int
|
Notes
Andres method presents the advantage that concentric circles create a disc whereas Bresenham can make holes. There is also less distortions when Andres circles are rotated. Bresenham method is also known as midpoint circle algorithm. Anti-aliased circle generator is available with circle_perimeter_aa.
References
[R90] | J.E. Bresenham, “Algorithm for computer control of a digital plotter”, IBM Systems journal, 4 (1965) 25-30. |
[R91] | E. Andres, “Discrete circles, rings and spheres”, Computers & Graphics, 18 (1994) 695-706. |
Examples
>>> from skimage.draw import circle_perimeter
>>> img = np.zeros((10, 10), dtype=np.uint8)
>>> rr, cc = circle_perimeter(4, 4, 3)
>>> img[rr, cc] = 1
>>> img
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 1, 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]], dtype=uint8)
skimage.draw.
circle_perimeter_aa
(r, c, radius, shape=None)[source]¶Generate anti-aliased circle perimeter coordinates.
Parameters: | r, c : int
radius: int
shape : tuple, optional
|
---|---|
Returns: | rr, cc, val : (N,) ndarray (int, int, float)
|
Notes
Wu’s method draws anti-aliased circle. This implementation doesn’t use lookup table optimization.
References
[R92] | X. Wu, “An efficient antialiasing technique”, In ACM SIGGRAPH Computer Graphics, 25 (1991) 143-152. |
Examples
>>> from skimage.draw import circle_perimeter_aa
>>> img = np.zeros((10, 10), dtype=np.uint8)
>>> rr, cc, val = circle_perimeter_aa(4, 4, 3)
>>> img[rr, cc] = val * 255
>>> img
array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 60, 211, 255, 211, 60, 0, 0, 0],
[ 0, 60, 194, 43, 0, 43, 194, 60, 0, 0],
[ 0, 211, 43, 0, 0, 0, 43, 211, 0, 0],
[ 0, 255, 0, 0, 0, 0, 0, 255, 0, 0],
[ 0, 211, 43, 0, 0, 0, 43, 211, 0, 0],
[ 0, 60, 194, 43, 0, 43, 194, 60, 0, 0],
[ 0, 0, 60, 211, 255, 211, 60, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
skimage.draw.
set_color
(image, coords, color, alpha=1)[source]¶Set pixel color in the image at the given coordinates.
Note that this function modifies the color of the image in-place. Coordinates that exceed the shape of the image will be ignored.
Parameters: | image : (M, N, D) ndarray
coords : tuple of ((P,) ndarray, (P,) ndarray)
color : (D,) ndarray
alpha : scalar or (N,) ndarray
|
---|
Examples
>>> from skimage.draw import line, set_color
>>> img = np.zeros((10, 10), dtype=np.uint8)
>>> rr, cc = line(1, 1, 20, 20)
>>> set_color(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, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1]], dtype=uint8)
skimage.draw.
random_shapes
(image_shape, max_shapes, min_shapes=1, min_size=2, max_size=None, multichannel=True, num_channels=3, shape=None, intensity_range=None, allow_overlap=False, num_trials=100, random_seed=None)[source]¶Generate an image with random shapes, labeled with bounding boxes.
The image is populated with random shapes with random sizes, random locations, and random colors, with or without overlap.
Shapes have random (row, col) starting coordinates and random sizes bounded by min_size and max_size. It can occur that a randomly generated shape will not fit the image at all. In that case, the algorithm will try again with new starting coordinates a certain number of times. However, it also means that some shapes may be skipped altogether. In that case, this function will generate fewer shapes than requested.
Parameters: | image_shape : tuple
max_shapes : int
min_shapes : int, optional
min_size : int, optional
max_size : int, optional
multichannel : bool, optional
num_channels : int, optional
shape : {rectangle, circle, triangle, None} str, optional
intensity_range : {tuple of tuples of uint8, tuple of uint8}, optional
allow_overlap : bool, optional
num_trials : int, optional
seed : int, optional
|
---|---|
Returns: | image : uint8 array
labels : list
|
Examples
>>> import skimage.draw
>>> image, labels = skimage.draw.random_shapes((32, 32), max_shapes=3)
>>> image # doctest: +SKIP
array([
[[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
...,
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]]], dtype=uint8)
>>> labels # doctest: +SKIP
[('circle', ((22, 18), (25, 21))),
('triangle', ((5, 6), (13, 13)))]
skimage.draw.
rectangle
(start, end=None, extent=None, shape=None)[source]¶Generate coordinates of pixels within a rectangle.
Parameters: | start : tuple
end : tuple
extent : tuple
shape : tuple, optional
|
---|---|
Returns: | coords : array of int, shape (Ndim, Npoints)
|
Notes
This function can be applied to N-dimensional images, by passing start and end or extent as tuples of length N.
Examples
>>> import numpy as np
>>> from skimage.draw import rectangle
>>> img = np.zeros((5, 5), dtype=np.uint8)
>>> start = (1, 1)
>>> extent = (3, 3)
>>> rr, cc = rectangle(start, extent=extent, shape=img.shape)
>>> img[rr, cc] = 1
>>> img
array([[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0]], dtype=uint8)
>>> img = np.zeros((5, 5), dtype=np.uint8)
>>> start = (0, 1)
>>> end = (3, 3)
>>> rr, cc = rectangle(start, end=end, shape=img.shape)
>>> img[rr, cc] = 1
>>> img
array([[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0]], dtype=uint8)