Install
Python 3.11 or later, with NumPy, SciPy and Pillow. Download the package of the latest release from Releases, unzip it, and install the wheel in it.
python -m pip install ellipse_foreground-X.Y.Z-py3-none-any.whl
Use
This program reads an image, takes an ellipse around the foreground and writes the alpha matte. Calling extract again on the same Analysis with another ellipse reuses the tree built for the image.
import numpy as np
from PIL import Image
from ellipse_foreground import Analysis, Ellipse, Mix
source = np.asarray(Image.open("input.png").convert("RGB"))
analysis = Analysis(source.astype(np.float32) / 255.0, source)
ellipse = Ellipse(center_x=640, center_y=330, radius_x=190, radius_y=300)
alpha = analysis.extract(ellipse, Mix.ADAPTIVE_COLOR)
Image.fromarray(np.rint(alpha * 255).astype(np.uint8)).save("alpha.png")
What it does
- Shrinks the image to a working image, 256 pixels wide by default, and builds a single-linkage hierarchical tree on its 4-neighbour pixel grid.
- Chooses the cut of the tree that maximises the sum of an ellipse prior, per-pixel cues and a boundary term. Dynamic programming finds the optimum exactly.
- Uses three cues, colour, texture and the nearest 3×3 patch, and weights the cues by how well each separates the inside of the ellipse from the outside.
- Turns the low-resolution mask into an alpha matte at the image resolution: joint bilateral upsampling, a colour-histogram correction and a guided filter.
Speed and limits
- It is written in NumPy and SciPy for the CPU. For an image of 1280×640 pixels on a Core i7-1360P, building the
Analysistakes about 0.3 s and oneextracttakes about 1 s. - The evaluation uses characters composited onto backgrounds. It does not cover photographs of people, and it does not compare with learned segmentation models. The paper says so as well.
Links
- The paper, or the PDF.
- README: requirements, usage, how to reproduce the evaluation, limits.
- Releases
- Zenodo record, for citing it.