Apply Morphology
The erode-and-dilate toolbox that fixes your masks
- image
- IMAGE
If you've ever had a mask that's too fat, too thin, full of pinholes, or missing chunks, this is the node. Morphology ("Apply Morphology") applies the classic image-processing morphology operations - erode, dilate, open, close, gradient, top_hat, bottom_hat - and it's been in ComfyUI core since March 2024, built on the kornia library. It's the built-in answer to the mask-cleanup chores that custom packs like WAS Node Suite get all the credit for.
The one-sentence mental model
Morphology grows and shrinks the bright regions of an image with a square kernel. Bright = "yes" (mask white), dark = "no" (mask black). Everything else is a combination of those two moves:
- erode - shrinks white regions, eats away thin edges and specks.
- dilate - grows white regions, fills small gaps.
- open - erode then dilate. Kills small white specks without shrinking the main shape. Your "clean the mask of random flecks" button.
- close - dilate then erode. Fills small holes without growing the main shape. Your "patch the mask gaps" button.
- gradient - dilate minus erode. Leaves an outline of the bright regions - a cheap way to get an edge/contour image.
- top_hat - original minus opened. Extracts the small bright details. bottom_hat - closed minus original, for small dark details.
Where it belongs in the workflow
The name says image/filters, but in practice most people feed it masks. An inpaint mask from the Mask Editor or an auto-masker is never clean on the first pass - run close with a small kernel to fill holes, open to kill stray specks, then erode by a couple of pixels and blur if you want the inpaint region to blend instead of showing a hard edge. It operates per channel, so a 1-channel mask-as-image works fine.
The inputs
- image - the IMAGE tensor (a mask converted to image, or any image).
- operation - the seven operations above, as a dropdown.
- kernel_size (3–999, default 3) - the side of the square kernel. 3 is gentle; 9+ starts visibly chewing. The kernel is a solid block of ones, so this is a square structuring element.
The output is an IMAGE of the same size. It's fast for small kernels and gets slower fast with big ones - kernel 999 on a large image is not a good time.
Getting it
Ships with ComfyUI core, no install - search "Morphology". It lives alongside the YUV color nodes in the same source file, if you've ever wondered where it hangs out.
Common gotchas
- It takes IMAGE, not MASK. A MASK tensor won't plug directly in. Convert mask → image first (the mask nodes do this), and remember the output comes back as an image too.
- Order of operations matters.
openandcloseare not symmetric with erode/dilate. If your mask both has holes and extra specks, runcloseand thenopen- doing it backwards moves the shape. - Even values behave oddly. The schema min is 3 and the kernel is a solid square; stick to odd sizes so the dilation is symmetric around the pixel.
- You can nuke a mask. Dilate on a thin mask can swallow the entire image into white. Preview the output before you wire it into the inpaint.
If your need is one-off - "grow this mask by 2px" - a dedicated mask-size node is simpler, but for real cleanup, nothing beats having erode/dilate/open/close in one drop-down.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| operation | COMBO | 7 options: erode, dilate, open, close, gradient, bottom_hat, +1 | |
| kernel_size | INT | 33–999 | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| IMAGE | IMAGE | — |