shapes: 2D shapes drawing#

This module provides functions making masks on an image.

  • circle_fill() function generates coordinates of a circle in an image.

  • draw_line() function generates coordinates of a line in an image.

  • polygon_fill_mask() function generates a mask from a set of points defining a polygon.

The Polygon class provides checking if a point is inside a polygon.

The whole module uses the (row, col) (i.e., (y, x))) convention for 2D coordinates.

circle_fill(int crow, int ccol, float radius)#

Generates coordinate of image points lying in a disk.

Parameters:
  • crow (int) – Row of the center of the disk

  • ccol (int) – Column of the center of the disk

  • radius (float) – Radius of the disk

Returns:

Array coordinates of points inside the disk (might be negative)

Return type:

2-tuple of numpy.ndarray (rows, cols)

ellipse_fill(int crow, int ccol, float radius_r, float radius_c)#

Generates coordinate of image points lying in a ellipse.

Parameters:
  • crow (int) – Row of the center of the ellipse

  • ccol (int) – Column of the center of the ellipse

  • radius_r (float) – Radius of the ellipse in the row

  • radius_c (float) – Radius of the ellipse in the column

Returns:

Array coordinates of points inside the ellipse (might be negative)

Return type:

2-tuple of numpy.ndarray (rows, cols)

draw_line(int row0, int col0, int row1, int col1, int width=1)#

Line includes both end points. Width is handled by drawing parallel lines, so junctions of lines belonging to different octant with width > 1 will not look nice.

Using Bresenham line algorithm: Bresenham, J. E. Algorithm for computer control of a digital plotter. IBM Systems Journal. Vol 4 No 1. 1965. pp 25-30

Parameters:
  • row0 (int) – Start point row

  • col0 (int) – Start point col

  • row1 (int) – End point row

  • col1 (int) – End point col

  • width (int) – Thickness of the line in pixels (default 1) Width must be at least 1.

Returns:

Array coordinates of points inside the line (might be negative)

Return type:

2-tuple of numpy.ndarray (rows, cols)

polygon_fill_mask(vertices, shape)#

Return a mask of boolean, True for pixels inside a polygon.

Parameters:
  • vertices (numpy.ndarray like container of dimension Nx2) – Strip of segments end points (row, column) or (y, x)

  • shape (2-tuple of int) – size of the mask as (height, width)

Returns:

Mask corresponding to the polygon

Return type:

numpy.ndarray of dimension shape

class Polygon(vertices)#

Define a polygon that provides inside check and mask generation.

Parameters:

vertices (Nx2 array of floats of (row, col)) – corners of the polygon

is_inside(self, row, col)#

Check if (row, col) is inside or outside the polygon

Parameters:
  • row (float) –

  • col (float) –

Returns:

True if position is inside polygon, False otherwise

make_mask(self, int height, int width)#

Create a mask array representing the filled polygon

Parameters:
  • height (int) – Height of the mask array

  • width (int) – Width of the mask array

Returns:

2D array (height, width)