Nodes/Masquerade Nodes/Mask Morphology
ComfyUI Node Runs on cloud

Mask Morphology

Shrink, grow, and clean up messy masks

By BadCafeCode·Created 3 years ago·Updated 2 years ago· 480
Mask Morphology
  • image
  • IMAGE
distance5
op

Masks from real sources are rarely clean. A hand-painted mask has jagged edges, a ClipSeg or SAM mask has a stray white pixel where the model got confused, a threshold operation leaves little black pinholes inside an otherwise solid area. Mask Morphology is the node that cleans that up - it's the same dilate/erode/open/close toolkit every computer-vision library ships, borrowed straight into ComfyUI for exactly this job.

The four operations do two things at their core, and the other two are combinations of them. Dilate grows the white area outward by however many pixels you specify - useful when a mask is a hair too tight and you want some breathing room before you cut or inpaint. Erode does the opposite, shrinking the white area inward, which is handy when a mask is bleeding slightly past the thing you actually wanted. Open is erode-then-dilate: it eats small white specks (they're too thin to survive the erosion) while leaving the main blob roughly the size it started, so use it to knock out noise before it does damage. Close is the mirror - dilate-then-erode - and it fills in small black holes and gaps inside a mask without changing the outer boundary much. If your mask looks like Swiss cheese, close is the fix.

The inputs that matter

  • image - the mask to operate on (wired as a MASK or IMAGE; this pack treats them interchangeably).
  • distance (INT, 0–128, default 5) - how many pixels the operation reaches. This is the only real knob. Small values (2–5) clean up noise without reshaping anything meaningfully; larger values start visibly changing the silhouette.
  • op - one of dilate, erode, open, close.

The single output is the resulting mask, ready to feed into whatever consumed the original - Combine Masks, Cut By Mask, Mask To Region, or a sampler's mask input.

There's a real order-of-operations tip worth internalizing: run open before feeding a mask into Separate Mask Components. Separate Mask Components treats every disconnected white region as its own blob, so a stray fleck the size of a few pixels becomes a spurious extra "component" in your batch. A quick open pass at distance 2–3 kills those before they cause downstream weirdness - extra cut-outs, extra paste targets, batch counts that don't match what you expected.

Installing it

Mask Morphology ships as part of Masquerade Nodes, a small, dependency-free node pack from BadCafeCode. Two ways in:

  • ComfyUI Manager - search "Masquerade Nodes" (or the repo name, masquerade-nodes-comfyui), install, restart.
  • Manual - cd ComfyUI/custom_nodes && git clone https://github.com/BadCafeCode/masquerade-nodes-comfyui, then restart ComfyUI.

Worth knowing going in: the pack's own README leads with a note from the author recommending Impact Pack instead, unless you specifically want a dependency-free library - this pack predates ComfyUI Manager and was built to need nothing extra. Mask Morphology in particular is about as low-risk as a custom node gets: no model download, no pip install, no external service. It's plain array math (grow/shrink a binary-ish region), so it either works immediately after a restart or something's wrong with the install itself, not the node.

Common issues

Because it has zero external dependencies, there's not much to troubleshoot specific to this node. If it doesn't show up in the node search after installing, that's almost always an incomplete restart - reloading the browser tab isn't enough, the ComfyUI server process itself needs to restart to pick up a new custom node. If the result looks unchanged at low distance values, that's expected on a mostly-clean mask; morphology is a subtle cleanup tool, not a mask redesign, and it's a wasted step if your source mask was already tidy. Push distance well past the size of the noise you're trying to remove - a 1–2px speck needs a distance of at least 2–3 to reliably vanish under open.

CategoryMasquerade Nodes

Inputs (3)

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

Outputs (1)

NameTypeDescription
IMAGEIMAGE