Skip to content

digitopo.topology

Digital topology of binary images: topological numbers, simple points, point classes.

The functions, parameters, return types and codes are those of pink26.topology; euler_characteristic and topology are additions.

For a point p, T(p) counts the pieces of the object around p that touch p, and T̄(p) the same for the background with the complementary connectivity. Around p means the 8 pixels in 2D, the 26 voxels for a 26-connected 3D object and the 18 face or edge voxels for a 6-connected one (Bertrand's geodesic neighbourhood); the background of a 26-connected object is 6-connected and uses the 18 voxels, that of a 6-connected object uses the 26. An object point is simple when T = 1 and T̄ = 1.

Simplicity is tested with Theorem 15 of Couprie and Bertrand (2009), on the cubical complex of the pixels; for 4 and 6 connectivity by duality. The two characterizations agree on all 2D and 3D configurations (tests of the crate).

References

G. Bertrand, "Simple points, topological numbers and geodesic neighborhoods in cubic grids", Pattern Recognition Letters 15(10), 1994, pp. 1003-1011.

M. Couprie, G. Bertrand, "New characterizations of simple points in 2D, 3D, and 4D discrete spaces", IEEE TPAMI 31(4), 2009, pp. 637-648.

G. Malandain, G. Bertrand, N. Ayache, "Topological segmentation of discrete surfaces", International Journal of Computer Vision 10(2), 1993, pp. 183-197.

PointType

Bases: IntEnum

Type of a point, as returned by point_types (codes of pink26).

BACKGROUND (0) not in the object; SIMPLE (1) T = 1 and T̄ = 1; MULTIPLE (2) T > 2; SEPARATING (3) T = 2; ISOLATED (4) T = 0; INTERIOR (5) T̄ = 0; OTHER (6) any other object point, for example a voxel of a surface one voxel thick (T = 1, T̄ = 2). The rules are applied in this order.

PointClass3D

Bases: IntEnum

Class of a 3D point of a 26-connected object (codes of pink26).

Types A to I of Malandain, Bertrand and Ayache (1993), Table 1, in this order: INTERIOR T̄ = 0; ISOLATED T = 0; BORDER T̄ = 1, T = 1; CURVE T̄ = 1, T = 2; CURVES_JUNCTION T̄ = 1, T > 2; SURFACE T̄ = 2, T = 1; SURFACE_CURVE_JUNCTION T̄ = 2, T ≥ 2; SURFACES_JUNCTION T̄ > 2, T = 1; SURFACES_CURVES_JUNCTION T̄ > 2, T ≥ 2.

Topology

Bases: NamedTuple

Topology of a binary image, as returned by topology.

holes counts the holes of a 2D object and the tunnels of a 3D one (first Betti number); cavities is 0 in 2D. euler is objects - holes + cavities.

topological_numbers

topological_numbers(
    image: ArrayLike, connectivity: int
) -> tuple[NDArray[np.uint8], NDArray[np.uint8]]

Compute the topological numbers T and T̄ of every point, object or background.

T counts the pieces of the object around the point that touch it, T̄ the same for the background (see the module documentation).

Parameters:

Name Type Description Default
image array_like

2D array (rows, columns) or 3D array (depth, rows, columns) of bool or integers. Any nonzero value is an object point; the outside of the array is background.

required
connectivity int

Connectivity of the object: 4 or 8 in 2D, 6 or 26 in 3D. No default (the choice changes the result); 18 is refused. The background uses the other one.

required

Returns:

Type Description
t, t_bar : tuple of numpy.ndarray

Two new uint8 arrays with the shape of image.

Raises:

Type Description
TypeError

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

ValueError

If image is not 2D or 3D, has an empty axis, or connectivity does not exist in its dimension.

DigitopoError

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

Examples:

>>> import numpy as np, digitopo
>>> square = np.zeros((5, 5), dtype=bool)
>>> square[1:4, 1:4] = True
>>> t, t_bar = digitopo.topology.topological_numbers(square, 8)
>>> int(t[2, 2]), int(t_bar[2, 2])      # the centre: no background around it
(1, 0)
>>> int(t[1, 1]), int(t_bar[1, 1])      # a corner: one piece of each
(1, 1)

simple_points

simple_points(
    image: ArrayLike, connectivity: int
) -> NDArray[np.bool_]

Find the simple points: object points whose removal keeps the topology.

A point is simple when T = 1 and T̄ = 1. Background points are never simple.

Parameters:

Name Type Description Default
image array_like

2D, 3D or 4D array of bool or integers. Any nonzero value is an object point; the outside of the array is background.

required
connectivity int

Connectivity of the object: 4 or 8 in 2D, 6 or 26 in 3D, 8 or 80 in 4D. No default; 18 is refused.

required

Returns:

Type Description
ndarray

New bool array, True on the simple points.

Raises:

Type Description
TypeError

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

ValueError

If image is not 2D, 3D or 4D, has an empty axis, or connectivity does not exist in its dimension.

DigitopoError

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

Examples:

>>> import numpy as np, digitopo
>>> square = np.zeros((5, 5), dtype=bool)
>>> square[1:4, 1:4] = True
>>> digitopo.topology.simple_points(square, 8).astype(int)
array([[0, 0, 0, 0, 0],
       [0, 1, 1, 1, 0],
       [0, 1, 0, 1, 0],
       [0, 1, 1, 1, 0],
       [0, 0, 0, 0, 0]])

point_types

point_types(
    image: ArrayLike, connectivity: int
) -> NDArray[np.uint8]

Classify every point with the codes of PointType.

Parameters:

Name Type Description Default
image array_like

2D array (rows, columns) or 3D array (depth, rows, columns) of bool or integers. Any nonzero value is an object point; the outside of the array is background.

required
connectivity int

Connectivity of the object: 4 or 8 in 2D, 6 or 26 in 3D. No default (the choice changes the result); 18 is refused. The background uses the other one.

required

Returns:

Type Description
ndarray

New uint8 array of PointType codes.

Raises:

Type Description
TypeError

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

ValueError

If image is not 2D or 3D, has an empty axis, or connectivity does not exist in its dimension.

DigitopoError

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

Examples:

>>> import numpy as np, digitopo
>>> square = np.zeros((5, 5), dtype=bool)
>>> square[1:4, 1:4] = True
>>> types = digitopo.topology.point_types(square, 8)
>>> PointType = digitopo.topology.PointType
>>> PointType(types[2, 2]).name, PointType(types[1, 1]).name
('INTERIOR', 'SIMPLE')

point_classes_3d

point_classes_3d(
    image: ArrayLike, connectivity: int
) -> NDArray[np.uint8]

Classify every point of a 3D image with the codes of PointClass3D.

Parameters:

Name Type Description Default
image array_like

3D array (depth, rows, columns) of bool or integers.

required
connectivity int

Only 26 is defined.

required

Returns:

Type Description
ndarray

New uint8 array of PointClass3D codes.

Raises:

Type Description
TypeError

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

ValueError

If image is not 3D or has an empty axis.

DigitopoError

If connectivity is not 26 (point classes are defined for 26 only), or the image is too large for the core.

Examples:

>>> import numpy as np, digitopo
>>> plate = np.zeros((5, 5, 5), dtype=bool)
>>> plate[2, 1:4, 1:4] = True
>>> classes = digitopo.topology.point_classes_3d(plate, 26)
>>> digitopo.topology.PointClass3D(classes[2, 2, 2]).name
'SURFACE'

end_points

end_points(
    image: ArrayLike, connectivity: int
) -> NDArray[np.bool_]

Find the end points: object points with exactly 1 object neighbour.

Meaningful on a thin object such as a skeleton.

Parameters:

Name Type Description Default
image array_like

2D array (rows, columns) or 3D array (depth, rows, columns) of bool or integers. Any nonzero value is an object point; the outside of the array is background.

required
connectivity int

Connectivity of the object: 4 or 8 in 2D, 6 or 26 in 3D. No default (the choice changes the result); 18 is refused. The background uses the other one.

required

Returns:

Type Description
ndarray

New bool array, True on the end points.

Raises:

Type Description
TypeError

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

ValueError

If image is not 2D or 3D, has an empty axis, or connectivity does not exist in its dimension.

DigitopoError

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

Examples:

>>> import numpy as np, digitopo
>>> y = np.zeros((7, 7), dtype=bool)          # a "Y" with three arms
>>> y[3, 3] = True
>>> for k in (1, 2, 3):
...     y[3 + k, 3] = y[3 - k, 3 - k] = y[3 - k, 3 + k] = True
>>> np.argwhere(digitopo.topology.end_points(y, 8)).tolist()
[[0, 0], [0, 6], [6, 3]]

curve_points

curve_points(
    image: ArrayLike, connectivity: int
) -> NDArray[np.bool_]

Find the curve points: T = 2 and exactly 2 object neighbours.

Parameters:

Name Type Description Default
image array_like

2D array (rows, columns) or 3D array (depth, rows, columns) of bool or integers. Any nonzero value is an object point; the outside of the array is background.

required
connectivity int

Connectivity of the object: 4 or 8 in 2D, 6 or 26 in 3D. No default (the choice changes the result); 18 is refused. The background uses the other one.

required

Returns:

Type Description
ndarray

New bool array, True on the curve points.

Raises:

Type Description
TypeError

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

ValueError

If image is not 2D or 3D, has an empty axis, or connectivity does not exist in its dimension.

DigitopoError

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

Examples:

>>> import numpy as np, digitopo
>>> curve = np.zeros((7, 7), dtype=bool)      # a closed curve, one point thick
>>> for p in [(1, 3), (2, 2), (2, 4), (3, 1), (3, 5), (4, 2), (4, 4), (5, 3)]:
...     curve[p] = True
>>> int(digitopo.topology.curve_points(curve, 8).sum()), int(curve.sum())
(8, 8)

junction_points

junction_points(
    image: ArrayLike, connectivity: int
) -> NDArray[np.bool_]

Find the junction points: object points with at least 3 object neighbours.

Meaningful on a thin skeleton: on a thick object nearly every point counts.

Parameters:

Name Type Description Default
image array_like

2D array (rows, columns) or 3D array (depth, rows, columns) of bool or integers. Any nonzero value is an object point; the outside of the array is background.

required
connectivity int

Connectivity of the object: 4 or 8 in 2D, 6 or 26 in 3D. No default (the choice changes the result); 18 is refused. The background uses the other one.

required

Returns:

Type Description
ndarray

New bool array, True on the junction points.

Raises:

Type Description
TypeError

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

ValueError

If image is not 2D or 3D, has an empty axis, or connectivity does not exist in its dimension.

DigitopoError

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

Examples:

>>> import numpy as np, digitopo
>>> y = np.zeros((7, 7), dtype=bool)          # a "Y" with three arms
>>> y[3, 3] = True
>>> for k in (1, 2, 3):
...     y[3 + k, 3] = y[3 - k, 3 - k] = y[3 - k, 3 + k] = True
>>> np.argwhere(digitopo.topology.junction_points(y, 8)).tolist()
[[3, 3]]

euler_characteristic

euler_characteristic(
    image: ArrayLike, connectivity: int
) -> int

Return the Euler characteristic of the object for its connectivity.

2D: objects - holes. 3D: objects - tunnels + cavities. It equals skimage.measure.euler_number(image, connectivity=1) for connectivity 4 or 6 and connectivity=image.ndim for 8 or 26.

Parameters:

Name Type Description Default
image array_like

2D array (rows, columns) or 3D array (depth, rows, columns) of bool or integers. Any nonzero value is an object point; the outside of the array is background.

required
connectivity int

Connectivity of the object: 4 or 8 in 2D, 6 or 26 in 3D. No default (the choice changes the result); 18 is refused. The background uses the other one.

required

Returns:

Type Description
int

Raises:

Type Description
TypeError

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

ValueError

If image is not 2D or 3D, has an empty axis, or connectivity does not exist in its dimension.

DigitopoError

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

Examples:

>>> import numpy as np, digitopo
>>> curve = np.zeros((7, 7), dtype=bool)      # a closed curve, one point thick
>>> for p in [(1, 3), (2, 2), (2, 4), (3, 1), (3, 5), (4, 2), (4, 4), (5, 3)]:
...     curve[p] = True
>>> digitopo.topology.euler_characteristic(curve, 8)   # 1 object - 1 hole
0

topology

topology(image: ArrayLike, connectivity: int) -> Topology

Count the objects, holes (2D) or tunnels (3D) and cavities of a binary image.

The objects use connectivity, the holes and cavities the complementary one; the outside of the array is background.

Parameters:

Name Type Description Default
image array_like

2D array (rows, columns) or 3D array (depth, rows, columns) of bool or integers. Any nonzero value is an object point; the outside of the array is background.

required
connectivity int

Connectivity of the object: 4 or 8 in 2D, 6 or 26 in 3D. No default (the choice changes the result); 18 is refused. The background uses the other one.

required

Returns:

Type Description
Topology

(objects, holes, cavities, euler).

Raises:

Type Description
TypeError

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

ValueError

If image is not 2D or 3D, has an empty axis, or connectivity does not exist in its dimension.

DigitopoError

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

Examples:

>>> import numpy as np, digitopo
>>> ring = np.ones((5, 5), dtype=bool)
>>> ring[2, 2] = False
>>> digitopo.topology.topology(ring, 8)
Topology(objects=1, holes=1, cavities=0, euler=0)