Nodes/ComfyUI-XJNodes/Mask Dilate (Circular)
ComfyUI Node

Mask Dilate (Circular)

Blow up a mask without turning every corner into a square

By alexjx·Created 10 months ago·Updated 4 months ago· 0
Mask Dilate (Circular)
  • mask
  • dilated_mask
dilation10

Mask Dilate (Circular) is the "expand this mask by N pixels" node, with one meaningful improvement over the naive version: it uses a circular structuring element, so grown masks keep rounded, organic shapes instead of getting squared-off corners. It's the difference between inflating a balloon and inflating a cardboard box. When your mask came from a segmentation model and is hugging a subject too tightly, a few pixels of this is the standard fix.

The real-world case is the one the whole mask ecosystem lives for: SAM or other segmenters return masks that under-segment - the mask cuts into the subject's edges, lopping off hair, fur, or soft boundaries. The inpainting and detailing lore in this space is consistent that a little mask growth before the sampling pass makes the transition invisible instead of a hard cut line. This node is that little growth, done properly.

How it works

It takes the mask, thresholds it to binary (pixels above 0.5 are "in"), builds a circular disk of the requested radius - the code literally constructs a disk via x² + y² ≤ r² - and runs a morphological dilation (or erosion) with that disk. Positive dilation grows the mask, negative shrinks it. A plain dilation with a square kernel grows corners into stair-steps; the circular kernel keeps everything rounded. The result is converted back to a float mask tensor, and batches are processed per-image so a whole batch comes out consistent.

Inputs and outputs

Only two inputs:

  • mask - the MASK to grow or shrink
  • dilation - INT, −512 to 512, default 10. Positive dilates, negative erodes

The single output is dilated_mask, a MASK. That's the whole node - wire it into anything that takes a mask: an inpainting mask input, a detailer, a mask-to-latent step.

Installing it

Standard XJNodes install. ComfyUI Manager → search "ComfyUI-XJNodes" → install → restart. Or:

cd ComfyUI/custom_nodes
git clone https://github.com/alexjx/ComfyUI-XJNodes
# restart ComfyUI

It needs scipy for the morphological operations - that ships with most ComfyUI installs, but if you get an import error, pip install scipy fixes it. Category: XJNodes/segs.

What to know before you rely on it

A few honest notes. This node works on raw masks; it has no idea about "keep the boundaries smooth in the image" - it just inflates the shape, so a mask growing over a hard edge will happily cover it. That's what the pack's fancier siblings, Mask Refine (Morph) and Mask Refine (K-Means), are for: they grow masks aware of image content, stopping at edges or color boundaries. If your mask needs to respect the picture, reach for those instead. Also note the output is binary (thresholded at 0.5), so any soft anti-aliased edge in your input mask gets flattened - if you need feathering, apply a blur after this node, not before. And if you're feeding masks into the Impact Pack's SEGS-based detailers, remember these mask nodes output plain MASK tensors, not SEGS - convert if that's your pipeline.

CategoryXJNodes/segs

Inputs (2)

NameTypeDefaultDescription
maskMASK
dilationINT10-512–512

Outputs (1)

NameTypeDescription
dilated_maskMASK