pyEdgeEval.preprocess.thin package

Submodules

pyEdgeEval.preprocess.thin.bwmorph_thin module

Credit to: @joefutrelle

https://gist.github.com/joefutrelle/562f25bbcf20691217b8

pyEdgeEval.preprocess.thin.bwmorph_thin.bwmorph_thin(image, n_iter=None)[source]

Perform morphological thinning of a binary image

Parameters
  • image (np.ndarray) – The image to be thinned (binary (M, N) ndarray).

  • n_iter (Optional[int]) – Regardless of the value of this parameter, the thinned image is returned immediately if an iteration produces no change. If this parameter is specified it thus sets an upper bound on the number of iterations performed.

Returns

out (np.ndarray) – Thinned image (ndarray of bools).

See also

skeletonize

Notes

This algorithm [1] works by making multiple passes over the image, removing pixels matching a set of criteria designed to thin connected regions while preserving eight-connected components and 2 x 2 squares [2]. In each of the two sub-iterations the algorithm correlates the intermediate skeleton image with a neighborhood mask, then looks up each neighborhood in a lookup table indicating whether the central pixel should be deleted in that sub-iteration.

References

Examples

>>> square = np.zeros((7, 7), dtype=np.uint8)
>>> square[1:-1, 2:-2] = 1
>>> square[0,1] =  1
>>> square
array([[0, 1, 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)
>>> skel = bwmorph_thin(square)
>>> skel.astype(np.uint8)
array([[0, 1, 0, 0, 0, 0, 0],
    [0, 0, 1, 0, 0, 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, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0]], dtype=uint8)

pyEdgeEval.preprocess.thin.thin module

Credit to @Britefury

https://github.com/Britefury/py-bsds500/blob/master/bsds/thin.py

pyEdgeEval.preprocess.thin.thin.binary_image_to_lut_indices(x)[source]

Convert a binary image to an index image that can be used with a lookup table to perform morphological operations. Non-zero elements in the image are interpreted as 1, zero elements as 0

Parameters

x (np.ndarray) – a 2D NumPy array

Returns

np.ndarray – a 2D NumPy array, same shape as x

pyEdgeEval.preprocess.thin.thin.apply_lut(x, lut)[source]

Perform a morphological operation on the binary image x using the supplied lookup table

Parameters
  • x – input array

  • lut – lookup table

pyEdgeEval.preprocess.thin.thin.identity_lut()[source]

Create identity lookup tablef

pyEdgeEval.preprocess.thin.thin.lut_masks_zero(neigh)[source]

Create a LUT index mask for which the specified neighbour is 0

Parameters

neigh – neighbour index; counter-clockwise from 1 staring at the eastern neighbour

Returns

a LUT index mask

pyEdgeEval.preprocess.thin.thin.lut_masks_one(neigh)[source]

Create a LUT index mask for which the specified neighbour is 1

Parameters

neigh – neighbour index; counter-clockwise from 1 staring at the eastern neighbour

Returns

a LUT index mask

pyEdgeEval.preprocess.thin.thin.binary_thin(x, max_iter=None)[source]

Binary thinning morphological operation

Parameters
  • x – a binary image, or an image that is to be converted to a binary image

  • max_iter (Optional[int]) – maximum number of iterations; default is None that results in an infinite number of iterations (note that binary_thin will automatically terminate when no more changes occur)

Returns

bool mask

Module contents

pyEdgeEval.preprocess.thin.bwmorph_thin(image, n_iter=None)[source]

Perform morphological thinning of a binary image

Parameters
  • image (np.ndarray) – The image to be thinned (binary (M, N) ndarray).

  • n_iter (Optional[int]) – Regardless of the value of this parameter, the thinned image is returned immediately if an iteration produces no change. If this parameter is specified it thus sets an upper bound on the number of iterations performed.

Returns

out (np.ndarray) – Thinned image (ndarray of bools).

See also

skeletonize

Notes

This algorithm [1] works by making multiple passes over the image, removing pixels matching a set of criteria designed to thin connected regions while preserving eight-connected components and 2 x 2 squares [2]. In each of the two sub-iterations the algorithm correlates the intermediate skeleton image with a neighborhood mask, then looks up each neighborhood in a lookup table indicating whether the central pixel should be deleted in that sub-iteration.

References

Examples

>>> square = np.zeros((7, 7), dtype=np.uint8)
>>> square[1:-1, 2:-2] = 1
>>> square[0,1] =  1
>>> square
array([[0, 1, 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)
>>> skel = bwmorph_thin(square)
>>> skel.astype(np.uint8)
array([[0, 1, 0, 0, 0, 0, 0],
    [0, 0, 1, 0, 0, 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, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
pyEdgeEval.preprocess.thin.binary_thin(x, max_iter=None)[source]

Binary thinning morphological operation

Parameters
  • x – a binary image, or an image that is to be converted to a binary image

  • max_iter (Optional[int]) – maximum number of iterations; default is None that results in an infinite number of iterations (note that binary_thin will automatically terminate when no more changes occur)

Returns

bool mask