Skip to content

digitopo.skeleton

Homotopic skeletons and parallel thinnings of binary images (2D and 3D).

The functions and parameters are those of pink26.skeleton, and the results are the same point by point: digitopo examines the points in the order of Pink's lskelubp2, lskelubp3d2, lskelcurv and lskelcurv3d, and ports Pink's parallel thinnings (lskelpar.c, lskelpar3d.c).

A homotopic thinning removes simple points one at a time, so the result has the topology of the input. Among the simple points, the one with the lowest priority goes first; by default the priority is the squared Euclidean distance to the background, so the object is peeled from its boundary inwards. Points of inhibit are never removed. The outside of the array is background.

zhang_wang_thinning and guo_hall_thinning are two classic 2D parallel thinnings, given for comparison: they have no topology proof.

References

G. Bertrand, M. Couprie, "Transformations topologiques discretes", in Geometrie discrete et images numeriques (D. Coeurjolly, A. Montanvert, J.-M. Chassery, eds), Hermes, 2007, pp. 187-209.

Y. Y. Zhang, P. S. P. Wang, "A modified parallel thinning algorithm", Proc. 9th ICPR, 1988, pp. 1023-1025.

Z. Guo, R. W. Hall, "Fast fully parallel thinning algorithms", CVGIP: Image Understanding 55(3), 1992, pp. 317-328.

ultimate_skeleton

ultimate_skeleton(
    image: ArrayLike,
    connectivity: int,
    priority: ArrayLike | None = None,
    inhibit: ArrayLike | None = None,
) -> NDArray[np.bool_]

Compute the ultimate homotopic skeleton of a binary image.

Repeats until no point changes: among the simple points of the object that are not in inhibit, remove the one with the lowest priority. A component without holes (2D), or without tunnels and cavities (3D), usually shrinks to a single point; an annulus becomes a closed curve.

Parameters:

Name Type Description Default
image array_like

2D or 3D array of bool or integers; any nonzero value is an object point.

required
connectivity int

4 or 8 in 2D, 6 or 26 in 3D. No default.

required
priority array_like

Same shape as image; the lowest values are removed first. By default, the squared Euclidean distance to the background. Only the order of the values matters.

None
inhibit array_like

Same shape as image; the nonzero points are never removed.

None

Returns:

Type Description
ndarray

New bool array, True on the skeleton.

Raises:

Type Description
TypeError

If image has a floating-point dtype, connectivity is not an integer, or priority or inhibit has an unsupported dtype.

ValueError

If image is not 2D or 3D, has an empty axis, or connectivity does not exist in its dimension; if priority or inhibit does not have the shape of image, or integer priorities do not fit in int32.

DigitopoError

If connectivity is 18, or the image is too large for the core.

Examples:

>>> import numpy as np, digitopo
>>> y, x = np.ogrid[-6:7, -6:7]
>>> int(digitopo.skeleton.ultimate_skeleton(x**2 + y**2 <= 25, 8).sum())
1

curvilinear_skeleton

curvilinear_skeleton(
    image: ArrayLike,
    connectivity: int,
    priority: ArrayLike | None = None,
    inhibit: ArrayLike | None = None,
) -> NDArray[np.bool_]

Compute the curvilinear homotopic skeleton of a binary image.

The same thinning as ultimate_skeleton, except that the points where the object is locally a curve (T > 1) are kept as well, so the branches stay as thin curves. As in Pink, the ends of the curves may move inward, and the transformation is not idempotent: on a curve one pixel thick, the end points are simple and are removed.

In 3D, Pink orders the points by 30 * priority + d, where d codes the background face neighbours: the differences between the priority values matter, not only their order. In 2D, the key is 10 * priority + d and the points are taken in batches of equal key / 10, truncated toward zero as in C: the priorities -1 and 0 then fall in the same batch. Use non-negative priorities (the default, the squared distance, is) to have one batch per priority value.

Parameters:

Name Type Description Default
image array_like

2D or 3D array of bool or integers; any nonzero value is an object point.

required
connectivity int

4 or 8 in 2D, 6 or 26 in 3D. No default.

required
priority array_like None
inhibit array_like

As for ultimate_skeleton: the nonzero points are never removed.

None

Returns:

Type Description
ndarray

New bool array, True on the skeleton.

Raises:

Type Description
TypeError

If image has a floating-point dtype, connectivity is not an integer, or priority or inhibit has an unsupported dtype.

ValueError

If image is not 2D or 3D, has an empty axis, or connectivity does not exist in its dimension; if priority or inhibit does not have the shape of image, or integer priorities do not fit in int32.

DigitopoError

If connectivity is 18, priority has more distinct values than Pink's keys can order, or the image is too large for the core.

Examples:

>>> import numpy as np, digitopo
>>> thin = np.zeros((3, 9), dtype=bool)
>>> thin[1, 1:8] = True
>>> digitopo.skeleton.curvilinear_skeleton(thin, 8)[1].tolist()
[False, False, True, True, True, True, True, False, False]

parallel_skeleton

parallel_skeleton(
    image: ArrayLike,
    connectivity: int,
    shape: str = "ultimate",
    symmetric: bool = True,
    max_steps: int | None = None,
    inhibit: ArrayLike | None = None,
) -> NDArray[np.bool_]

Thin a binary image in parallel with critical kernels.

Each step removes at once all the simple points, except that each small configuration of simple points whose complete removal would change the topology (two neighbouring pixels, two diagonal pixels, a 2 x 2 square; in 3D a few more) keeps at least one point. The result has the topology of the input.

The symmetric version keeps all the points of such configurations: a rotated or mirrored object gives the rotated or mirrored result, but a small core may remain (a 2 x 2 block for a square of even side). The asymmetric version keeps one point per configuration, chosen by a fixed rule, and reaches one point for a disk.

shape: "ultimate" thins as far as possible; "curvilinear" also keeps the points where the object is locally a curve; "surface" (3D) keeps the points where it is locally a surface.

The parameters and results are those of pink26.skeleton.parallel_skeleton. Combinations provided, with the function of Pink they reproduce (pink26's corrected versions in 3D):

=========== ========= ========= =========== shape symmetric 2D 3D =========== ========= ========= =========== ultimate True lskelMK2 lskelMK3 ultimate False lskelNK2b lskelAMK3 curvilinear True lskelCK2 lskelCK3a curvilinear False (none) lskelACK3a surface True (none) lskelSK3 =========== ========= ========= ===========

The transformation is not idempotent: applied again to its result, a curvilinear or surface skeleton may lose more points (its curves are two points thick in places, where the symmetric kernels keep both points). The topology is kept in every case.

Parameters:

Name Type Description Default
image array_like

2D or 3D array of bool or integers; any nonzero value is an object point.

required
connectivity int

8 in 2D, 26 in 3D (Pink defines these thinnings for these connectivities only; 4 and 6 raise digitopo.DigitopoError).

required
shape ('ultimate', 'curvilinear', 'surface')

What the result keeps ("surface" in 3D only).

"ultimate"
symmetric bool

Symmetric or asymmetric algorithm.

True
max_steps int or None

None or a negative value: until stability; 0: a copy; k: at most k parallel steps.

None
inhibit array_like

Object points that are never removed.

None

Returns:

Type Description
ndarray

New bool array, True on the result.

Raises:

Type Description
TypeError

If image has a floating-point dtype, or connectivity, symmetric or max_steps has the wrong type.

ValueError

If image is not 2D or 3D or has an empty axis, connectivity does not exist in its dimension, shape is unknown (or "surface" in 2D), max_steps does not fit in int32, or inhibit does not have the shape of image.

DigitopoError

If connectivity is the small one (4 or 6), Pink provides no such shape and symmetric combination in this dimension, or the image is too large for the core.

Examples:

>>> import numpy as np, digitopo
>>> bar = np.zeros((6, 10), dtype=bool)
>>> bar[2:4, 2:8] = True
>>> symmetric = digitopo.skeleton.parallel_skeleton(bar, 8)
>>> asymmetric = digitopo.skeleton.parallel_skeleton(bar, 8, symmetric=False)
>>> int(symmetric.sum()), int(asymmetric.sum())
(4, 1)

zhang_wang_thinning

zhang_wang_thinning(
    image: ArrayLike, max_steps: int | None = None
) -> NDArray[np.bool_]

Thin a 2D binary image with the Zhang-Wang parallel algorithm.

In each step, every object pixel that has 2 to 6 object neighbours (among its 8 neighbours) and whose neighbours, taken around it, form exactly one group of object pixels is removed, all at once, except where the object is two pixels thick (according to Pink's source comment, this is Zhang and Wang's correction of the older Zhang-Suen algorithm). The steps repeat until nothing changes. The object is 8-connected.

The result is pink26's (Pink's lskelzhangwang on a copy framed by 2 pixels of background).

Warning

This algorithm has no topology proof and Pink states that it may not preserve the topology: it can disconnect an object or erase a small part of it. It is given to compare with the critical-kernel thinnings (parallel_skeleton), which are proved.

Parameters:

Name Type Description Default
image array_like

2D array of bool or integers. Any nonzero value is an object point.

required
max_steps int or None

Maximal number of parallel steps; None (or any negative value) runs until no point changes, 0 returns a copy of the image.

None

Returns:

Type Description
ndarray

New bool array with the shape of image.

Raises:

Type Description
TypeError

If image has a floating-point dtype or max_steps is not an integer.

ValueError

If image is not a 2D array, has an axis of length zero, or max_steps does not fit in int32.

DigitopoError

If the image is too large for 32-bit point indices (about 4 x 10^9 points with the frame; pink26's limit is 2^31 pixels).

See Also

guo_hall_thinning, parallel_skeleton

References

Y. Y. Zhang, P. S. P. Wang, "A modified parallel thinning algorithm", Proc. 9th International Conference on Pattern Recognition, 1988, pp. 1023-1025. Pink function: lskelzhangwang.

Examples:

A bar 3 pixels thick becomes a thin line inside it:

>>> import numpy as np, digitopo
>>> bar = np.zeros((7, 14), dtype=bool)
>>> bar[2:5, 2:12] = True
>>> thin = digitopo.skeleton.zhang_wang_thinning(bar)
>>> bool((thin <= bar).all()) and 0 < int(thin.sum()) < int(bar.sum())
True

guo_hall_thinning

guo_hall_thinning(
    image: ArrayLike,
    variant: int = 1,
    max_steps: int | None = None,
) -> NDArray[np.bool_]

Thin a 2D binary image with the Guo-Hall parallel algorithm.

In each step, every object pixel on the border of the object (it has a background 4-neighbour) with more than 2 object neighbours, whose neighbours, taken around it, form exactly one group of object pixels, is removed, all at once, except some configurations that depend on the variant. The steps repeat until nothing changes. The object is 8-connected.

The result is pink26's (Pink's lskelguohall on a copy framed by 2 pixels of background).

Warning

Like zhang_wang_thinning, these algorithms are not proved to preserve the topology: they may disconnect an object or remove a component. They are given to compare with the critical-kernel thinnings (parallel_skeleton), which are proved.

Parameters:

Name Type Description Default
image array_like

2D array of bool or integers. Any nonzero value is an object point.

required
variant (1, 2, 3)

1: the first algorithm of Guo and Hall (a test on an L-shaped configuration two pixels away). 2: three tests on diagonal configurations instead. 3: variant 2, and two kinds of end pixels are removed too.

1
max_steps int or None

Maximal number of parallel steps; None (or any negative value) runs until no point changes, 0 returns a copy of the image.

None

Returns:

Type Description
ndarray

New bool array with the shape of image.

Raises:

Type Description
TypeError

If image has a floating-point dtype, or variant or max_steps is not an integer (or variant does not fit in a C int).

ValueError

If image is not a 2D array or has an axis of length zero, variant is not 1, 2 or 3, or max_steps does not fit in int32.

DigitopoError

If the image is too large for 32-bit point indices (about 4 x 10^9 points with the frame; pink26's limit is 2^31 pixels).

See Also

zhang_wang_thinning, parallel_skeleton

References

Z. Guo, R. W. Hall, "Fast fully parallel thinning algorithms", CVGIP: Image Understanding 55(3), 1992, pp. 317-328. Pink function: lskelguohall (variants 1, 2, 3 are the commands 8, 9, 10 of skelpar).

Examples:

A bar 3 pixels thick becomes a thin line inside it:

>>> import numpy as np, digitopo
>>> bar = np.zeros((7, 14), dtype=bool)
>>> bar[2:5, 2:12] = True
>>> thin = digitopo.skeleton.guo_hall_thinning(bar)
>>> bool((thin <= bar).all()) and 0 < int(thin.sum()) < int(bar.sum())
True