pymatting/pymatting

A Python library for alpha matting

What it solves

PyMatting addresses the alpha matting problem, which involves estimating the alpha channel (transparency) of a foreground object in an image. This allows the object to be precisely separated from its background so it can be composed onto a different one.

How it works

The library uses a "trimap"—a hand-drawn image that marks pixels as definitely foreground, definitely background, or unknown. It then applies various mathematical and algorithmic methods to estimate the transparency of the unknown regions. It leverages NumPy, SciPy, and Numba for performance and offers GPU acceleration (via CuPy and PyOpenCL) specifically for foreground estimation.

Who it’s for

It is designed for developers and researchers working on image processing, computer vision, and background removal tools.

Highlights

  • Implements multiple alpha matting algorithms, including Closed Form, Large Kernel, KNN, Learning Based Digital, Random Walk, and Shared Sampling matting.
  • Provides foreground estimation tools, including a fast multi-level implementation.
  • Includes specialized preconditioners (Incomplete thresholded Cholesky decomposition and V-Cycle Geometric Multigrid) to speed up convergence.
  • Supports GPU acceleration for foreground estimation tasks.

Related

  • Project
  • Project
  • Project
  • Project