Nodes/ComfyUI-Image-Filters/Dilate/Erode Mask
ComfyUI Node

Dilate/Erode Mask

Grow or shrink a mask's edge, one node, one sign flip

By spacepxl·Created 3 years ago·Updated 8 months ago· 293
Dilate/Erode Mask
  • masks
  • MASK
radius0
shape

Masks from segmentation models, SAM, or a hand-drawn selection are rarely exactly the boundary you want. Too tight and you get a visible seam at the edge of an inpaint; too loose and you're regenerating background you meant to protect. Dilate/Erode Mask is the classic computer-vision fix - grow the mask (dilate) to cover more, or shrink it (erode) to pull back - and this node does both with a single radius parameter: positive grows, negative shrinks.

That's the whole mechanism. No separate dilate and erode nodes to juggle - flip the sign and you've switched direction.

The inputs

  • masks (MASK) - required.
  • radius (default 0, range -1023 to 1023) - positive dilates (grows the white region outward), negative erodes (shrinks it inward). 0 is a no-op.
  • shape - box or circle. circle gives you a rounder, more natural-looking edge; box is a cheaper, blockier kernel. For most masking work circle looks better and the cost difference is negligible unless you're at very large radii.

One output: MASK.

Where this actually gets used

Two workflows dominate. First, cleaning up an inpaint mask - a common pattern is to dilate a rough selection by a handful of pixels so the generated fix overlaps the original edge instead of butting right up against it, then soften with this pack's Blur Mask (Fast) afterward for a feathered rather than hard transition. Second, building a trimap for matting: dilate your rough foreground mask to get an outer boundary and erode it to get an inner "definitely foreground" core, and the band between the two becomes the "unknown" region a matting algorithm (this pack's Image Matting node, for instance) needs to solve for actual fractional edges - hair, fur, anything that isn't a hard binary cutout.

Note what this node doesn't do: it changes the mask's boundary, not its softness. A dilated mask still has a hard edge, just a bigger one. If you want a soft, feathered transition, pair it with a blur pass afterward.

How to install it

ComfyUI Manager: search "ComfyUI-Image-Filters", install, restart. Manual install:

cd ComfyUI/custom_nodes
git clone https://github.com/spacepxl/ComfyUI-Image-Filters

then restart. Dependency is OpenCV (opencv-contrib-python covers it); a cv2 import error on startup usually means another pack installed a conflicting opencv variant, and the fix is the author's install.bat/import_error_install.bat on Windows, or manually uninstalling stray opencv packages and installing opencv-contrib-python alone on Linux. No models, negligible compute cost.

Common issues & troubleshooting

Edges still look hard/jagged after dilating. That's expected - dilation moves the boundary, it doesn't blur it. Chain Blur Mask (Fast) right after this node if you need a soft feathered edge for compositing or inpainting.

Building a trimap and the "unknown" band feels too thin or too thick. Adjust the dilate and erode radii independently - run this node twice (once with a positive radius for the outer bound, once with a negative radius on the original mask for the inner bound), then combine. A too-thin unknown band gives a matting solver too little information to work with; too-thick and you're asking it to guess over a huge area.

Large radius on a large mask is slow. The cost scales with radius, so if you're dilating by hundreds of pixels on a big image, expect it to take noticeably longer than a small cleanup dilate - downscale first if you just need an approximate mask shape.

CategoryImage-Filters/mask

Inputs (3)

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

Outputs (1)

NameTypeDescription
MASKMASK