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).
git clone https://github.com/pymatting/pymatting
cd pymatting
pip3 install .
Example
# First import will take a minute due to compilation
from pymatting import cutout
cutout(
# input image path
"data/lemur/lemur.png",
# input trimap path
"data/lemur/lemur_trimap.png",
# output cutout path
"lemur_cutout.png")
All implemented methods rely on trimaps which roughly classify the image into foreground, background and unknown regions.
Trimaps are expected to be numpy.ndarrays of type np.float64 having the same shape as the input image with only one color-channel.
Trimap values of 0.0 denote pixels which are 100% background.
Similarly, trimap values of 1.0 denote pixels which are 100% foreground.
All other values indicate unknown pixels which will be estimated by the algorithm.
This project is licensed under the MIT License - see the LICENSE.md file for details
Citing
If you found PyMatting to be useful for your work, please consider citing our paper:
@article{Germer2020,
doi = {10.21105/joss.02481},
url = {https://doi.org/10.21105/joss.02481},
year = {2020},
publisher = {The Open Journal},
volume = {5},
number = {54},
pages = {2481},
author = {Thomas Germer and Tobias Uelwer and Stefan Conrad and Stefan Harmeling},
title = {PyMatting: A Python Library for Alpha Matting},
journal = {Journal of Open Source Software}
}
References
[1]
Anat Levin, Dani Lischinski, and Yair Weiss. A closed-form solution to natural image matting. IEEE transactions on pattern analysis and machine intelligence, 30(2):228–242, 2007.
[2]
Kaiming He, Jian Sun, and Xiaoou Tang. Fast matting using large kernel matting laplacian matrices. In 2010 IEEE Computer Society Conference on Computer Vision and Pattern Recognition, 2165–2172. IEEE, 2010.
[3]
Qifeng Chen, Dingzeyu Li, and Chi-Keung Tang. Knn matting. IEEE transactions on pattern analysis and machine intelligence, 35(9):2175–2188, 2013.
[4]
Yuanjie Zheng and Chandra Kambhamettu. Learning based digital matting. In 2009 IEEE 12th international conference on computer vision, 889–896. IEEE, 2009.
[5]
Leo Grady, Thomas Schiwietz, Shmuel Aharon, and Rüdiger Westermann. Random walks for interactive alpha-matting. In Proceedings of VIIP, volume 2005, 423–429. 2005.
[6]
Eduardo S. L. Gastal and Manuel M. Oliveira. "Shared Sampling for Real-Time Alpha Matting". Computer Graphics Forum. Volume 29 (2010), Number 2, Proceedings of Eurographics 2010, pp. 575-584.
[7]
Germer, T., Uelwer, T., Conrad, S., & Harmeling, S. (2020). Fast Multi-Level Foreground Estimation. arXiv preprint arXiv:2006.14970.