In this project, I wanted to gain experience with a multi-language codebase, mixing a Python front end with a C++ computational core. I chose Active Contour segmentation, also known as Snakes. This is a good fit because loading images and initializing a contour is easy in Python, while the iterative optimization is efficient in C++.
The package is hosted on the TestPyPI environment. To install it, run the following command:
pip install -i https://test.pypi.org/simple/ SnakesPyPlease refer to requirements.txt for Python dependencies.
Wheels are not built for every platform yet, but some versions are currently available for Windows, macOS, and Linux (see files).
To run the program, install the package in your environment and run:
python -m SnakesPy.test_api "path\to\your\image.png"This opens a pop-up window where you can draw an initial shape to evolve and set the model parameters and solver.

This repository uses GitHub Actions for testing, wheel building, release uploads, and TestPyPI publishing.
Builds the C++ project and runs the test suite.
- Builds wheels via
cibuildwheelon:ubuntu-latest(x86_64)windows-latest(AMD64)macos-latest(universal2)
- Uploads built wheels as GitHub Actions artifacts
- Runs only after wheel build succeeds
- Runs only for tag builds or manual dispatch
- Downloads all wheel artifacts and uploads them to the matching GitHub Release via
gh release upload
- Triggered by completion of the
Build Wheelsworkflow (workflow_run) - Downloads wheel artifacts from the triggering workflow and publishes them to TestPyPI (
https://test.pypi.org/legacy/)
cpp_code/: C++ core implementation, pybind11 bindings, and C++ tests.python_code/SnakesPy/: Python package (UI, API wrappers)..github/workflows/: CI/CD workflows for tests, wheels, and TestPyPI publish.pyproject.toml: Packaging and build configuration (scikit-build + CMake).
Currently, this project is based on a C++ core together with a Python UI, which makes it easy to load images and draw initial shapes. The connection between Python and C++ is implemented using Pybind11. This tool compiles the C++ code into a Python extension module, which can then be imported by the Python package.
The active contour model formulates segmentation as an energy minimization problem. We start with an initial contour that evolves to a (locally) minimum-energy state.

Let s parameterize the contour such that s -> v(s) = (x(s), y(s)), with periodic boundary conditions v(1) = v(0).
Here,
In practice this is calculated using a Sobel or Canny edge detection filter.
The internal energy
Combining all this we can write the problem as such,
where
The greedy snake is the most naive implementation for solving this energy minimization problem. For each point on the contour, it evaluates how the energy changes if the point moves to one of its neighboring grid points. This process is repeated for every point. It works, but it is slower and more prone to getting stuck in a local minimum.
A more advanced way to solve this energy minimization problem is to use the Euler-Lagrange equation from classical mechanics. Using the Euler-Lagrange equation we can restate our energy minimization problem as such.
So for our problem this results in the following components.
Combining all this we can see that the snake that minimizes our energy functional must follow:
This equation is solved numerically using the Euler method.
[1] Pierre, F., et al. "Segmentation with Active Contours," in Image Processing On Line, vol. 11, pp. 120–141, 2021.