Skip to content

digitopo.measure

Measurements on skeletons: branches and their lengths (2D and 3D).

This module is an addition of digitopo; it has no equivalent in pink26.

A thin skeleton (for example the result of digitopo.skeleton.parallel_skeleton with shape="curvilinear") is cut into branches at its nodes:

  • an end point has exactly one skeleton neighbour;
  • a junction is a connected group of points with three skeleton neighbours or more: when two branches leave a junction through two sides of a pixel that share a corner, several neighbouring points have three neighbours, and they form one junction.

A branch is the chain of points between two nodes, both included. Its length is the sum of the Euclidean distances between the centres of its successive points, in the units of spacing: a step along an axis counts spacing[axis], a diagonal step the square root of the sum of the squares. This chain length overestimates oblique curves by a few percent (up to about 8 % for a straight line at 22.5 degrees in 2D).

Two points of the skeleton are linked when they are neighbours for the connectivity and no other skeleton point lies in the box that they span (the m-adjacency of Gonzalez and Woods, extended to 3D): a staircase of an 8-connected skeleton is then a chain, not a row of junctions.

A hole may be surrounded by junction points only, for example a one-pixel hole in the skeleton of a noisy image. Each such hole gives an INTERNAL branch from the junction to itself: the closed path round the hole through the junction points. The branches thus keep the cycles of the skeleton, one independent loop per hole (per tunnel in 3D).

BranchKind

Bases: IntEnum

Kind of a branch, as stored in Branches.kind.

TERMINAL (1): from an end point to a junction. INTERNAL (2): between two junctions, possibly the same one (a loop on a junction, including the path round a hole surrounded by junction points only). ISOLATED (3): between two end points (a lone curve), or a single point. CYCLE (4): a closed curve with no end point and no junction.

Branches dataclass

Branches of a skeleton, as returned by branches.

Attributes:

Name Type Description
start, end ndarray

(n, ndim) integer coordinates of the first and last point of each branch. A TERMINAL branch starts at its end point. For a CYCLE, the start and the end are the same point.

length ndarray

(n,) lengths, in the units of spacing.

kind ndarray

(n,) uint8 codes of BranchKind.

paths list of numpy.ndarray

For each branch, the (m, ndim) coordinates of its points, in order.

labels ndarray

int32 image of the shape of the skeleton: 0 outside the skeleton, k on the points of branch k - 1 (labels start at 1), -1 on the junction points, which belong to several branches.

skeleton ndarray

The bool skeleton that was measured: the input, thinned when thin=True.

count

count(kind: BranchKind) -> int

Number of branches of the given kind.

branches

branches(
    skeleton: ArrayLike,
    connectivity: int,
    *,
    spacing: ArrayLike | None = None,
    thin: bool = True,
) -> Branches

Cut a thin skeleton into branches and measure them.

Parameters:

Name Type Description Default
skeleton array_like

2D or 3D array of bool or integers; any nonzero value is a skeleton point. It should be thin (one point wide), as given by the skeletons of digitopo.skeleton; on a thick object nearly every point is a junction.

required
connectivity int

Adjacency of the skeleton points: 4 or 8 in 2D, 6 or 26 in 3D. Use the connectivity used to compute the skeleton.

required
spacing array_like

Physical size of a step along each axis, for example the voxel size (dz, dy, dx) of a volume. By default 1 on every axis.

None
thin bool

Thin the skeleton first: remove its simple points, except its end points (digitopo.skeleton.ultimate_skeleton with the end points inhibited). The topology and the end points are kept; the points that make a curve two points thick, or a 2 x 2 block, go. The symmetric parallel skeletons keep such points, which would otherwise appear as junctions or small cycles. Set False for a skeleton that is already thin.

True

Returns:

Type Description
Branches

Warns:

Type Description
UserWarning

When a component of the skeleton is made of junction points only and surrounds no hole (a block, or the surface left round a cavity): it is not a thin curve and gives no branch. Its points are labelled -1.

Raises:

Type Description
TypeError

If skeleton has a floating-point dtype or connectivity is not an integer.

ValueError

If skeleton is not 2D or 3D, has an empty axis, or connectivity does not exist in its dimension, or spacing does not hold one positive finite value per axis.

DigitopoError

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

Examples:

A "Y" with three branches of 3 pixels around a centre:

>>> import numpy as np, digitopo
>>> y = np.zeros((11, 11), dtype=bool)
>>> y[5, 5] = True
>>> for k in (1, 2, 3):
...     y[5 + k, 5] = y[5 - k, 5 - k] = y[5 - k, 5 + k] = True
>>> b = digitopo.measure.branches(y, 8)
>>> len(b), b.count(digitopo.measure.BranchKind.TERMINAL)
(3, 3)
>>> np.round(np.sort(b.length), 3).tolist()
[3.0, 4.243, 4.243]

prune

prune(
    skeleton: ArrayLike,
    connectivity: int,
    min_length: float,
    *,
    spacing: ArrayLike | None = None,
    thin: bool = True,
) -> NDArray[np.bool_]

Remove the short terminal branches (spurs) of a skeleton.

One pass: every TERMINAL branch shorter than min_length (in the units of spacing) loses its points, except its junction point. The other branches are kept: removing an ISOLATED branch would delete an object, an INTERNAL or CYCLE branch would cut the network or open a cycle. The topology of the skeleton therefore does not change. Small noise on the boundary of an object gives such spurs; a branch that becomes terminal after a pass can be removed by calling prune again.

With thin=True (the default), the junction point of an 8-connected line is simple and the thinning removes it first: the junction moves one pixel into the spur, and a bump of one pixel remains after pruning.

Parameters:

Name Type Description Default
skeleton array_like

2D or 3D thin skeleton (bool or integers).

required
connectivity int

4 or 8 in 2D, 6 or 26 in 3D, as for branches.

required
min_length float

Terminal branches strictly shorter than this length are removed.

required
spacing array_like

Physical size of a step along each axis, as for branches.

None
thin bool

As for branches: thin the skeleton first.

True

Returns:

Type Description
ndarray

New bool array: the (thinned) skeleton without its short spurs.

Raises:

Type Description
TypeError

If skeleton has a floating-point dtype or connectivity is not an integer.

ValueError

If skeleton is not 2D or 3D, has an empty axis, or connectivity does not exist in its dimension, spacing does not hold one positive finite value per axis, or min_length is negative or not finite.

DigitopoError

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

Examples:

A segment of 21 pixels with a spur of 2 pixels in its middle:

>>> import numpy as np, digitopo
>>> skeleton = np.zeros((9, 25), dtype=bool)
>>> skeleton[5, 2:23] = True
>>> skeleton[3:5, 12] = True
>>> pruned = digitopo.measure.prune(skeleton, 8, min_length=3)
>>> int(skeleton.sum()), int(pruned.sum())
(23, 21)
>>> len(digitopo.measure.branches(pruned, 8))
1