Nodes/comfyui_imgutils/Mask Morphology
ComfyUI Node

Mask Morphology

Dilate, erode, open, close — the classic mask fixers

By LK-168·Created about a year ago·Updated about a year ago· 1
Mask Morphology
  • mask
  • mask
operation
kernel_size3
iterations1
kernel_shapeellipse

The node that de-junks your masks

Every mask that comes out of a detector or SAM has problems: specks of noise, holes in the middle, edges that wobble. Mask Morphology is the traditional computer-vision answer, all four of them behind one dropdown. If you've ever fought a mask that "looks right" but produces garbage when you use it, this is the cleanup you were missing.

How it works

It's OpenCV morphology, no AI involved. Four operations, each with a kernel:

  • dilate - grows the mask outward. Fills small gaps and fattens thin regions.
  • erode - shrinks it. Removes thin noise and pinches off stray specks.
  • opening - erode then dilate. Kills small white specks inside the background while keeping the main shape's size. The "de-noise" operation.
  • closing - dilate then erode. Fills small holes inside the subject while keeping its size. The "de-hole" operation.

The controls are kernel_size (default 3, 1–51, odd), iterations (how many passes - more is stronger), and kernel_shape (ellipse default, rectangle, cross). Ellipse gives the roundest, least blocky edges; rectangle is more aggressive.

The inputs that matter

Honestly just two: operation and kernel_size (+iterations when a single pass isn't enough). The mental model: SAM mask has holes in the hair? Closing. Detector mask has speckles all over? Opening. Want to make sure the inpaint region fully covers the subject? Dilate. The pattern I reach for on a fresh SAM mask is opening at a small kernel to nuke the flecks, then a gentle dilate if I need breathing room.

Install

Part of LK-168/comfyui_imgutils. ComfyUI Manager → search "comfyui_imgutils", or:

cd ComfyUI/custom_nodes
git clone https://github.com/LK-168/comfyui_imgutils

Restart ComfyUI. Pure cv2 math on a mask - no models, no downloads, no $HF_HOME drama.

Common issues

  • Mask got blocky / square-ish. You're using rectangle or a big kernel; switch to ellipse.
  • Nothing visibly changed. kernel_size 3 with one iteration is genuinely subtle on a big mask. Crank iterations up or jump to a larger kernel before assuming it's broken.
  • Edges are now hard. Morphology gives hard binary edges by design. If that matters downstream, follow it with Mask Attributes' smooth_radius or the feather option on Mask Edge Operations.

One drop-down, four fixes - this is the mask-cleanup workhorse of the pack, and the censor example workflow in the repo stacks two of them between SAM and the censor node for exactly this reason.

Categoryimgutils/mask

Inputs (5)

NameTypeDefaultDescription
maskMASK
operationCOMBO4 options: dilate, erode, opening, closing
kernel_sizeINT31–51
iterationsINT11–10
kernel_shapeoptCOMBOellipse3 options: ellipse, rectangle, cross

Outputs (1)

NameTypeDescription
maskMASK