Module: draw

skimage.draw.bezier_curve Generate Bezier curve coordinates.
skimage.draw.circle(cy, cx, radius[, shape]) Generate coordinates of pixels within circle.
skimage.draw.circle_perimeter Generate circle perimeter coordinates.
skimage.draw.circle_perimeter_aa Generate anti-aliased circle perimeter coordinates.
skimage.draw.ellipse(cy, cx, yradius, xradius) Generate coordinates of pixels within ellipse.
skimage.draw.ellipse_perimeter 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.line Generate line pixel coordinates.
skimage.draw.line_aa Generate anti-aliased line pixel coordinates.
skimage.draw.polygon Generate coordinates of pixels within polygon.
skimage.draw.set_color(img, coords, color) Set pixel color in the image at the given coordinates.

bezier_curve

skimage.draw.bezier_curve()

Generate Bezier curve coordinates.

Parameters:

y0, x0 : int

Coordinates of the first control point.

y1, x1 : int

Coordinates of the middle control point.

y2, x2 : int

Coordinates of the last control point.

weight : double

Middle control point weight, it describes the line tension.

Returns:

rr, cc : (N,) ndarray of int

Indices of pixels that belong to the Bezier curve. May be used to directly index into an array, e.g. img[rr, cc] = 1.

Notes

The algorithm is the rational quadratic algorithm presented in reference [R67].

References

[R67](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)

circle

skimage.draw.circle(cy, cx, radius, shape=None)

Generate coordinates of pixels within circle.

Parameters:

cy, cx : double

Centre coordinate of circle.

radius: double :

Radius of circle.

shape : tuple, optional

Image shape which is used to determine maximum extents of output pixel coordinates. This is useful for circles which exceed the image size. By default the full extents of the circle are used.

Returns:

rr, cc : ndarray of int

Pixel coordinates of circle. May be used to directly index into an array, e.g. img[rr, cc] = 1.

Notes :

—– :

This function is a wrapper for skimage.draw.ellipse()

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)

circle_perimeter

skimage.draw.circle_perimeter()

Generate circle perimeter coordinates.

Parameters:

cy, cx : int

Centre coordinate of circle.

radius: int :

Radius of circle.

method : {‘bresenham’, ‘andres’}, optional

bresenham : Bresenham method (default) andres : Andres method

Returns:

rr, cc : (N,) ndarray of int

Bresenham and Andres’ method: Indices of pixels that belong to the circle perimeter. May be used to directly index into an array, e.g. img[rr, cc] = 1.

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

[R68]J.E. Bresenham, “Algorithm for computer control of a digital plotter”, IBM Systems journal, 4 (1965) 25-30.
[R69]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)

circle_perimeter_aa

skimage.draw.circle_perimeter_aa()

Generate anti-aliased circle perimeter coordinates.

Parameters:

cy, cx : int

Centre coordinate of circle.

radius: int :

Radius of circle.

Returns:

rr, cc, val : (N,) ndarray (int, int, float)

Indices of pixels (rr, cc) and intensity values (val). img[rr, cc] = val.

Notes

Wu’s method draws anti-aliased circle. This implementation doesn’t use lookup table optimization.

References

[R70]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)

ellipse

skimage.draw.ellipse(cy, cx, yradius, xradius, shape=None)

Generate coordinates of pixels within ellipse.

Parameters:

cy, cx : double

Centre coordinate of ellipse.

yradius, xradius : double

Minor and major semi-axes. (x/xradius)**2 + (y/yradius)**2 = 1.

shape : tuple, optional

Image shape which is used to determine maximum extents of output pixel coordinates. This is useful for ellipses which exceed the image size. By default the full extents of the ellipse are used.

Returns:

rr, cc : ndarray of int

Pixel coordinates of ellipse. May be used to directly index into an array, e.g. img[rr, cc] = 1.

Examples

>>> from skimage.draw import ellipse
>>> img = np.zeros((10, 10), dtype=np.uint8)
>>> rr, cc = ellipse(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, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 1, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 1, 1, 1, 1, 0],
       [0, 0, 1, 1, 1, 1, 1, 1, 1, 0],
       [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, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)

ellipse_perimeter

skimage.draw.ellipse_perimeter()

Generate ellipse perimeter coordinates.

Parameters:

cy, cx : int

Centre coordinate of ellipse.

yradius, xradius : int

Minor and major semi-axes. (x/xradius)**2 + (y/yradius)**2 = 1.

orientation : double, optional (default 0)

Major axis orientation in clockwise direction as radians.

Returns:

rr, cc : (N,) ndarray of int

Indices of pixels that belong to the ellipse perimeter. May be used to directly index into an array, e.g. img[rr, cc] = 1.

References

[R71]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)

ellipsoid

skimage.draw.ellipsoid(a, b, c, spacing=(1.0, 1.0, 1.0), levelset=False)

Generates ellipsoid with semimajor axes aligned with grid dimensions on grid with specified spacing.

Parameters:

a : float

Length of semimajor axis aligned with x-axis.

b : float

Length of semimajor axis aligned with y-axis.

c : float

Length of semimajor axis aligned with z-axis.

spacing : tuple of floats, length 3

Spacing in (x, y, z) spatial dimensions.

levelset : bool

If True, returns the level set for this ellipsoid (signed level set about zero, with positive denoting interior) as np.float64. False returns a binarized version of said level set.

Returns:

ellip : (N, M, P) array

Ellipsoid centered in a correctly sized array for given spacing. Boolean dtype unless levelset=True, in which case a float array is returned with the level set above 0.0 representing the ellipsoid.

ellipsoid_stats

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.

Parameters:

a : float

Length of semimajor axis aligned with x-axis.

b : float

Length of semimajor axis aligned with y-axis.

c : float

Length of semimajor axis aligned with z-axis.

Returns:

vol : float

Calculated volume of ellipsoid.

surf : float

Calculated surface area of ellipsoid.

line

skimage.draw.line()

Generate line pixel coordinates.

Parameters:

y, x : int

Starting position (row, column).

y2, x2 : int

End position (row, column).

Returns:

rr, cc : (N,) ndarray of int

Indices of pixels that belong to the line. May be used to directly index into an array, e.g. img[rr, cc] = 1.

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)

line_aa

skimage.draw.line_aa()

Generate anti-aliased line pixel coordinates.

Parameters:

y1, x1 : int

Starting position (row, column).

y2, x2 : int

End position (row, column).

Returns:

rr, cc, val : (N,) ndarray (int, int, float)

Indices of pixels (rr, cc) and intensity values (val). img[rr, cc] = val.

References

[R72]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,  56,   0,   0,   0,   0,   0,   0,   0],
       [  0,  56, 255,  56,   0,   0,   0,   0,   0,   0],
       [  0,   0,  56, 255,  56,   0,   0,   0,   0,   0],
       [  0,   0,   0,  56, 255,  56,   0,   0,   0,   0],
       [  0,   0,   0,   0,  56, 255,  56,   0,   0,   0],
       [  0,   0,   0,   0,   0,  56, 255,  56,   0,   0],
       [  0,   0,   0,   0,   0,   0,  56, 255,  56,   0],
       [  0,   0,   0,   0,   0,   0,   0,  56, 255,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0]], dtype=uint8)

polygon

skimage.draw.polygon()

Generate coordinates of pixels within polygon.

Parameters:

y : (N,) ndarray

Y-coordinates of vertices of polygon.

x : (N,) ndarray

X-coordinates of vertices of polygon.

shape : tuple, optional

Image shape which is used to determine maximum extents of output pixel coordinates. This is useful for polygons which exceed the image size. By default the full extents of the polygon are used.

Returns:

rr, cc : ndarray of int

Pixel coordinates of polygon. May be used to directly index into an array, e.g. img[rr, cc] = 1.

Examples

>>> from skimage.draw import polygon
>>> img = np.zeros((10, 10), dtype=np.uint8)
>>> x = np.array([1, 7, 4, 1])
>>> y = np.array([1, 2, 8, 1])
>>> rr, cc = polygon(y, x)
>>> 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)

set_color

skimage.draw.set_color(img, coords, color)

Set pixel color in the image at the given coordinates.

Coordinates that exceed the shape of the image will be ignored.

Parameters:

img : (M, N, D) ndarray

Image

coords : ((P,) ndarray, (P,) ndarray)

Coordinates of pixels to be colored.

color : (D,) ndarray

Color to be assigned to coordinates in the image.

Returns:

img : (M, N, D) ndarray

The updated image.

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)