![](https://image.jiqizhixin.com/uploads/editor/a1b93157-958e-4019-9e53-5b4f3aeea955/640.jpeg)
numpy>=1.16.0
pillow>=5.2.0
numba>=0.47.0
scipy>=1.1.0
cupy-cuda90>=6.5.0 or similar
pyopencl>=2019.1.2
pytest>=5.3.4
from pymatting import cutout
cutout(
# input image path"data/lemur.png",
# input trimap path"data/lemur_trimap.png",
# output cutout path"lemur_cutout.png")
作者还提供了高级代码,直接进行抠图,生成新图像:
image = load_image("../data/lemur/lemur.png", "RGB", scale, "box")
trimap = load_image("../data/lemur/lemur_trimap.png", "GRAY", scale, "nearest")
# estimate alpha from image and trimap
alpha = estimate_alpha_cf(image, trimap)
# make gray background
new_background = np.zeros(image.shape)
new_background[:, :] = [0.5, 0.5, 0.5]
# estimate foreground from image and alpha
foreground, background = estimate_foreground_ml(image, alpha, return_background=True)
# blend foreground with background and alpha, less color bleeding
new_image = blend(foreground, new_background, alpha)
python3 tests/download_images.py
pip3 install -r requirements_tests.txt
pytest
![](https://image.jiqizhixin.com/uploads/editor/414d69c5-a208-4ea1-b61e-375cf3c760ea/640.png)