Nodes/ComfyUI-XJNodes/Dilate Mask (SEGS Expand)
ComfyUI Node

Dilate Mask (SEGS Expand)

Grow a mask without clipping it at the crop edge

By alexjx·Created 10 months ago·Updated 4 months ago· 0
Dilate Mask (SEGS Expand)
  • segs
  • expanded_segs
dilation10
expand_buffer0

Dilating a mask sounds trivial until you remember how SEGS stores things. Each segment's mask lives inside a tightly-cropped region, and a naive dilation just runs off the edge of that crop and gets cut off - your grown mask silently stops at the old crop boundary, which defeats the whole point of growing it. XJSegsDilateMaskExpand is the node that understands the problem: it dilates (or erodes, with a negative value) the mask and expands the crop region to fit the full result.

The mechanism is worth knowing because it's the difference between "this works" and "this looks broken." The node pads the mask, applies a circular structuring element via scipy's binary dilation/erosion (so the growth keeps a natural round profile rather than turning into squares), then recomputes the crop region and bounding box to match the enlarged mask. If the growth hits the edge of the actual image, it clamps gracefully and trims the mask rather than erroring. There's also an expand_buffer input for adding slack beyond the pure dilation amount - the tooltip spells it out: 0 = expand exactly enough for dilation.

The interface:

  • segs - SEGS in.
  • dilation - INT, -512 to 512. Positive grows, negative erodes.
  • expand_buffer - INT, 0–256. Extra pixels of crop expansion beyond the dilation.
  • Output: expanded_segs.

Where you'd reach for it: the classic detailer problem - your mask is too tight around the subject and the detail pass keeps getting cut off at the edges. Growing the mask a bit before feeding it to a Detailer (SEGS) or a refine node gives the sampler room to breathe. The "expand the crop" part matters more than it sounds: standard dilation nodes that operate inside a fixed crop produce a bigger mask that's still visually clipped, which is why this one exists at all. It composes naturally with the pack's Refine nodes when you want to grow and clean up.

Install is the standard pack routine:

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

Restart ComfyUI, find it under XJNodes/segs, or use ComfyUI Manager and search "ComfyUI-XJNodes". No model downloads; requirements.txt is empty, though it does lean on scipy's ndimage module - which a stock ComfyUI install already ships.

Two notes from experience: this is a personal-use pack, so don't expect a tutorial ecosystem around it. And test with a big dilation value first - it's the fastest way to see whether the crop-expansion behavior matches what your workflow assumed.

CategoryXJNodes/segs

Inputs (3)

NameTypeDefaultDescription
segsSEGS
dilationINT10-512–512
expand_bufferINT00–256Extra pixels to expand beyond dilation amount. 0 = expand exactly enough for dilation.

Outputs (1)

NameTypeDescription
expanded_segsSEGS