Nodes/ComfyUI-BadmanNodes/Dilate Erode Mask (Badman)
ComfyUI Node

Dilate Erode Mask (Badman)

Grow or shrink a mask with one signed slider

By MariusKM·Created 2 years ago·Updated 3 months ago· 4
Dilate Erode Mask (Badman)
  • masks
  • MASK
radius0
shape

Mask morphology usually comes as two separate operations you have to remember to chain - a dilate node to grow a mask, an erode node to shrink one. Badman collapses both into a single node with a signed radius: positive numbers grow the mask, negative numbers shrink it. One node, one slider, both directions.

Why you'd want to grow or shrink a mask at all

If you've done any inpainting or compositing, you've hit this problem: the mask you get out of a segmentation model, a manual paint, or a matte-cleanup step is rarely exactly the boundary you want to act on. Grow it (dilate) a few pixels and you buy yourself margin - useful when you want an inpaint to blend a little past the exact subject edge so there's no hard seam, or when you're feeding a mask into a ControlNet-style guide and want a bit of slack around the region. Shrink it (erode) and you pull the boundary in from the edges - handy for trimming off a soft, noisy fringe a segmentation model left behind, or for making sure a mask doesn't touch pixels that belong to the background. This node does both jobs from the same input, which means you can wire it once and just flip the sign of radius depending on which direction you need that pass.

Inputs and outputs

Three required fields:

  • masks - the MASK you're operating on.
  • radius (integer, -1023 to 1023, default 0, step 1) - the size and direction of the operation. Positive dilates (grows), negative erodes (shrinks), 0 is a no-op pass-through. The huge range means you can do anything from a one-pixel nudge to a dramatic reshape, though anything past a couple hundred pixels on a large mask is going to visibly balloon or eat the shape rather than just cleaning an edge.
  • shape - an enum, box or circle, the structuring element used for the operation. box grows/shrinks along axis-aligned edges and will noticeably square off corners on anything past a small radius; circle keeps corners rounded and generally looks more natural on organic subjects. If your dilated mask is coming out with weirdly sharp, blocky corners, that's box doing exactly what it's supposed to - switch to circle.

One output: the modified MASK, same shape and semantics as the input, ready to feed into inpainting, compositing, or another mask-processing step.

Installing it

ComfyUI Manager: search ComfyUI-BadmanNodes, install, restart. Or manually:

cd ComfyUI/custom_nodes
git clone https://github.com/MariusKM/ComfyUI-BadmanNodes

Restart ComfyUI. No dependencies or model downloads noted in the README for this node - it's pure mask-tensor math, so install is as clean as it gets.

Where people get tripped up

The main trap is forgetting the range is signed and reaching for a separate erode node out of habit when you actually just needed a negative radius here. The second is scale mismatch: a radius value that looks reasonable on a 512px test image can be wildly too aggressive (or too subtle to notice) once you're working at 2K or 4K, since this is a pixel-radius value, not a percentage of the mask's size - recheck it whenever your resolution changes. And if you're chaining this before something like a Gaussian mask blur, do the dilate/erode pass first and blur after; blurring a hard-edged mask and then reshaping it tends to reintroduce the harsh edge you just spent a blur pass softening.

CategoryBadman

Inputs (3)

NameTypeDefaultDescription
masksMASK
radiusINT0-1023–1023
shapeCOMBO2 options: box, circle

Outputs (1)

NameTypeDescription
MASKMASK