Refine Mask (Expand & Blur)
Refine Mask — expand or shrink a mask, then feather it, in one node
- mask
- MASK
Raw masks from detection or segmentation models are rarely usable as-is. They're too tight, they've got stray specks, the edges are knife-sharp. RefineMask is the cleanup node: it expands or contracts the mask by a number of pixels, then optionally blurs the edges, all in one pass. Two knobs, one mask in, a better mask out.
The workflow that made you want this is usually an inpaint or a composite. You get a subject mask from SAM2, and the edges bleed or the mask clips the hair, so you need to grow it a little to catch the full subject, then soften the edge so the composite blends. That's the classic expand-and-feather, and it's exactly what this node automates.
How it works
The mechanism is two operations chained. The expand parameter drives a morphological operation: positive values dilate the mask (grow it by N pixels), negative values erode it (shrink by N pixels), using scipy.ndimage's binary dilation and erosion. Then, if blur is greater than 0, it applies a Gaussian blur to the result for the feathering pass.
One detail worth knowing: the dilation/erosion happens on a binary view of the mask. So if you feed in a mask that's already feathered (has soft gray edges), the expand step re-thresholds it internally. The classic pattern is therefore: expand first, feather last. Do your clean-up grow, then apply the blur as the final step, and the gray edge comes back exactly where you want it.
Inputs and outputs
mask(MASK) - the input mask.expand(INT, default 0, range -100 to 100) - grow (positive) or shrink (negative) the mask.blur(INT, default 0, range 0 to 100) - Gaussian blur radius for edge softening. 0 means no blur.
Output is a single MASK, same size as the input, ready to feed into a VAE encode, a composite, or mask math. The negative expand range is the reason this node can replace a shrink-only node too - one node does both directions.
Installing it
Part of the AnotherUtils pack:
cd ComfyUI/custom_nodes
git clone https://github.com/marcoc2/ComfyUI-AnotherUtils.git
Restart ComfyUI, or install via ComfyUI Manager (search "AnotherUtils"). It does rely on scipy for the morphology and blur - most ComfyUI environments have it, but if the node throws an import error on a fresh install, pip install scipy in your ComfyUI environment is the fix.
Gotchas
The binary-then-blur sequencing is the thing that'll bite you. If you set blur first, then increase expand later and re-run, the erosion/dilation re-thresholds and wipes out your feather. Order of operations matters more than the values.
Also, expand is measured in pixels, and "pixels" at 512px means something very different at 2048px - a mask that looks barely grown at high resolution is a huge grow at low res. Tune per resolution, don't reuse values blindly across different image sizes.
For the common "SAM2 gave me a mask and it's slightly too small" problem, set expand to 5–10, blur to a small value, and check the edge quality. It's the difference between a visible seam and a clean composite, and it's genuinely one of the more useful small utilities in this pack.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| mask | MASK | — | |
| expand | INT | 0-100–100 | — |
| blur | INT | 00–100 | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| MASK | MASK | — |