Nodes/ComfyUI-Yuan/Mask Preprocess Morphology
ComfyUI Node

Mask Preprocess Morphology

Your Inpaint Mask Is Ragged. This Node Grows, Shrinks, and Cleans It.

By the-hexer·Created 2 years ago·Updated 10 months ago· 15
Mask Preprocess Morphology
  • mask
  • MASK
â—„distance5â–º
◄op▾►

What it actually is

Masks from real tools are messy. BiRefNet, SAM, a hand-drawn brush in the ComfyUI image editor - they all hand you something with ragged edges, stray specks, or a selection that stops a pixel or three short of where the subject ends. That matters, because in inpainting the mask edge is the seam. Leave a sliver of the thing you're removing outside the mask and the model "fixes" half an object and leaves a ghost.

Mask Preprocess Morphology is a one-trick node from the small ComfyUI-MoneyMaker pack (repo ComfyUI-Yuan, by Cyber_BlackCat) that fixes exactly that: it dilates, erodes, opens, or closes a mask by a distance you choose, and - here's the part that matters - hands you back a genuine MASK tensor, not an image. That last bit is the whole reason it exists. The pack's README says v1.0.1 was built around a bug in masquerade-nodes-comfyui, which spat masks out as image format so they couldn't be fed straight into generation. This one can.

How it works

The math is refreshingly honest - no models, no weights, no downloads. Dilation is a max-pooling pass: each pixel takes the maximum mask value in a square window of radius distance, so the covered area creeps outward by up to that many pixels. Erosion is the complement trick - invert the mask, dilate, invert back. "Open" runs erode then dilate (kills specks and smooths the edge), "Close" runs dilate then erode (fills small holes). It all operates on the continuous 0–1 mask values, so soft, anti-aliased masks stay soft instead of getting thresholded into hard-edged blobs. Set distance to 0 and the kernel is 1×1 - a no-op.

The inputs that matter

Honestly the whole node is three inputs:

  • mask (MASK) - any mask. It should already be a MASK, not an image.
  • op - dilate / erode / open / close. For inpainting you'll mostly live on dilate and close.
  • distance (default 5, 0–128) - how far the operation reaches, in pixels.

The output is a single MASK, which wires straight into Set Latent Noisy Mask, ConditioningSetMask, Mask to Image, or the mask socket of an inpaint VAE encode. That's the point: no conversion step between you and the sampler.

Install

No model files anywhere - it's pure torch ops, so it runs on whatever ComfyUI already has. In ComfyUI Manager, search "ComfyUI-MoneyMaker"; or:

cd ComfyUI/custom_nodes
git clone https://github.com/Cyber-Blacat/ComfyUI-Yuan

then restart ComfyUI. The pack's requirements.txt lists opencv-python and scikit-image, but this node never touches them; if you hit a missing-module error on some other node in the pack, pip install -r requirements.txt in your venv fixes it.

Common gotchas

  • "The mask won't connect downstream." You're almost certainly feeding an IMAGE in or getting one out. This node expects a real MASK - if your source is an image, run Convert Image to Mask (or the pack's Mask To Gray) first.
  • distance too big. Dilation has no idea what's foreground. Crank it to 30 and the mask happily grows into neighboring areas it was never meant to cover. 3–8 is the sane range for tightening an inpaint region.
  • Don't mistake this for feathering. Morphology moves where the edge is; it doesn't soften it. If you're getting hard seams, you still want a Mask Blur / feather pass on top - 4–12px is the usual range.

The honest take: if you already run Impact Pack, its Mask Dilate/Erode Region does the same job. This node's niche is being standalone, dependency-free, and small - grab it when you want one less mega-pack in your graph.

CategoryMoneyMaker😺

Inputs (3)

NameTypeDefaultDescription
maskMASK—
distanceINT50–128—
opCOMBO4 options: dilate, erode, open, close

Outputs (1)

NameTypeDescription
MASKMASK—