skimage2.morphology.medial_axis#

skimage2.morphology.medial_axis(image, mask=None, return_distance=False, *, rng=None)[source]#

Compute the medial axis transform of a binary image.

Parameters:
imagebinary ndarray, shape (M, N)

The image of the shape to skeletonize. If this input isn’t already a binary image, it gets converted into one: In this case, zero values are considered background (False), nonzero values are considered foreground (True).

maskbinary ndarray, shape (M, N), optional

If a mask is given, only those elements in image with a true value in mask are used for computing the medial axis.

return_distancebool, optional

If true, the distance transform is returned as well as the skeleton.

rng{numpy.random.Generator, int}, optional

Pseudo-random number generator. By default, a PCG64 generator is used (see numpy.random.default_rng()). If rng is an int, it is used to seed the generator.

The PRNG determines the order in which pixels are processed for tiebreaking.

Added in version 0.19.

Returns:
outndarray of bools

Medial axis transform of the image

distndarray of ints, optional

Distance transform of the image (only returned if return_distance is True)

See also

skeletonize, thin

Notes

This algorithm computes the medial axis transform of an image as the ridges of its distance transform.

The different steps of the algorithm are as follows
  • A lookup table is used, that assigns 0 or 1 to each configuration of the 3x3 binary square, whether the central pixel should be removed or kept. We want a point to be removed if it has more than one neighbor and if removing it does not change the number of connected components.

  • The distance transform to the background is computed, as well as the cornerness of the pixel.

  • The foreground (value of 1) points are ordered by the distance transform, then the cornerness.

  • A cython function is called to reduce the image to its skeleton. It processes pixels in the order determined at the previous step, and removes or maintains a pixel according to the lookup table. Because of the ordering, it is possible to process all pixels in only one pass.

Examples

>>> square = np.zeros((7, 7), dtype=bool)
>>> square[1:-1, 2:-2] = 1
>>> square.view(np.uint8)
array([[0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
>>> medial_axis(square).view(np.uint8)
array([[0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 1, 0, 0],
       [0, 0, 0, 1, 0, 0, 0],
       [0, 0, 0, 1, 0, 0, 0],
       [0, 0, 0, 1, 0, 0, 0],
       [0, 0, 1, 0, 1, 0, 0],
       [0, 0, 0, 0, 0, 0, 0]], dtype=uint8)