Digital topology for teaching: a C11 + Python rewrite of Pink, the image-processing library by Michel Couprie, Gilles Bertrand (ESIEE), and many contributors.
C API reference: https://alainlebret.github.io/pink26/ (built with make docs).
License
CeCILL (see LICENSE).
The code written for pink26 is Copyright Alain Lebret (2026). The files taken from Pink keep their original copyright notice (Copyright ESIEE): the admitted modules of src/legacy/ and the headers of src/legacy/include/ carry a "Repris de Pink" line naming their source file and Pink commit; the frozen copies of tests/oracle/pink_ref/ are listed in tests/oracle/pink_ref/VENDORED.txt.
Credits
Based on Pink by Michel Couprie, Gilles Bertrand and the Pink contributors (https://github.com/hugues-talbot/pink).
The C tests use minunit by David Siñuela Pastor (MIT license, tests/include/MINUNIT-LICENSE.txt).
Usage
make test # build and run the C tests
pip install . # install the Python package
import numpy as np
import pink26
image = np.zeros((5, 7), dtype=np.uint8)
image[2, 3] = 255
pink26.io.write("dot.pgm", image)
print(pink26.io.read("dot.pgm").shape)
Simple points of a binary image (Bertrand 1994), here with 8-connectivity:
import pink26
image = pink26.io.read("dot.pgm")
simple = pink26.topology.simple_points(image, 8)
print(int(simple.sum()), "simple points")
Where a thin object ends and where it branches (a "Y" of three branches; 8-connectivity). In 3D, point_classes_3d tells volumes, surfaces, curves and their junctions apart (Malandain, Bertrand and Ayache 1993); see the docstrings for the two meanings of "junction":
import numpy as np
import pink26
y = np.zeros((11, 11), dtype=np.uint8)
y[5, 5] = 1
for k in (1, 2, 3):
y[5 + k, 5] = y[5 - k, 5 - k] = y[5 - k, 5 + k] = 1
print(int(pink26.topology.end_points(y, 8).sum()))
print(int(pink26.topology.junction_points(y, 8).sum()))
print(int(pink26.topology.curve_points(y, 8).sum()))
Distance map and ultimate skeleton of a disk (the skeleton is one point, its center):
import numpy as np
import pink26
rows, cols = np.mgrid[:21, :21]
disk = (rows - 10) ** 2 + (cols - 10) ** 2 <= 64
dist = pink26.distance.distance_map(disk)
print(int(dist[10, 10]), "at the center")
skeleton = pink26.skeleton.ultimate_skeleton(disk, 8)
print(np.argwhere(skeleton))
Parallel thinning with critical kernels: the symmetric version keeps a small core, the asymmetric one reaches a point (in 3D, pink26 corrects defects of Pink, see the docstring of parallel_skeleton):
bar = np.zeros((6, 10), dtype=bool)
bar[2:4, 2:8] = True
sym = pink26.skeleton.parallel_skeleton(bar, 8)
asym = pink26.skeleton.parallel_skeleton(bar, 8, symmetric=False)
print(int(sym.sum()), int(asym.sum()))
Grayscale homotopic kernel: the sections of the image keep their topology. A bump of height 40 with a peak of 90 shrinks to one point, at the level of the peak (in grayscale the connectivity is the one of the minima, see the docstrings):
bump = np.zeros((7, 7), dtype=np.uint8)
bump[1:6, 1:6] = 40
bump[3, 3] = 90
kernel = pink26.grayscale.homotopic_kernel(bump, 4)
print(np.argwhere(kernel > 0), kernel.max())
Topological watershed: the minima spread over their basins and only the crest lines stay, at the level of the passes. With one marker in each basin, watershed_lines gives the lines as a boolean array:
relief = np.tile(np.array([0, 50, 100, 200, 100, 50, 0], dtype=np.uint8), (3, 1))
print(pink26.watershed.topological_watershed(relief, 4)[0])
markers = np.zeros(relief.shape, dtype=bool)
markers[1, 0] = markers[1, 6] = True
lines = pink26.watershed.watershed_lines(relief, markers, 4)
print(np.argwhere(lines[0]).ravel())
Development
make test # C unit tests
make asan # same, with AddressSanitizer and UBSan
make check-release # strict warnings at -O3 -DNDEBUG (Release flags)
make oracle # compare against the frozen Pink reference
make fuzz FUZZ_CC=clang FUZZ_SECONDS=60 # libFuzzer (needs LLVM clang, see below)
make format-check # clang-format check (make format to apply)
make docs # C API reference (Doxygen) in build/docs/html/index.html
uv venv && source .venv/bin/activate
uv pip install ".[test]"
pytest tests/python
On macOS, Apple's clang has no libFuzzer: use Homebrew's LLVM with make fuzz FUZZ_CC=/opt/homebrew/opt/llvm/bin/clang.
CI (.github/workflows/ci.yml) runs these on Linux and macOS, plus the Python tests on Windows. The Makefile always treats warnings as errors (-Werror). The Python build (CMake) does so only with P26_WERROR=ON, which CI and the wheel builds set; pass -C cmake.define.P26_WERROR=ON to pip install to get the same locally.
Compiled C library
pink26 is also a C library: static libpink26.a (pink26.lib with MSVC) and the public headers include/pink26/*.h. The Wheels workflow builds one archive per platform (Linux x86_64 and aarch64 for glibc >= 2.28, macOS arm64 + x86_64 from 11.0, Windows x86_64 with MSVC), each with include/, lib/ and share/doc/pink26/ (licence). To build it yourself:
cmake -S . -B build-c -DP26_PYTHON=OFF -DP26_C_LIBRARY=ON -DCMAKE_BUILD_TYPE=Release
cmake --build build-c --config Release
cmake --install build-c --config Release --prefix <prefix>
cc my_program.c -I<prefix>/include <prefix>/lib/libpink26.a -lm
tests/package/consumer.c is a minimal program linked this way.
Publishing (maintainer only)
Publishing to TestPyPI is manual:
- On TestPyPI, add a trusted publisher: project pink26, workflow wheels.yml, environment testpypi. Create the testpypi environment in the GitHub repository settings, with required reviewers (recommended: the publish job then waits for an approval before uploading).
- In GitHub, run the Wheels workflow manually (Actions, Run workflow) with publish checked.
A run with publish unchecked only builds the wheels, the sdist and the C library archives as artifacts (GitHub keeps artifacts 90 days).
References
The digital topology in pink26 comes from the work of Gilles Bertrand, Michel Couprie and their co-authors. Full list with BibTeX keys: docs/papers/REFERENCES.md and docs/papers/references.bib. PDFs are kept locally in docs/papers/ and are not distributed with this repository.
Simple points and topological numbers (2D/3D)
- G. Bertrand, "Simple points, topological numbers and geodesic neighborhoods in cubic grids", Pattern Recognition Letters 15(10), pp. 1003-1011, 1994. doi:10.1016/0167-8655(94)90032-9
- G. Bertrand, G. Malandain, "A new characterization of three-dimensional simple points", Pattern Recognition Letters 15(2), pp. 169-175, 1994. doi:10.1016/0167-8655(94)90046-9
- M. Couprie, G. Bertrand, "New Characterizations of Simple Points in 2D, 3D, and 4D Discrete Spaces", IEEE Transactions on Pattern Analysis and Machine Intelligence 31(4), pp. 637-648, 2009. doi:10.1109/tpami.2008.117
- G. Bertrand, M. Couprie, N. Passat, "A note on 3-D simple points and simple-equivalence", Information Processing Letters 109(13), pp. 700-704, 2009. doi:10.1016/j.ipl.2009.03.002
Critical kernels and parallel thinning
- G. Bertrand, "On critical kernels", Comptes Rendus. Mathématique 345(7), pp. 363-367, 2007. doi:10.1016/j.crma.2007.09.001
- G. Bertrand, M. Couprie, "Two-Dimensional Parallel Thinning Algorithms Based on Critical Kernels", Journal of Mathematical Imaging and Vision 31(1), pp. 35-56, 2008. doi:10.1007/s10851-007-0063-0
- G. Bertrand, M. Couprie, "On Parallel Thinning Algorithms: Minimal Non-simple Sets, P-simple Points and Critical Kernels", Journal of Mathematical Imaging and Vision 35(1), pp. 23-35, 2009. doi:10.1007/s10851-009-0152-3
- G. Bertrand, M. Couprie, "A New 3D Parallel Thinning Scheme Based on Critical Kernels", DGCI 2006, LNCS 4245, Springer, pp. 580-591, 2006. doi:10.1007/11907350_49
- G. Bertrand, M. Couprie, "New 2D Parallel Thinning Algorithms Based on Critical Kernels", Combinatorial Image Analysis (IWCIA 2006), LNCS 4040, Springer, pp. 45-59, 2006 (reference taken from Couprie, Saude and Bertrand, SIBGRAPI 2006; DOI not verified)
- G. Bertrand, M. Couprie, "Powerful Parallel and Symmetric 3D Thinning Schemes Based on Critical Kernels", Journal of Mathematical Imaging and Vision 48(1), pp. 134-148, 2014. doi:10.1007/s10851-012-0402-7
- G. Bertrand, M. Couprie, "Isthmus based parallel and symmetric 3D thinning algorithms", Graphical Models 80, pp. 1-15, 2015. doi:10.1016/j.gmod.2015.05.001
- M. Couprie, G. Bertrand, "Asymmetric parallel 3D thinning scheme and algorithms based on isthmuses", Pattern Recognition Letters 76, pp. 22-31, 2016. doi:10.1016/j.patrec.2015.03.014
Classic 2D parallel thinnings (no topology proof; for comparison):
- Y. Y. Zhang, P. S. P. Wang, "A modified parallel thinning algorithm", Proc. 9th International Conference on Pattern Recognition (ICPR), pp. 1023-1025, 1988 (not verified: no local copy)
- Z. Guo, R. W. Hall, "Fast fully parallel thinning algorithms", CVGIP: Image Understanding 55(3), pp. 317-328, 1992 (not verified: no local copy)
Sequential and distance-guided homotopic skeletons
- M. Couprie, A. Saude, G. Bertrand, "Euclidean homotopic skeleton based on critical kernels", Proc. SIBGRAPI 2006, IEEE, pp. 307-314, 2006. doi:10.1109/sibgrapi.2006.16
- M. Couprie, D. Coeurjolly, R. Zrour, "Discrete bisector function and Euclidean skeleton in 2D and 3D", Image and Vision Computing 25(10), pp. 1543-1556, 2007. doi:10.1016/j.imavis.2006.06.020
- M. Couprie, G. Bertrand, "Discrete Topological Transformations for Image Processing", in Digital Geometry Algorithms (Brimkov & Barneva, eds), LNCVB 2, Springer, pp. 73-107, 2012. doi:10.1007/978-94-007-4174-4_3
- G. Bertrand, M. Couprie, "Transformations topologiques discrètes", in Géométrie discrète et images numériques (D. Coeurjolly, A. Montanvert, J.-M. Chassery, eds), Hermès, pp. 187-209, 2007.
Classification of points (surfaces, curves, junctions)
- G. Malandain, G. Bertrand, N. Ayache, "Topological segmentation of discrete surfaces", International Journal of Computer Vision 10(2), pp. 183-197, 1993. doi:10.1007/BF01420736
Grayscale topology, homotopic kernels, lambda-kernels
- G. Bertrand, J.C. Everat, M. Couprie, "Image segmentation through operators based upon topology", Journal of Electronic Imaging 6(4), pp. 395-405, 1997. doi:10.1117/12.276856
- M. Couprie, F.N. Bezerra, G. Bertrand, "Topological operators for grayscale image processing", Journal of Electronic Imaging 10(4), pp. 1003-1015, 2001. doi:10.1117/1.1408316
Topological watershed
- M. Couprie, G. Bertrand, "Topological gray-scale watershed transformation", Vision Geometry VI, Proc. SPIE vol. 3168, pp. 136-146, 1997. doi:10.1117/12.292778
- G. Bertrand, "On Topological Watersheds", Journal of Mathematical Imaging and Vision 22(2-3), pp. 217-230, 2005. doi:10.1007/s10851-005-4891-5
- M. Couprie, L. Najman, G. Bertrand, "Quasi-Linear Algorithms for the Topological Watershed", Journal of Mathematical Imaging and Vision 22(2-3), pp. 231-249, 2005. doi:10.1007/s10851-005-4892-4