Polygon Utilities

Drawing

Mahotas is not a package to generate images, but there are a few simple functions to draw lines and polygons on an image (the target image is known as the canvas in this documentation).

The simplest function is line: Give it two points and it draws a line between them. The implementation is simple, and in Python, so it will be slow for many complex usage.

The main purpose of these utilities is to aid debugging and visualisation. If you need to generate fancy graphs, look for packages such as matplotlib.

Convex Hull

Convex hull functions are a more typical image processing feature. Mahotas has a simple one, called convexhull. Given a boolean image (or anything that will get interpreted as a boolean image), it finds the convex hull of all its on points.

The implementation is in C++, so it is fast.

A companion function fill_convexhull returns the convex hull as a binary image.

API Documentation

mahotas.polygon.convexhull(bwimg)

Compute the convex hull as a polygon

This is an implementation of the Graham Scan: https://en.wikipedia.org/wiki/Graham_scan

Parameters:
bwimgndarray

input image (interpreted as boolean). Only 2D arrays are supported.

Returns:
hullndarray

Set of (y,x) coordinates of hull corners

mahotas.polygon.fill_convexhull(bwimg)

Compute the convex hull and return it as a binary mask

Parameters:
bwimageinput image (interpreted as boolean)
Returns:
hullimage of same size and dtype as bwimg with the hull filled in.
mahotas.polygon.fill_polygon([(y0, x0), (y1, x1), ..., ]canvas, color=1)

Draw a filled polygon in canvas

Parameters:
polygonlist of pairs

a list of (y,x) points

canvasndarray

where to draw, will be modified in place

colorinteger, optional

which colour to use (default: 1)

mahotas.polygon.line((y0, x0), (y1, x1), canvas, color=1)

Draw a line

Parameters:
p0pair of integers

first point

p1pair of integers

second point

canvasndarray

where to draw, will be modified in place

colorinteger, optional

which value to store on the pixels (default: 1)

Notes

Implementation Reference