Mask (Alpha) Clean
Patch the pinholes and speckle a segmenter leaves in a mask
- mask
- MASK
A mask from SAM, a segmentation model, or a hand-drawn selection often isn't clean where it should be solid - a stray hole in the middle of what should be a fully-covered region, or "near-white" pixels sitting at 0.9-something instead of a clean 1.0. That's a different problem from a mask's boundary being in the wrong place (which is Dilate/Erode Mask's job). Mask (Alpha) Clean fixes the interior, per the README: "Clean up holes and near-solid areas in a matte."
Think of it as a defect-removal pass, not a boundary-adjustment one. You'd run this on a mask whose overall shape is already right but whose fill has speckle, pinholes, or noisy near-solid values that should just be flat 1.0 or 0.0.
The four settings
mask(MASK) - required.radius(default 8, 1–64) - the cleanup filter's kernel size.fill_holes(default 1, 0–16) - how aggressively small interior holes get filled;0disables hole-filling entirely, higher values push harder.white_threshold(default 0.9, 0.001–1) - values above this get snapped fully to white (1.0), cleaning up "should be solid but sits at 0.87" noise.extra_clip(default 0.98, 0.001–1) - a further clipping pass tightening the result after the threshold step.
One output: MASK.
Where it fits and where it doesn't
Run this on binary-ish masks - SAM output, a rough segmentation mask, anything that's meant to be solidly one thing or another with some noise in the fill. Don't run it after Image Matting. Matting's whole purpose is producing genuine fractional alpha at edges - hair strands, semi-transparent material - and Mask Clean's white_threshold/extra_clip will happily snap those soft, deliberately-partial values to hard 0 or 1, throwing away exactly the detail the matting solve computed. Use it on masks that are supposed to be nearly binary, not on ones that are supposed to be soft.
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
then restart. Dependency is opencv-contrib-python; a cv2 import error at startup usually points to a conflicting opencv variant from another node pack rather than this one being broken. On Windows the author's install.bat/import_error_install.bat fix that by reinstalling a matched opencv set; manually elsewhere, uninstall the stray opencv packages and install opencv-contrib-python alone. No models, negligible compute.
Common issues & troubleshooting
Ran it on a matte from Image Matting and lost the soft edges. That's the aggressive white_threshold/extra_clip defaults doing exactly what they're designed to do - snapping near-solid values to hard 0/1. This node isn't meant for masks with intentional soft transitions; if you need that, skip it or run it before matting, not after.
Holes still visible after running it. fill_holes at the default of 1 is a light pass - push it up if you've got larger or more numerous holes to close. It won't fix holes that are so large they don't read as "small defects" relative to radius - those need a manual fix or a different mask entirely.
Mask edges look harder/more jagged than before. white_threshold and extra_clip near 1.0 push soft transitions toward hard binary values, which can make an edge that used to fade gently look like a sharper cutoff. If you wanted a soft edge preserved, back those thresholds down or skip this node for that particular mask.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| mask | MASK | — | |
| radius | INT | 81–64 | — |
| fill_holes | INT | 10–16 | — |
| white_threshold | FLOAT | 0.9000.001–1 | — |
| extra_clip | FLOAT | 0.9800.001–1 | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| MASK | MASK | — |