Modify Mask
The jackknife of mask cleanup
- mask
- Mask
- Inverted Mask
UC_ModifyMask is the node you reach for when a mask is almost right. It grows, shrinks, blurs, inverts, and generally tortures a mask into shape - and it's one of the few mask utilities that understands you're probably working with a video, not a single frame. It's the mask-cleanup workhorse of silveroxides/ComfyUI-UtilsCollection.
The core idea is simple: a mask from a detector (or from Core's segmentation) is never pixel-perfect. Edges are jagged, holes are scattered around, the thing is 3px too small to cover the subject. This node fixes all of that in one pass. In the detailer loop that the KB calls the four-step grind - detect, crop, re-render, paste back - this is the part that makes the pasted seam disappear, because a feathered, slightly-expanded mask is what hides the boundary between the refined region and the original.
What actually matters on the node
- expand - the one you'll touch constantly. Pixels to grow the mask; negative values shrink it. +8 to cover a subject's edges, -4 to pull back from a background that's bleeding in.
- blur_radius - Gaussian blur after grow/shrink. This is the difference between a hard, visible seam and a soft blend. Zero disables it.
- tapered_corners - on by default, uses a cross-shaped kernel so corners come out tapered instead of boxy. Leave it on unless you specifically want square edges.
- flip_input - inverts the mask before anything else. Free inversion, right there, no extra node.
- fill_holes - optional input that fills enclosed zero-value holes after grow/shrink. Perfect for masks with speckled interiors.
- lower_clamp / upper_clamp - hard floor and ceiling on output values as percentages, if you need to keep a mask from going fully black or fully white.
The interesting machinery is the batch-aware trio: incremental_expandrate, lerp_alpha, and decay_factor. incremental_expandrate grows (or shrinks) each consecutive mask in a batch a little more than the last - the standard trick when a mask needs to track something expanding over video frames. lerp_alpha blends each frame's mask with the previous processed one, and decay_factor adds a decaying memory of earlier masks before normalizing. Together they let a per-frame mask stay temporally stable instead of flickering frame to frame. If your mask flickers in a video, this is the fix.
Outputs
- Mask - the processed mask.
- Inverted Mask - the same mask flipped, so you don't need a second node.
Installing it
Part of the pack, not standalone:
- ComfyUI Manager: search "ComfyUI-UtilsCollection".
- Or:
cd ComfyUI/custom_nodes
git clone https://github.com/silveroxides/ComfyUI-UtilsCollection
Restart ComfyUI. Requirements are just opencv-python and typing-extensions - no model downloads for this one.
Gotchas
The expansion is iterative, one pixel at a time, so a huge expand on a big mask is slower than you'd expect - nothing to panic about, but don't run 200px expansions in a tight loop over hundreds of frames. Also remember that incremental_expandrate is per-frame: if you only have a single mask, it does nothing, and you might briefly wonder why your slider is dead. And the blur happens after grow/shrink, so a negative expand with blur gives you a feathered erosion - handy for trimming a mask's edge so inpainting doesn't touch the subject's outline. This is one of those nodes you'll forget you have until the moment you need it, and then you'll use it everywhere.
Inputs (11)
| Name | Type | Default | Description |
|---|---|---|---|
| mask | MASK | — | |
| expand | INT | 0-16384–16384 | Pixels used to grow each mask; negative values shrink it. |
| incremental_expandrate | FLOAT | 0.000–100 | Additional grow or shrink amount applied after each mask in the batch. |
| tapered_corners | BOOLEAN | true | Uses a cross-shaped kernel for more tapered corners; disabled uses a square kernel. |
| flip_input | BOOLEAN | false | Inverts the input mask before all other processing. |
| blur_radius | FLOAT | 0.000–100 | Gaussian blur radius applied after grow or shrink; zero disables blur. |
| lerp_alpha | FLOAT | 1.000–1 | Blends each batch mask with the previous processed mask; 1 uses only the current mask. |
| decay_factor | FLOAT | 1.000–1 | Adds a decayed previous batch mask before normalization; 1 preserves its full contribution. |
| lower_clamp | FLOAT | 0.00–100 | Minimum output mask value as a percentage. |
| upper_clamp | FLOAT | 100.00–100 | Maximum output mask value as a percentage. |
| fill_holesopt | BOOLEAN | false | Fills enclosed zero-valued holes after grow or shrink. |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| Mask | MASK | — |
| Inverted Mask | MASK | — |