Skip to content

Correspondence with pink26

pink26 is the C11 and Python update of Pink, the image processing library of Michel Couprie and Gilles Bertrand. digitopo is a rewrite in Rust of its digital topology part, with the same public API and the same results.

Same API

digitopo has the same submodules, function names, parameter names, defaults, return types, dtypes, errors and messages as pink26. Code written for one runs with the other by changing the import only:

from digitopo import skeleton, topology     # or: from pink26 import skeleton, topology

The correspondences:

pink26 digitopo
pink26.topology, pink26.skeleton, pink26.watershed, pink26.grayscale, pink26.distance, pink26.io the modules of the same names
Pink26Error DigitopoError (a subclass of ValueError)
no equivalent digitopo.evaluation (topology_errors, cl_dice)
no equivalent digitopo.measure (branches, prune)
no equivalent digitopo.interop (connectivity, skimage_connectivity, as_uint8), digitopo.watershed.watershed_labels
no equivalent convert=True of the grey-level functions (the default False keeps pink26's rule: uint8 only)
topology.euler_characteristic, topology.topology additions of digitopo in digitopo.topology

The names inherited from Pink are kept (for example inhibit, not keep). The connectivity is mandatory, as in pink26. One difference in the messages: a Guo-Hall variant outside the C int raises a TypeError in both libraries, but pink26's message is that of its binding library (nanobind).

Same results

The results are identical to pink26 point by point, and for files byte by byte. This is not an approximation of Pink's algorithms: digitopo examines the points in the same order as Pink.

  • The sequential skeletons follow the order of Pink's lskelubp2, lskelubp3d2, lskelcurv and lskelcurv3d, including Pink's red-black tree and its reallocations (src/pink_rbt.rs), so that equal priorities are resolved as in Pink.
  • parallel_skeleton gives Pink's lskelMK2, lskelNK2b and lskelCK2 in 2D, and pink26's corrected lskelMK3, lskelAMK3, lskelCK3a, lskelACK3a and lskelSK3 in 3D.
  • The watershed functions give Pink's lwshedtopo with pink26's second pass.
  • The grey-level operators port the Pink code that pink26 calls (lhtkern.c, lhtkern3d.c and llambdakern.c, with pink26's fix of the lambda-kernel), with its scan orders, queues and neighbour orders.
  • distance_map uses the neighbourhoods and weights of Pink's ldist and lchamfrein; io.read follows the C parsing rules of pink26's reader, and io.write writes the bytes of pink26.

The simplicity test, the topological numbers, the Euler characteristic and the distance map are written from the articles. See Design of the API for the provenance of each part.

How this is tested

  • All 256 2D and all \(2^{26}\) 3D configurations: the simplicity test of Couprie and Bertrand agrees with the topological numbers, for both connectivities (cargo test --release -- --ignored all_3d).
  • Python tests against independent references: a NumPy computation of the topological numbers, SciPy labelling, skimage.measure.euler_number.
  • tests/python/test_oracle_pink26.py and the other test_oracle_*.py files: every function of the API is compared point by point with pink26, in 2D and 3D, for the four connectivities, on smooth and noisy random images, with the default, integer and float priorities, with and without inhibit. These tests are skipped when pink26 is not installed.
  • In continuous integration, the oracle job builds pink26 and runs the whole suite, the comparisons included, on Linux and macOS (see Releasing).

Speed

Measured with benches/compare_pink26.py on a laptop with 11 cores (Apple M3 Pro), digitopo 0.1.0 against pink26 0.1.1: best of 5 runs (3 for the grey-level operators), connectivity 8 in 2D and 26 in 3D. The results are identical to pink26 in every case. Gain is the time of pink26 divided by the time of digitopo on 11 threads, its default. pink26 runs on one core: the 1 thread column (RAYON_NUM_THREADS=1) separates what comes from the algorithms from what comes from the cores.

Image Function pink26 digitopo, 1 thread digitopo, 11 threads Gain
2D 1024 x 1024 simple_points 0.009 s 0.004 s 0.001 s 9.0x
2D 1024 x 1024 topological_numbers 0.005 s 0.006 s 0.001 s 5.0x
2D 1024 x 1024 point_types 0.009 s 0.007 s 0.002 s 4.5x
2D 1024 x 1024 euler_characteristic 0.003 s 0.001 s
2D 1024 x 1024 topology 0.018 s 0.015 s
2D 1024 x 1024 ultimate_skeleton 0.18 s 0.18 s 0.17 s 1.1x
2D 1024 x 1024 curvilinear_skeleton 0.21 s 0.21 s 0.20 s 1.1x
2D 1024 x 1024 parallel_skeleton (ultimate, symmetric) 1.88 s 2.44 s 0.38 s 5.0x
3D 128^3 simple_points 1.09 s 0.044 s 0.007 s 156x
3D 128^3 topological_numbers 1.08 s 0.17 s 0.021 s 51x
3D 128^3 point_types 1.10 s 0.12 s 0.015 s 73x
3D 128^3 euler_characteristic 0.010 s 0.002 s
3D 128^3 topology 0.088 s 0.095 s
3D 128^3 ultimate_skeleton 1.56 s 0.71 s 0.66 s 2.4x
3D 128^3 curvilinear_skeleton 12.3 s 2.04 s 1.89 s 6.5x
3D 128^3 parallel_skeleton (ultimate, symmetric) 4.29 s 1.48 s 0.87 s 4.9x
2D 1024 x 1024 topological_watershed 0.61 s 0.28 s 0.28 s 2.2x
2D 1024 x 1024 watershed_lines 0.43 s 0.18 s 0.19 s 2.3x
3D 128^3 topological_watershed 6.32 s 3.21 s 3.43 s 1.8x
3D 128^3 watershed_lines 5.17 s 2.96 s 3.11 s 1.7x
2D 1024 x 1024 homotopic_kernel 0.11 s 0.10 s 0.10 s 1.1x
2D 1024 x 1024 homotopic_thinning 0.67 s 0.74 s 0.74 s 0.9x
3D 64^3 homotopic_kernel 2.03 s 0.22 s 0.22 s 9.3x
3D 64^3 homotopic_thinning 16.2 s 2.09 s 2.09 s 7.8x
2D 1024 x 1024 measure.branches 0.064 s 0.047 s
3D 128^3 measure.branches 0.11 s 0.045 s
2D 1024 x 1024 distance_map (squared_euclidean) 0.016 s 0.016 s 0.003 s 5.3x
2D 1024 x 1024 distance_map (chamfer) 0.014 s 0.011 s 0.011 s 1.3x
2D 1024 x 1024 distance_map (d4) 0.018 s 0.009 s 0.009 s 2.0x
2D 1024 x 1024 distance_map (d8) 0.027 s 0.011 s 0.011 s 2.5x
3D 128^3 distance_map (squared_euclidean) 0.050 s 0.058 s 0.009 s 5.6x
3D 128^3 distance_map (d6) 0.043 s 0.022 s 0.023 s 1.9x
3D 128^3 distance_map (d18) 0.12 s 0.034 s 0.035 s 3.6x
3D 128^3 distance_map (d26) 0.17 s 0.045 s 0.045 s 3.7x
2D 1024 x 1024 zhang_wang_thinning 0.072 s 0.048 s 0.048 s 1.5x
2D 1024 x 1024 guo_hall_thinning (version 1) 0.081 s 0.046 s 0.046 s 1.8x
2D 1024 x 1024 guo_hall_thinning (version 2) 0.079 s 0.046 s 0.050 s 1.6x
2D 1024 x 1024 guo_hall_thinning (version 3) 0.084 s 0.048 s 0.051 s 1.6x

The gains come from the algorithms and from the cores, not from Rust itself: compare the 1 thread column with pink26. On 1 thread, digitopo is slower than pink26 on the 2D parallel skeleton (2.44 s against 1.88 s), the 2D homotopic thinning (0.74 s against 0.67 s) and the 3D squared Euclidean distance map (0.058 s against 0.050 s); the other differences in favour of pink26 are within the noise of the measure.

The point-by-point functions, the Euler characteristic and the distance map run on all cores (rayon); set RAYON_NUM_THREADS to limit them. The sequential skeletons follow Pink's order point after point and use one core, apart from their distance map.

In 3D, the simplicity test uses Bertrand's topological numbers with early exits, which is faster than the cube test of Couprie and Bertrand and equal to it on all \(2^{26}\) configurations (exhaustive test); the cube test remains the reference, and serves in 4D.