Nodes/Image Misc/Estimate foreground (FMLFE)
ComfyUI Node

Estimate foreground (FMLFE)

The serious foreground estimator that also hands you a clean background

By set-soft·Created about a year ago·Updated 8 months ago· 2
Estimate foreground (FMLFE)
  • images
  • masks
  • foreground
  • background
  • mask
implementationauto
regularization0.0000
n_small_iterations10
n_big_iterations2
small_size32
gradient_weight1.0

The pack's other foreground estimator, and the one that takes itself seriously. Estimate foreground (FMLFE) implements Fast Multi-Level Foreground Estimation - the algorithm from Germer, Uelwer, Conrad and Harmeling's 2020 paper (arXiv:2006.14970), the same work that powers the PyMatting library's estimate_foreground_ml. Given an image and an alpha matte, it solves for the true, un-blended foreground and background colors, treating semi-transparent edge pixels as unknowns to be recovered rather than mixtures to be faked.

That last part is the difference from the sibling AFFCE node. AFFCE is a fast blur-and-fuse approximation - it cleans the halo cheaply and gives you foreground only. FMLFE is a proper solver: it estimates the foreground plate, a fully inpainted background plate (the subject removed, the background color reconstructed behind it), and hands you both. That makes it the right tool when you want to relight, replace, or realistically composite - when the background behind the subject matters, not just the subject's edges.

How it works

It builds an image pyramid and solves a least-squares color-estimation problem at each level, starting coarse and refining up to full resolution. The regularization term is your smoothness knob (higher = smoother, less noise, less detail; the default 1e-5 preserves fine detail), gradient_weight controls how hard the alpha matte's edges pull the color boundaries, and the two iteration counts (n_small_iterations, n_big_iterations) trade quality against speed per pyramid level.

The real practical detail is implementation. There are four backends - cupy, opencl, numba, torch - and auto picks the fastest available. Here's the catch: this pack installs none of the fast ones. Out of the box you get the pure PyTorch fallback, which the author's own docs call slow and approximated. It works, it just takes a while. If you want the good stuff, pip install cupy (needs a full CUDA setup), pyopencl, or numba, and auto will find them.

Inputs and outputs

  • images - the source image(s).
  • masks - the alpha matte. White = known foreground, black = known background, gray = the semi-transparent region the solver reconstructs.
  • implementation - auto/cupy/opencl/numba/torch.
  • regularization, gradient_weight, n_small_iterations, n_big_iterations, small_size - the solver knobs above.

Outputs: foreground, background, and mask (your input matte, echoed back).

Installing it

Same pack, same install:

cd ComfyUI/custom_nodes
git clone https://github.com/set-soft/ComfyUI-ImageMisc
cd ComfyUI-ImageMisc && pip install -r requirements.txt

or ComfyUI Manager → "Image Misc". Restart, find it under image/foreground. No model downloads - the cost is compute, not weights.

The honest take

FMLFE is genuinely excellent for what it does, and it's near-invisible in the wider ecosystem - pymatting and fast-foreground-estimation barely get mentioned anywhere, so this pack is one of the few places you'll meet the algorithm at all. But don't reach for it first. For a quick cutout, AFFCE is a tenth of the work and instant. FMLFE earns its keep when the background plate matters, or when your mask's gray regions deserve a real solve. And if you're on torch backend, budget the time: the algorithm is iterative, and the pyramid means big images take a while. The errors it throws when every backend fails are loud and clear, so at least you'll know what happened.

Categoryimage/foreground

Inputs (8)

NameTypeDefaultDescription
imagesIMAGEThe source image(s) from which to estimate the foreground and background.
masksMASKThe alpha matte that guides the estimation. White areas are treated as known foreground, black as known background, and gray areas are the semi-transparent regions the algorithm will solve for.
implementationCOMBOauto5 options: auto, cupy, opencl, numba, torch
regularizationoptFLOAT0.00000–0.1The regularization strength (epsilon). This acts as a smoothness prior. Higher values result in smoother, more blended foreground and background colors, but may lose very fine details. Lower values preserve more detail but can be noisier.
n_small_iterationsoptINT101–100The number of solver iterations to perform on the lower-resolution levels of the image pyramid. More iterations can improve quality at the cost of speed.
n_big_iterationsoptINT21–100The number of solver iterations to perform on the higher-resolution (larger) levels of the image pyramid. Fewer iterations are typically needed at high resolution as the details are propagated up from the smaller levels.
small_sizeoptINT328–256The pixel dimension threshold. Image pyramid levels smaller than this size will use the higher 'n_small_iterations' count, while larger levels will use 'n_big_iterations'.
gradient_weightoptFLOAT1.00–10Controls how strongly the edges in the alpha matte influence color blending. A higher value makes the algorithm respect the mask's edges more, leading to sharper color boundaries. A lower value allows more color bleeding, an effect similar to increasing regularization.

Outputs (3)

NameTypeDescription
foregroundIMAGE
backgroundIMAGE
maskMASK