Skip to content

digitopo

digitopo is a library of digital topology operators for binary and grey-level images in 2D and 3D (4D for the simple points). It is written in Rust and used from Python with NumPy arrays.

It provides:

  • the local topology of binary images: simple points, topological numbers, point types, end and junction points, Euler characteristic, number of objects, holes, tunnels and cavities;
  • skeletons that keep the topology of the object: sequential (ultimate or curvilinear, with a priority and points to keep) and parallel (critical kernels, symmetric or not, curves or surfaces in 3D);
  • the topological watershed of a grey-level image, and watershed lines from markers;
  • homotopic operators on grey-level images: thinning, thickening, kernel and lambda-kernel;
  • distance maps (squared Euclidean, chamfer, d4, d8, d6, d18, d26);
  • reading and writing images in the PGM format of the Pink library;
  • measurements on skeletons: branches, their kinds and physical lengths, pruning of short spurs;
  • a topological evaluation of a segmentation: topology errors and clDice.

The public API is that of pink26, the C11 and Python update of Pink, the image processing library of Michel Couprie and Gilles Bertrand. The results are identical to those of pink26 point by point (see Correspondence with pink26). The modules measure, evaluation and interop, and watershed.watershed_labels, are additions of digitopo.

The changes are listed in the changelog.

Installation

digitopo needs Python 3.11 or later and NumPy 1.24 or later.

Install it from PyPI (wheels for Linux, macOS and Windows):

pip install digitopo

To install it from the source, you need a Rust toolchain (1.88 or later, from rustup.rs); pip builds the extension with maturin:

git clone https://github.com/alainlebret/digitopo.git
cd digitopo
pip install .            # or: pip install ".[test]" to run the tests

To build this documentation, install the docs extra and run MkDocs from the root of the repository:

pip install ".[docs]"
mkdocs build --strict    # the site is written to site/
mkdocs serve             # or: browse it at http://127.0.0.1:8000

A first example

A disk with a hole is an object with one hole. Its ultimate skeleton is a thin set of pixels, inside the object, with the same topology and no simple point left (see concepts): a closed curve.

y, x = np.ogrid[-12:13, -12:13]
ring = (x**2 + y**2 <= 100) & (x**2 + y**2 >= 25)  # a disk with a hole
curve = skeleton.ultimate_skeleton(ring, 8)  # 8-connected object

print("ring: ", int(ring.sum()), "pixels,", topology.topology(ring, 8))
print("curve:", int(curve.sum()), "pixels,", topology.topology(curve, 8))

Output:

ring:  248 pixels, Topology(objects=1, holes=1, cavities=0, euler=0)
curve: 42 pixels, Topology(objects=1, holes=1, cavities=0, euler=0)

A ring of grey pixels and its ultimate skeleton, a closed curve of blue pixels

connectivity is mandatory: here 8 means that two object pixels are neighbours when they share a side or a corner. The concepts page explains this choice, simple points and the other terms used in this documentation.

The whole script (with the figure)
# SPDX-License-Identifier: CECILL-2.1
# Figure of docs/index.md. This script is the source of truth: the page
# includes it with pymdownx.snippets. Run it from the repository root:
#     python docs/examples/scripts/first_example.py
from pathlib import Path

import matplotlib

matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
from digitopo import skeleton, topology

y, x = np.ogrid[-12:13, -12:13]
ring = (x**2 + y**2 <= 100) & (x**2 + y**2 >= 25)  # a disk with a hole
curve = skeleton.ultimate_skeleton(ring, 8)  # 8-connected object

print("ring: ", int(ring.sum()), "pixels,", topology.topology(ring, 8))
print("curve:", int(curve.sum()), "pixels,", topology.topology(curve, 8))

FIGURES = Path(__file__).resolve().parents[2] / "figures"


def overlay(obj, thin):
    """White background, object in light grey, thin result in blue."""
    rgb = np.ones(obj.shape + (3,))
    rgb[obj] = (0.85, 0.85, 0.85)
    rgb[thin] = (0.145, 0.490, 0.878)  # #257de0
    return rgb


fig, axes = plt.subplots(1, 2, figsize=(5.2, 2.8), facecolor="white",
                         edgecolor="#bbbbbb", linewidth=1)
axes[0].imshow(overlay(ring, np.zeros_like(ring)), interpolation="nearest")
axes[0].set_title("ring: 1 object, 1 hole", fontsize=9)
axes[1].imshow(overlay(ring, curve), interpolation="nearest")
axes[1].set_title("ultimate_skeleton(ring, 8)", fontsize=9)
for ax in axes:
    ax.set_axis_off()
fig.tight_layout()
fig.savefig(FIGURES / "first-example.png", dpi=100)

Where to go next

The figures of these pages are made by the scripts of docs/examples/scripts/. Each page includes its script, so the code shown is the code that made the figure. Run a script from the root of the repository to make its figures again, for example python docs/examples/scripts/skeletons.py.

Licence and credits

digitopo is distributed under the CeCILL v2.1 licence (LICENSE, French version in LICENCE-fr), a free software licence compatible with the GNU GPL.

digitopo is based on the work of Gilles Bertrand and Michel Couprie (ESIEE Paris, LIGM) on digital topology, and on Pink, their image processing library. pink26, the C11 and Python update of Pink, serves as the reference for the tests.