Image Matting
Turn a rough mask into a real matte with actual soft edges
- images
- trimap
- matte
- fg
- bg
A segmentation model - SAM, BiRefNet's base weights, anything that draws you a mask - gives you a hard yes/no answer per pixel: foreground or background. That's structurally the wrong tool for hair, fur, a veil, motion blur, or smoke, because none of those things are cleanly one or the other. Image Matting solves the other problem: given a rough mask (a trimap - foreground, background, and an "unknown" band in between), it solves for the actual fractional transparency at every pixel in that unknown band, using closed-form matting (built on the pymatting library).
This is the classical, non-ML approach to matting - no model weights, a linear system solved from color statistics in the unknown region. The README's own framing is direct: it's for "cleaning up SAM segments or hand drawn masks," not for generating a mask from scratch. You still need a segmenter first; this node refines what it gives you.
What it needs and what it gives back
images(IMAGE) - the source.trimap(MASK) - your rough mask. Build this with this pack's own Dilate/Erode Mask: dilate your rough selection for the outer bound, erode it for the inner "definitely foreground" core, and the gap between the two is the unknown band the solver works on.preblur(default 8, 0–256) - smooths the input before solving, which reduces sensitivity to per-pixel noise in that unknown region.blackpoint/whitepoint(default 0.01 / 0.99) - clip near-0 and near-1 trimap values to hard background/foreground before solving, so you're not asking the solver to second-guess regions that are already obviously certain.max_iterations(default 1000, 10–10000) - the iterative solver's cap. Higher gets more accurate but slower; it's a genuine speed/quality trade, not a free lunch.estimate_fg(default true, BOOLEAN) - also estimates and returns color-corrected foreground/background colors, defringing edges that picked up background-color spill during matting.
Three outputs: matte (MASK, the real fractional-alpha result), fg (IMAGE, the color-corrected foreground estimate), bg (IMAGE, the background estimate).
How to install it
ComfyUI Manager: search "ComfyUI-Image-Filters", install, restart. Manual:
cd ComfyUI/custom_nodes
git clone https://github.com/spacepxl/ComfyUI-Image-Filters
restart. This node in particular pulls in pymatting as well as OpenCV - the ComfyUI Manager install and the pack's requirements.txt handle both, but if you're installing manually and see an import error mentioning pymatting, that's a missing pip dependency rather than the opencv conflict this pack is more commonly known for. opencv-contrib-python covers the OpenCV side; if cv2 itself errors, that's the usual conflicting-variant issue the author's install.bat/import_error_install.bat handle on Windows.
Common issues & troubleshooting
Result is slow, especially on a large image. Closed-form matting is an iterative linear-system solve - it's genuinely CPU-heavy, not GPU work, and it scales with image size and max_iterations. Downscale the input, matte at the lower resolution, then upscale the matte back if speed matters more than pixel-perfect precision.
Edges look wrong or the matte just looks like your original mask, unchanged. The output is entirely a function of trimap quality - this node cannot invent detail your trimap doesn't expose. If the "unknown" band from Dilate/Erode Mask is too thin, the solver has almost nothing to work with; widen it.
Expecting this to replace a dedicated matting model. It won't generate a mask on its own - BiRefNet-matting's ML weights are the one-shot alternative if you want a model to produce fractional alpha directly from an image with no trimap step. This node's job is specifically refining an existing rough mask, which makes it the right tool when you already have SAM or segmentation output and just need the edges cleaned up.
Inputs (7)
| Name | Type | Default | Description |
|---|---|---|---|
| images | IMAGE | — | |
| trimap | MASK | — | |
| preblur | INT | 80–256 | — |
| blackpoint | FLOAT | 0.010–0.99 | — |
| whitepoint | FLOAT | 0.990.01–1 | — |
| max_iterations | INT | 100010–10000 | — |
| estimate_fg | BOOLEAN | true | — |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| matte | MASK | — |
| fg | IMAGE | — |
| bg | IMAGE | — |