Estimate foreground (AFFCE)
De-contaminate your cutout edges — the fast foreground-estimation fix
- images
- masks
- foreground
- mask
Here's the problem nobody warns you about with background removal. Your BiRefNet or rembg cutout looks great against grey, and then you drop it onto a new background and every edge carries a halo of the old background color - hair especially. That's not a mask problem, it's a color problem. A mask can tell you where the subject is; it can't tell you what color the foreground truly was where the pixels are a mixture of subject and background. That's what this node solves.
Estimate foreground (AFFCE) runs Approximate Fast Foreground Colour Estimation on an image + mask pair and returns a "de-contaminated" foreground whose edges are pure subject color instead of background bleed. It's the same technique that powers Photoroom's background-removal pipeline, via the fast-foreground-estimation repo (the ICIP 2021 paper behind it), and this pack's version is a clean PyTorch port of the reference implementation that shipped inside BiRefNet's image processing - no OpenCV needed.
How it works
The algorithm is a two-stage blur-and-fuse trick, and it's genuinely cheap. First it builds a coarsely blurred version of the image with a big Gaussian-ish blur (radius blur_size, default 90) and fuses it with the original weighted by the mask - near the edges, the mask is mid-value, so the fusion starts to separate foreground color from the background color bleeding through. Then it repeats the fusion with a second, fine blur (blur_size_two, default 6) to sharpen the result. Two box blurs and a few per-pixel ops. That's the whole thing, which is why it's "approximate" - and why it's fast enough to run on a batch.
Inputs and outputs
- images - the cutout you want to clean up.
- masks - the alpha matte from your background-removal model.
- blur_size - coarse blur diameter; bigger = smoother but mushier edges.
- blur_size_two - fine blur diameter, the detail pass.
- batched - process all images at once. Faster, more VRAM; uncheck for one-at-a-time on a tight budget.
Outputs are foreground (the cleaned image) and mask (your input mask, passed through). The foreground output is ready to composite onto a new background - that's the intended use, and it fixes the halo problem without re-running a model.
Installing it
Part of Image Misc, installed like the rest of the pack:
cd ComfyUI/custom_nodes
git clone https://github.com/set-soft/ComfyUI-ImageMisc
cd ComfyUI-ImageMisc && pip install -r requirements.txt
or via ComfyUI Manager ("Image Misc"). Then restart and look under image/foreground. It uses only PyTorch and ComfyUI's own stack - no model files, no extra downloads. That's the pack's whole philosophy, and it's refreshing in a world where half the background-removal nodes want you to fetch gigabytes of weights.
Common issues
This is a cheap approximate fix, so don't expect miracles on transparent materials - glass and veils genuinely need a matting model, and this won't reconstruct detail that isn't in the image. If edges come out soft, drop blur_size_two; if they stay contaminated, raise blur_size. And know that this is the "good enough, instant" option in this pack: there's a sibling node, FMLFE, that solves the same problem with a real multi-level solver and also hands you a cleaned background plate, at the cost of being far slower. AFFCE is the one you reach for when speed and simplicity win. It's also the engine behind the pack's Apply Mask using AFFCE node, which packages this exact estimation plus the actual masking into one step.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| images | IMAGE | — | |
| masks | MASK | — | |
| blur_size | INT | 901–255 | — |
| blur_size_two | INT | 61–255 | — |
| batched | BOOLEAN | true | Process the images at once. Faster, needs more memory |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| foreground | IMAGE | — |
| mask | MASK | — |