ComfyUI Node

Modify Mask

The jackknife of mask cleanup

By silveroxides·Created 2 months ago·Updated about 7 hours ago· 24
Modify Mask
  • mask
  • Mask
  • Inverted Mask
expand0
incremental_expandrate0.00
tapered_cornerstrue
flip_inputfalse
blur_radius0.00
lerp_alpha1.00
decay_factor1.00
lower_clamp0.0
upper_clamp100.0
fill_holesfalse

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.

Categoryutils/mask

Inputs (11)

NameTypeDefaultDescription
maskMASK
expandINT0-16384–16384Pixels used to grow each mask; negative values shrink it.
incremental_expandrateFLOAT0.000–100Additional grow or shrink amount applied after each mask in the batch.
tapered_cornersBOOLEANtrueUses a cross-shaped kernel for more tapered corners; disabled uses a square kernel.
flip_inputBOOLEANfalseInverts the input mask before all other processing.
blur_radiusFLOAT0.000–100Gaussian blur radius applied after grow or shrink; zero disables blur.
lerp_alphaFLOAT1.000–1Blends each batch mask with the previous processed mask; 1 uses only the current mask.
decay_factorFLOAT1.000–1Adds a decayed previous batch mask before normalization; 1 preserves its full contribution.
lower_clampFLOAT0.00–100Minimum output mask value as a percentage.
upper_clampFLOAT100.00–100Maximum output mask value as a percentage.
fill_holesoptBOOLEANfalseFills enclosed zero-valued holes after grow or shrink.

Outputs (2)

NameTypeDescription
MaskMASK
Inverted MaskMASK