DrawProxy

class Stoner.Image.attrs.DrawProxy(*args, **kargs)[source]

Bases: object

Provides a wrapper around skimage.draw to allow easy drawing of objects onto images.

This class allows access the user to draw simply shapes on an image (or its mask) by specifying the desired shape and geometry (centre, length/width etc). Mostly this implemented by pass throughs to the skimage.draw module, but methods are provided for an annulus, rectangle (and square) and rectangle-perimeter meothdds- the latter offering rotation about the centre pooint in contrast to the skimage.draw equivalents.

No state data is stored with this class so the attribute does not need to be serialised when the parent ImageFile is saved.

Methods Summary

annulus(r, c, radius1, radius2[, shape, value])

Use a combination of two circles to draw and annulus.

bezier_curve(c0, r1, c1, r2, c2, weight[, shape])

Generate Bezier curve coordinates.

circle(c, radius[, shape])

Generate coordinates of pixels within circle.

circle_perimeter(c, radius[, method, shape])

Generate circle perimeter coordinates.

circle_perimeter_aa(c, radius[, shape])

Generate anti-aliased circle perimeter coordinates.

disk(radius, *[, shape])

Generate coordinates of pixels within circle.

ellipse(c, r_radius, c_radius[, shape, rotation])

Generate coordinates of pixels within ellipse.

ellipse_perimeter(c, r_radius, c_radius[, ...])

Generate ellipse perimeter coordinates.

ellipsoid(b, c[, spacing, levelset])

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

ellipsoid_stats(b, c)

Calculates analytical surface area and volume for ellipsoid with semimajor axes aligned with grid dimensions of specified spacing.

line(c0, r1, c1)

Generate line pixel coordinates.

line_aa(c0, r1, c1)

Generate anti-aliased line pixel coordinates.

line_nd(stop, *[, endpoint, integer])

Draw a single-pixel thick line in n dimensions.

polygon(c[, shape])

Generate coordinates of pixels within polygon.

polygon2mask(polygon)

Compute a mask from polygon.

polygon_perimeter(c[, shape, clip])

Generate polygon perimeter coordinates.

random_shapes(max_shapes[, min_shapes, ...])

Generate an image with random shapes, labeled with bounding boxes.

rectangle(r, c, w, h[, angle, shape, value])

Draw a rectangle on an image.

rectangle_perimeter(r, c, w, h[, angle, ...])

Draw the perimter of a rectangle on an image.

set_color(coords, color[, alpha])

Set pixel color in the image at the given coordinates.

square(r, c, w[, angle, shape, value])

Draw a square on an image.

Methods Documentation

annulus(r, c, radius1, radius2, shape=None, value=1.0)[source]

Use a combination of two circles to draw and annulus.

Parameters
  • r,c (float) – Centre co-ordinates

  • radius1,radius2 (float) – Inner and outer radius.

Keyword Arguments
  • shape (2-tuple, None) – Confine the co-ordinates to staywith shape

  • value (float) – value to draw with

Returns

A copy of the image with the annulus drawn on it.

Notes

If radius2<radius1 then the sense of the whole shape is inverted so that the annulus is left clear and the filed is filled.

bezier_curve(c0, r1, c1, r2, c2, weight, shape=None)

Generate Bezier curve coordinates.

r0, c0int

Coordinates of the first control point.

r1, c1int

Coordinates of the middle control point.

r2, c2int

Coordinates of the last control point.

weightdouble

Middle control point weight, it describes the line tension.

shapetuple, optional

Image shape which is used to determine the maximum extent of output pixel coordinates. This is useful for curves that exceed the image size. If None, the full extent of the curve is used.

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.

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

1

A Rasterizing Algorithm for Drawing Curves, A. Zingl, 2012 http://members.chello.at/easyfilter/Bresenham.pdf

>>> 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(c, radius, shape=None)

Generate coordinates of pixels within circle.

r, cdouble

Center coordinate of disk.

radiusdouble

Radius of disk.

shapetuple, optional

Image shape which is used to determine the maximum extent of output pixel coordinates. This is useful for disks that exceed the image size. If None, the full extent of the disk is used. Must be at least length 2. Only the first two values are used to determine the extent of the input image.

rr, ccndarray of int

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

Deprecated:

New in version 0.17: This function is deprecated and will be removed in scikit-image 0.19. Please use the function named disk instead.

circle_perimeter(c, radius, method='bresenham', shape=None)

Generate circle perimeter coordinates.

r, cint

Centre coordinate of circle.

radiusint

Radius of circle.

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

bresenham : Bresenham method (default) andres : Andres method

shapetuple, optional

Image shape which is used to determine the maximum extent of output pixel coordinates. This is useful for circles that exceed the image size. If None, the full extent of the circle is used. Must be at least length 2. Only the first two values are used to determine the extent of the input image.

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.

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.

1

J.E. Bresenham, “Algorithm for computer control of a digital plotter”, IBM Systems journal, 4 (1965) 25-30.

2

E. Andres, “Discrete circles, rings and spheres”, Computers & Graphics, 18 (1994) 695-706.

>>> 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(c, radius, shape=None)

Generate anti-aliased circle perimeter coordinates.

r, cint

Centre coordinate of circle.

radiusint

Radius of circle.

shapetuple, optional

Image shape which is used to determine the maximum extent of output pixel coordinates. This is useful for circles that exceed the image size. If None, the full extent of the circle is used. Must be at least length 2. Only the first two values are used to determine the extent of the input image.

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

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

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

Use the function draw.set_color to apply circle_perimeter_aa results to color images.

1

X. Wu, “An efficient antialiasing technique”, In ACM SIGGRAPH Computer Graphics, 25 (1991) 143-152.

>>> 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)
>>> from skimage import data, draw
>>> image = data.chelsea()
>>> rr, cc, val = draw.circle_perimeter_aa(r=100, c=100, radius=75)
>>> draw.set_color(image, (rr, cc), [1, 0, 0], alpha=val)
disk(radius, *, shape=None)

Generate coordinates of pixels within circle.

centertuple

Center coordinate of disk.

radiusdouble

Radius of disk.

shapetuple, optional

Image shape which is used to determine the maximum extent of output pixel coordinates. This is useful for disks that exceed the image size. If None, the full extent of the disk is used. Must be at least length 2. Only the first two values are used to determine the extent of the input image.

rr, ccndarray of int

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

>>> from skimage.draw import disk
>>> img = np.zeros((10, 10), dtype=np.uint8)
>>> rr, cc = disk((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)
ellipse(c, r_radius, c_radius, shape=None, rotation=0.0)

Generate coordinates of pixels within ellipse.

r, cdouble

Centre coordinate of ellipse.

r_radius, c_radiusdouble

Minor and major semi-axes. (r/r_radius)**2 + (c/c_radius)**2 = 1.

shapetuple, optional

Image shape which is used to determine the maximum extent of output pixel coordinates. This is useful for ellipses which exceed the image size. By default the full extent of the ellipse are used. Must be at least length 2. Only the first two values are used to determine the extent.

rotationfloat, optional (default 0.)

Set the ellipse rotation (rotation) in range (-PI, PI) in contra clock wise direction, so PI/2 degree means swap ellipse axis

rr, ccndarray of int

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

>>> 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)

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)
ellipse_perimeter(c, r_radius, c_radius, orientation=0, shape=None)

Generate ellipse perimeter coordinates.

r, cint

Centre coordinate of ellipse.

r_radius, c_radiusint

Minor and major semi-axes. (r/r_radius)**2 + (c/c_radius)**2 = 1.

orientationdouble, optional

Major axis orientation in clockwise direction as radians.

shapetuple, optional

Image shape which is used to determine the maximum extent of output pixel coordinates. This is useful for ellipses that exceed the image size. If None, the full extent of the ellipse is used. Must be at least length 2. Only the first two values are used to determine the extent of the input image.

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.

1

A Rasterizing Algorithm for Drawing Curves, A. Zingl, 2012 http://members.chello.at/easyfilter/Bresenham.pdf

>>> 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)
ellipsoid(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.

afloat

Length of semimajor axis aligned with x-axis.

bfloat

Length of semimajor axis aligned with y-axis.

cfloat

Length of semimajor axis aligned with z-axis.

spacingtuple of floats, length 3

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

levelsetbool

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.

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(b, c)

Calculates analytical surface area and volume for ellipsoid with semimajor axes aligned with grid dimensions of specified spacing.

afloat

Length of semimajor axis aligned with x-axis.

bfloat

Length of semimajor axis aligned with y-axis.

cfloat

Length of semimajor axis aligned with z-axis.

volfloat

Calculated volume of ellipsoid.

surffloat

Calculated surface area of ellipsoid.

line(c0, r1, c1)

Generate line pixel coordinates.

r0, c0int

Starting position (row, column).

r1, c1int

End position (row, column).

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.

Anti-aliased line generator is available with line_aa.

>>> 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(c0, r1, c1)

Generate anti-aliased line pixel coordinates.

r0, c0int

Starting position (row, column).

r1, c1int

End position (row, column).

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

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

1

A Rasterizing Algorithm for Drawing Curves, A. Zingl, 2012 http://members.chello.at/easyfilter/Bresenham.pdf

>>> 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)
line_nd(stop, *, endpoint=False, integer=True)

Draw a single-pixel thick line in n dimensions.

The line produced will be ndim-connected. That is, two subsequent pixels in the line will be either direct or diagonal neighbours in n dimensions.

startarray-like, shape (N,)

The start coordinates of the line.

stoparray-like, shape (N,)

The end coordinates of the line.

endpointbool, optional

Whether to include the endpoint in the returned line. Defaults to False, which allows for easy drawing of multi-point paths.

integerbool, optional

Whether to round the coordinates to integer. If True (default), the returned coordinates can be used to directly index into an array. False could be used for e.g. vector drawing.

coordstuple of arrays

The coordinates of points on the line.

>>> lin = line_nd((1, 1), (5, 2.5), endpoint=False)
>>> lin
(array([1, 2, 3, 4]), array([1, 1, 2, 2]))
>>> im = np.zeros((6, 5), dtype=int)
>>> im[lin] = 1
>>> im
array([[0, 0, 0, 0, 0],
       [0, 1, 0, 0, 0],
       [0, 1, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 0, 0, 0]])
>>> line_nd([2, 1, 1], [5, 5, 2.5], endpoint=True)
(array([2, 3, 4, 4, 5]), array([1, 2, 3, 4, 5]), array([1, 1, 2, 2, 2]))
polygon(c, shape=None)

Generate coordinates of pixels within polygon.

r(N,) ndarray

Row coordinates of vertices of polygon.

c(N,) ndarray

Column coordinates of vertices of polygon.

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.

rr, ccndarray of int

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

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

Compute a mask from polygon.

image_shapetuple of size 2.

The shape of the mask.

polygonarray_like.

The polygon coordinates of shape (N, 2) where N is the number of points.

mask2-D ndarray of type ‘bool’.

The mask that corresponds to the input polygon.

This function does not do any border checking, so that all the vertices need to be within the given shape.

>>> image_shape = (128, 128)
>>> polygon = np.array([[60, 100], [100, 40], [40, 40]])
>>> mask = polygon2mask(image_shape, polygon)
>>> mask.shape
(128, 128)
polygon_perimeter(c, shape=None, clip=False)

Generate polygon perimeter coordinates.

r(N,) ndarray

Row coordinates of vertices of polygon.

c(N,) ndarray

Column coordinates of vertices of polygon.

shapetuple, optional

Image shape which is used to determine maximum extents of output pixel coordinates. This is useful for polygons that exceed the image size. If None, the full extents 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.

clipbool, optional

Whether to clip the polygon to the provided shape. If this is set to True, the drawn figure will always be a closed polygon with all edges visible.

rr, ccndarray of int

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

>>> 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)
random_shapes(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)

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.

image_shapetuple

The number of rows and columns of the image to generate.

max_shapesint

The maximum number of shapes to (attempt to) fit into the shape.

min_shapesint, optional

The minimum number of shapes to (attempt to) fit into the shape.

min_sizeint, optional

The minimum dimension of each shape to fit into the image.

max_sizeint, optional

The maximum dimension of each shape to fit into the image.

multichannelbool, optional

If True, the generated image has num_channels color channels, otherwise generates grayscale image.

num_channelsint, optional

Number of channels in the generated image. If 1, generate monochrome images, else color images with multiple channels. Ignored if multichannel is set to False.

shape{rectangle, circle, triangle, ellipse, None} str, optional

The name of the shape to generate or None to pick random ones.

intensity_range{tuple of tuples of uint8, tuple of uint8}, optional

The range of values to sample pixel values from. For grayscale images the format is (min, max). For multichannel - ((min, max),) if the ranges are equal across the channels, and ((min_0, max_0), … (min_N, max_N)) if they differ. As the function supports generation of uint8 arrays only, the maximum range is (0, 255). If None, set to (0, 254) for each channel reserving color of intensity = 255 for background.

allow_overlapbool, optional

If True, allow shapes to overlap.

num_trialsint, optional

How often to attempt to fit a shape into the image before skipping it.

random_seedint, optional

Seed to initialize the random number generator. If None, a random seed from the operating system is used.

imageuint8 array

An image with the fitted shapes.

labelslist

A list of labels, one per shape in the image. Each label is a (category, ((r0, r1), (c0, c1))) tuple specifying the category and bounding box coordinates of the shape.

>>> import skimage.draw
>>> image, labels = skimage.draw.random_shapes((32, 32), max_shapes=3)
>>> image 
array([
   [[255, 255, 255],
    [255, 255, 255],
    [255, 255, 255],
    ...,
    [255, 255, 255],
    [255, 255, 255],
    [255, 255, 255]]], dtype=uint8)
>>> labels 
[('circle', ((22, 18), (25, 21))),
 ('triangle', ((5, 6), (13, 13)))]
rectangle(r, c, w, h, angle=0.0, shape=None, value=1.0)[source]

Draw a rectangle on an image.

Parameters
  • r,c (float) – Centre co-ordinates

  • w,h (float) – Lengths of the two sides of the rectangle

Keyword Arguments
  • angle (float) – Angle to rotate the rectangle about

  • shape (2-tuple or None) – Confine the co-ordinates to this shape.

  • value (float) – The value to draw with.

Returns

A copy of the current image with a rectangle drawn on.

rectangle_perimeter(r, c, w, h, angle=0.0, shape=None, value=1.0)[source]

Draw the perimter of a rectangle on an image.

Parameters
  • r,c (float) – Centre co-ordinates

  • w,h (float) – Lengths of the two sides of the rectangle

Keyword Arguments
  • angle (float) – Angle to rotate the rectangle about

  • shape (2-tuple or None) – Confine the co-ordinates to this shape.

  • value (float) – The value to draw with.

Returns

A copy of the current image with a rectangle drawn on.

set_color(coords, color, alpha=1)

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.

image(M, N, D) ndarray

Image

coordstuple of ((P,) ndarray, (P,) ndarray)

Row and column coordinates of pixels to be colored.

color(D,) ndarray

Color to be assigned to coordinates in the image.

alphascalar or (N,) ndarray

Alpha values used to blend color with image. 0 is transparent, 1 is opaque.

>>> 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)
square(r, c, w, angle=0.0, shape=None, value=1.0)[source]

Draw a square on an image.

Parameters
  • r,c (float) – Centre co-ordinates

  • w (float) – Length of the side of the square

Keyword Arguments
  • angle (float) – Angle to rotate the rectangle about

  • shape (2-tuple or None) – Confine the co-ordinates to this shape.

  • value (float) – The value to draw with.

Returns

A copy of the current image with a rectangle drawn on.