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
|
|
length |
ndarray
|
|
kind |
ndarray
|
|
paths |
list of numpy.ndarray
|
For each branch, the |
labels |
ndarray
|
|
skeleton |
ndarray
|
The |
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 |
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
|
None
|
thin
|
bool
|
Thin the skeleton first: remove its simple points, except its end
points ( |
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 |
ValueError
|
If |
DigitopoError
|
If |
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 ( |
required |
connectivity
|
int
|
4 or 8 in 2D, 6 or 26 in 3D, as for |
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 |
None
|
thin
|
bool
|
As for |
True
|
Returns:
| Type | Description |
|---|---|
ndarray
|
New |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
DigitopoError
|
If |
Examples:
A segment of 21 pixels with a spur of 2 pixels in its middle: