Nodes/ComfyUI-YCNodes/Mask Direction Expand (YC)
ComfyUI Node

Mask Direction Expand (YC)

Grow a mask in one direction without wrecking the other

By yichengup·Created 2 years ago·Updated 4 months ago· 48
Mask Direction Expand (YC)
  • mask
  • mask
top0
bottom0
left0
right0

Most "grow the mask" nodes do it symmetrically, and that's a problem in exactly the case you hit every day: you have a mask that should extend downward to cover more of a subject, but the moment you expand it, the sides puff out too, and your inpaint region now includes the wall you wanted left alone. YCMaskDirectionExpand fixes that by letting you grow - or shrink - each edge independently. Up, down, left, right are four separate sliders, each accepting positive (expand) or negative (shrink) values.

How it works

This is the clever one in the pack, and the mechanism is worth understanding because it's why it behaves better than the naive version. A normal MaskGrow dilates with a square kernel, which by definition rounds corners and expands in all directions at once. This node instead uses separable 1D kernels - a tall vertical kernel for top/bottom changes and a wide horizontal one for left/right. The vertical pass touches only vertical structure, the horizontal pass only horizontal structure, so expanding "up" by 50px extends the mask upward while leaving its left and right edges untouched. Shrinking works the same way, with erosion, which means a thin vertical line can be shortened without being wiped out - the failure mode that plagues symmetric erode. Each direction is clamped and anchored correctly so the geometry matches your intent rather than approximating it.

Under the hood it's OpenCV dilate/erode on a 0–255 mask, converted back to 0–1 float. If all four values are zero, it returns the input unchanged (handy - bypassing isn't needed).

The inputs that matter

  • mask - a MASK tensor. Feeds from anything that produces one.
  • top / bottom / left / right - integers, range −10000 to +10000. Positive grows that edge outward; negative cuts inward from that edge. Values are in pixels, and the tooltips are honestly the clearest description you'll get: "positive: grow upward | negative: cut down from the top," and so on.

Output is a single MASK, same dimensions, ready to feed a VAE encode for inpainting, a composite, or a blur.

Where it earns its keep

This is the node for the "expand the grounding box" problem the KB keeps hitting: when you get a mask from SAM or a VLM-detected bounding box, it's tighter than you want - it wraps the cup, not the saucer - and the standard advice is to pad it by ~50px of context before inpainting. With a symmetric grow that padding bleeds in all directions; with this you can add context exactly on the side where the image needs to continue (say, outward from an object at the edge of frame) and leave the already-correct side alone. It's also great for outpainting margins and for cleaning masks that are a few pixels too fat on one side.

Installing it

Part of ComfyUI-YCNodes. ComfyUI Manager → search ComfyUI-YCNodes → install, or:

cd ComfyUI/custom_nodes
git clone https://github.com/yichengup/ComfyUI-YCNodes

Restart, then look under YCNode/Mask. Requires opencv-python from the pack's requirements.txt - already present in virtually every ComfyUI install.

Gotchas

The tooltips say "shrink from the top" and the sign convention is consistent, but people still reach for negative values expecting the opposite - when you set top to −20, the top edge moves down, cutting the mask shorter. That's the documented behavior, just remember positive always means "grow toward that edge." Also, values are pixels, not percent, so on a 512² mask 100px is a lot of growth while on a 2048² canvas it's barely a nudge. The scale mismatch is on you.

CategoryYCNode/Mask

Inputs (5)

NameTypeDefaultDescription
maskMASK
topINT0-10000–10000正数:向上生长 | 负数:从顶部向下裁切
bottomINT0-10000–10000正数:向下生长 | 负数:从底部向上裁切
leftINT0-10000–10000正数:向左生长 | 负数:从左侧向右裁切
rightINT0-10000–10000正数:向右生长 | 负数:从右侧向左裁切

Outputs (1)

NameTypeDescription
maskMASK