Skip to content

Digital topology in one page

This page defines the terms used by digitopo, with small examples. Each example is part of docs/examples/scripts/concepts.py, which also draws the figures.

Pixels, neighbours and connectivity pairs

A binary image is a set of pixels (voxels in 3D) with value 1, the object, and the other pixels, the background. In digitopo every nonzero value is an object point, and the outside of the array is background.

Two pixels are 4-adjacent when they share a side, and 8-adjacent when they share a side or a corner. In 3D, two voxels are 6-adjacent when they share a face, 18-adjacent when they share a face or an edge, and 26-adjacent when they share a face, an edge or a corner. A connected component is a maximal set of points linked by chains of adjacent points. The connectivity says which adjacency is used.

The 4 and 8 neighbours of a pixel, and a diamond of pixels that touch by their corners

The object and the background cannot use the same connectivity. The diamond above is made of pixels that touch only by their corners:

y, x = np.ogrid[-5:6, -5:6]
diamond = np.abs(x) + np.abs(y) == 4  # pixels that touch by their corners only

print("(8, 4):", topology.topology(diamond, 8))
print("(4, 8):", topology.topology(diamond, 4))
(8, 4): Topology(objects=1, holes=1, cavities=0, euler=0)
(4, 8): Topology(objects=16, holes=0, cavities=0, euler=16)

With the object 8-connected and the background 4-connected, the diamond is one closed curve and it encloses one hole. With the object 4-connected and the background 8-connected, it is 16 isolated pixels and the inside is not a hole: the background passes between the pixels. Both answers are consistent. If both used 8, the diamond would be a closed curve and yet the inside would be connected to the outside, through the corners. If both used 4, the 16 pixels would be separate and yet they would enclose a hole.

So digitopo uses a connectivity pair: (8, 4) or (4, 8) in 2D, (26, 6) or (6, 26) in 3D, the first number for the object and the second for the background. The argument connectivity is that of the object, and it is mandatory. 18 is refused for the binary functions.

scikit-image

In scikit-image, connectivity=1 is 4 (2D) or 6 (3D), and connectivity=image.ndim is 8 (2D) or 26 (3D).

The topology of a binary image is counted by topology.topology: the number of objects, of holes in 2D (tunnels in 3D, like the hole of a ring), of cavities in 3D (background enclosed in the object, like the inside of a hollow ball), and the Euler characteristic: objects minus holes (2D), or objects minus tunnels plus cavities (3D).

Simple points and topological numbers

A simple point is an object point whose removal does not change the topology: no object appears or disappears, no hole or tunnel or cavity appears, disappears or merges with another. Removing simple points, one after the other, is the basic step of every skeleton of digitopo.

Simplicity is a local property: it depends only on the 3 x 3 (3 x 3 x 3) neighbourhood of the point. G. Bertrand (1994) expressed it with two numbers:

  • the topological number \(T(p)\) is the number of connected components of the object, in the neighbourhood of \(p\) without \(p\), that are adjacent to \(p\);
  • \(\bar T(p)\) is the same number for the background, with the complementary connectivity.

In 2D the neighbourhood is the 8 pixels around \(p\). In 3D it is the 26 voxels around \(p\) for a 26-connected set, and the 18 face or edge voxels for a 6-connected set (Bertrand's geodesic neighbourhood). An object point is simple if and only if \(T(p) = 1\) and \(\bar T(p) = 1\).

shape = np.zeros((9, 13), bool)
shape[1:8, 1:6] = True        # a block
shape[4, 6:11] = True         # a line leaving it
shape[2:7, 10:12] = True      # a small block at the end of the line
shape[4, 2] = False           # a hole in the first block

t, t_bar = topology.topological_numbers(shape, 8)
simple = topology.simple_points(shape, 8)
types = topology.point_types(shape, 8)

points = {"corner": (1, 1), "line": (4, 8), "wall of the hole": (4, 1), "inside": (6, 3)}
for name, (r, c) in points.items():
    kind = topology.PointType(types[r, c]).name
    print(f"{name:17s} T={t[r, c]} T_bar={t_bar[r, c]} simple={simple[r, c]} {kind}")
print("simple points:", int(simple.sum()), "of", int(shape.sum()))
corner            T=1 T_bar=1 simple=True SIMPLE
line              T=2 T_bar=2 simple=False SEPARATING
wall of the hole  T=2 T_bar=2 simple=False SEPARATING
inside            T=1 T_bar=0 simple=False INTERIOR
simple points: 30 of 48

Simple points of a small shape in blue, and the type of every point

A point of the thin line has \(T = 2\): removing it would cut the object in two. The pixel between the hole and the outside has \(\bar T = 2\): removing it would merge the hole with the outside. An interior point has \(\bar T = 0\): removing it would create a hole. topology.point_types gives these types with the codes of pink26: SIMPLE (\(T = 1\), \(\bar T = 1\)), MULTIPLE (\(T > 2\)), SEPARATING (\(T = 2\)), ISOLATED (\(T = 0\)), INTERIOR (\(\bar T = 0\)) and OTHER, tested in this order.

Why a skeleton must keep the topology

A skeleton is a thin version of an object, used to measure it: the number of branches of a root or a vessel tree, the number of loops of a network, the length of a fibre. These counts are only right if the skeleton has the topology of the object: one piece for one object, one loop for one hole. A skeleton that cuts a branch or closes a loop gives wrong measurements.

A thinning that removes only simple points, one at a time, keeps the topology: each removal keeps it. Such a transformation is called homotopic. Removing all the simple points at once is not safe, as this bar two pixels thick shows:

bar = np.zeros((6, 10), bool)
bar[2:4, 2:8] = True                                   # two pixels thick

all_simple = topology.simple_points(bar, 8)
naive = bar & ~all_simple                              # remove them all at once
sequential = skeleton.ultimate_skeleton(bar, 8)        # one at a time
kernel = skeleton.parallel_skeleton(bar, 8)            # critical kernels

print("simple points:", int(all_simple.sum()), "of", int(bar.sum()))
for name, result in (("all at once", naive), ("sequential", sequential),
                     ("critical kernels", kernel)):
    print(f"{name:17s} {int(result.sum())} pixels, {topology.topology(result, 8)}")
simple points: 12 of 12
all at once       0 pixels, Topology(objects=0, holes=0, cavities=0, euler=0)
sequential        1 pixels, Topology(objects=1, holes=0, cavities=0, euler=1)
critical kernels  4 pixels, Topology(objects=1, holes=0, cavities=0, euler=1)

A bar two pixels thick: all its points are simple; removed at once, nothing is left; sequential and critical-kernel thinnings keep one or four pixels

Every point of the bar is simple, but removing them all deletes the object. digitopo has two families of homotopic skeletons:

  • sequential skeletons (ultimate_skeleton, curvilinear_skeleton) remove one simple point at a time. The point removed first is the one with the lowest priority; by default the priority is the squared Euclidean distance to the background, so the object is peeled from its boundary inwards and the skeleton is centred. Points given in inhibit are never removed. The result depends on the order, and digitopo uses the order of Pink exactly.
  • parallel skeletons (parallel_skeleton) remove many points at each step, chosen so that the topology is kept. They use critical kernels (below).

The ultimate skeleton removes simple points until none is left: an object without holes usually shrinks to one point, an object with one hole to a closed curve. The curvilinear skeleton also keeps the points where the object is locally a curve (\(T > 1\) in the sequential case), so the branches of the object remain as thin curves. In 3D, a surface skeleton keeps the points where the object is locally a surface.

Critical kernels and parallel thinning

The theory of critical kernels (G. Bertrand, 2007) gives a condition for removing a set of points at once without changing the topology. Its statement uses cubical complexes (next section). In the terms of the docstring of skeleton.parallel_skeleton: each step removes all the simple points at once, except that each small configuration of simple points whose complete removal would change the topology keeps at least one point. In 2D, these critical configurations are two neighbouring pixels, two diagonal pixels and a 2 x 2 square; in 3D there are a few more. The bar above is such a case: its 2 x 2 squares cannot all go.

The symmetric version keeps all the points of the critical configurations. A rotated or mirrored object gives the rotated or mirrored result, but a small core may remain (here the 2 x 2 block in the middle of the bar). The asymmetric version keeps one point per configuration, chosen by a fixed rule, and can reach one point.

An isthmus is a point where the object is locally a curve (in 2D and 3D) or a surface (in 3D). The curvilinear parallel skeleton keeps the first kind, the surface skeleton the second. See the skeletons example.

The cubical-complex characterization

digitopo tests simplicity with Theorem 15 of M. Couprie and G. Bertrand (2009). A pixel is seen as a closed unit square (a voxel as a closed unit cube), with its faces: its sides and corners in 2D, its square faces, edges and corners in 3D. The attachment of a point is the set of the faces of its boundary that it shares with the rest of the object. For \(d = 2, 3, 4\), the point is simple if and only if the attachment is connected, the rest of the boundary is connected, and the Euler characteristic of the attachment equals 1. In 2D, the Euler characteristic of a set of sides and corners is the number of corners minus the number of sides.

The figure shows the attachment (orange) of the centre pixel (white, outlined) in three configurations, with the verdict of topology.simple_points:

Attachment of a pixel in three configurations: chi = 1 simple, chi = 2 not simple, chi = 0 not simple

A: 3 vertices, 2 edges, chi = 1, simple = True
B: 4 vertices, 2 edges, chi = 2, simple = False
C: 4 vertices, 4 edges, chi = 0, simple = False

In A the attachment is one piece (two sides and their three corners), with Euler characteristic 1: the pixel is simple. In B it is two separate sides: removing the pixel would cut the object. In C it is the whole boundary: removing the pixel would create a hole.

This theorem covers the connectivity pairs (8, 4) and (26, 6) and works in 4D as well. The pairs (4, 8) and (6, 26) follow by duality: a point is (4, 8)-simple in an object if and only if it is (8, 4)-simple in the complement of the object plus the point. digitopo checks that this test agrees with the topological numbers on all 256 2D configurations and all \(2^{26}\) 3D configurations, for both connectivities. In 3D, it uses Bertrand's topological numbers with early exits, which are faster and equal to the cube test on all these configurations; the cube test remains the reference and serves in 4D.

Homotopic operators on grey-level images

A grey-level image \(F\) is seen as a stack of binary images, its sections: for each level \(k\), the section is the set of the points \(p\) with \(F(p) \geq k\). An operator is homotopic when every section of the result has the topology of the same section of the input.

A point \(p\) is destructible when it is simple in the section of level \(F(p)\): lowering it to the largest value of its neighbours below \(F(p)\) keeps the topology of every section. The homotopic kernel (grayscale.homotopic_kernel) lowers destructible points, lowest level first, until none is left: the minima spread as much as possible and end separated by thin crest lines. The lambda-kernel (grayscale.lambda_kernel) also lowers peaks and passes of contrast at most \(\lambda\), so the minima and peaks of small contrast disappear: it is a filter, not a homotopic operator (except for \(\lambda = 0\)).

For these operators, connectivity is the connectivity of the minima (the dark regions); the sections (the bright regions) use the complementary one. On a 0/1 image, connectivity=4 thins like a binary 8-connected object.

relief = np.zeros((9, 15), np.uint8)
relief[1:8, 1:7] = 100
relief[3:6, 3:5] = 200                 # a peak on the first bump
relief[1:8, 8:14] = 100
relief[4, 10:12] = 30                  # a pit in the second bump

for level in (50, 150):
    section = relief >= level
    print(f"section >= {level}:", topology.topology(section, 8))
kernel = grayscale.homotopic_kernel(relief, 4)
for level in (50, 150):
    print(f"kernel, section >= {level}:", topology.topology(kernel >= level, 8))
section >= 50: Topology(objects=2, holes=1, cavities=0, euler=1)
section >= 150: Topology(objects=1, holes=0, cavities=0, euler=1)
kernel, section >= 50: Topology(objects=2, holes=1, cavities=0, euler=1)
kernel, section >= 150: Topology(objects=1, holes=0, cavities=0, euler=1)

A grey-level image, two of its sections, and its homotopic kernel

The kernel looks very different from the image, but its sections have the same topology: two objects and one hole at level 50, one object at level 150. The first bump becomes one point at the level of its peak (200); the second becomes a thin ring at level 100 around its pit, which spreads at level 30. See the grey-level example.

Topological watershed

Think of a grey-level image as a relief: dark points are low, bright points are high. The lower section at level \(k\) is the set of the points below \(k\); its connected components are the lakes at level \(k\). A point is W-destructible when lowering it by one level neither creates a lake, nor merges two lakes, nor makes one disappear: the lower points of its neighbourhood belong to exactly one lake. The topological watershed (G. Bertrand, 2005; M. Couprie, L. Najman and G. Bertrand, 2005) lowers W-destructible points until none is left. The result has the same minima, the same number of lakes at every level, and the same level of passes between the minima; each minimum fills its basin, and what remains between the basins are thin crest lines at the level of the passes.

On a profile (an image of one row):

s = np.arange(60)
profile = (120 + 60 * np.cos(s / 4.0) + 40 * np.cos(s / 9.0)).astype(np.uint8)
lowered = watershed.topological_watershed(profile[np.newaxis, :], 4)[0]
inner = np.arange(1, len(s) - 1)
crests = inner[(lowered[inner] > lowered[inner - 1]) & (lowered[inner] > lowered[inner + 1])]
print("crests at", crests.tolist(), "levels", lowered[crests].tolist(), "of", profile[crests].tolist())
print("minima levels:", sorted(set(lowered.tolist()) - set(lowered[crests].tolist())))
crests at [25, 51] levels [142, 211] of [142, 211]
minima levels: [38, 64, 124]

A grey-level profile and its topological watershed: the basins are flattened to their minima, two crests remain at the pass levels

The three basins are lowered to their minima (64, 38 and, at the right border, 124). Two crest points remain, at the level they had in the image: 142 is the pass between the first two basins, 211 between the last two. The image has no frame for the watershed: a minimum on the border of the image is a minimum like any other. See the watershed example.

References

  • G. Bertrand, "Simple points, topological numbers and geodesic neighborhoods in cubic grids", Pattern Recognition Letters 15(10):1003-1011, 1994.
  • G. Malandain, G. Bertrand, N. Ayache, "Topological segmentation of discrete surfaces", International Journal of Computer Vision 10(2):183-197, 1993.
  • M. Couprie and G. Bertrand, "New characterizations of simple points in 2D, 3D and 4D discrete spaces", IEEE Transactions on Pattern Analysis and Machine Intelligence 31(4):637-648, 2009. hal-00622393
  • G. Bertrand, "On critical kernels", Comptes Rendus Mathematique 345(7):363-367, 2007.
  • G. Bertrand and M. Couprie, "Two-dimensional parallel thinning algorithms based on critical kernels", Journal of Mathematical Imaging and Vision 31(1):35-56, 2008.
  • G. Bertrand and M. Couprie, "Transformations topologiques discretes", in D. Coeurjolly, A. Montanvert, J.-M. Chassery (eds), Geometrie discrete et images numeriques, Hermes, 2007, pp. 187-209.
  • G. Bertrand, J.-C. Everat, M. Couprie, "Image segmentation through operators based upon topology", Journal of Electronic Imaging 6(4):395-405, 1997.
  • M. Couprie, F. N. Bezerra, G. Bertrand, "Topological operators for grayscale image processing", Journal of Electronic Imaging 10(4):1003-1015, 2001.
  • G. Bertrand, "On topological watersheds", Journal of Mathematical Imaging and Vision 22(2-3):217-230, 2005.
  • M. Couprie, L. Najman, G. Bertrand, "Quasi-linear algorithms for the topological watershed", Journal of Mathematical Imaging and Vision 22(2-3):231-249, 2005.