We introduce the PyMatting package for Python which implements various methods to solve the alpha matting problem.
Given an input image and a hand-drawn trimap (top row), alpha matting estimates the alpha channel of a foreground object which can then be composed onto a different background (bottom row).
- Alpha matting implementations for:
- Foreground estimation implementations for:
- Closed Form Foreground Estimation 
- Multilevel Foreground Estimation (CPU, CUDA and OpenCL)
- Fast multithreaded KNN search
- Preconditioners to accelerate the convergence rate of conjugate gradient descent:
- The incomplete thresholded Cholesky decomposition (Incomplete is part of the name. The implementation is quite complete.)
- The V-Cycle Geometric Multigrid preconditioner
- Readable code leveraging NumPy, SciPy and Numba
Additional requirements for GPU support
- cupy-cuda90>=6.5.0 or similar
Requirements to run the tests
Installation with PyPI
pip3 install pymatting
Installation from Source
git clone https://github.com/pymatting/pymatting cd pymatting pip3 install .
from pymatting import cutout cutout( # input image path "data/lemur.png", # input trimap path "data/lemur_trimap.png", # output cutout path "lemur_cutout.png")
Run the tests from the main directory:
python3 tests/download_images.py pip3 install -r requirements_tests.txt pytest
Currently 89% of the code is covered by tests.
- Thomas Germer
- Tobias Uelwer
- Stefan Conrad
- Stefan Harmeling
Subscribe to Python Awesome
Get the latest posts delivered right to your inbox