Nodes/ComfyUI-JH-PixelPro/Mask: Morphology
ComfyUI Node

Mask: Morphology

Mask Surgery 101

By jetthuangai·Created 5 months ago·Updated 4 months ago· 5
Mask: Morphology
  • mask
  • mask
operationdilate
radius3
iterations1

Morphology is the oldest trick in the mask-cleaning book, and JHPixelProMaskMorphology wraps it with elliptical kernels and a clean dropdown: dilate, erode, open, close, gradient, tophat, blackhat. If your SAM mask has speckle noise or hairline holes, this is where you fix it before the mask ever reaches a composite or an inpaint. It's the kind of node that looks academic until the first time a mask arrives full of stray specks.

The two you'll actually use 90% of the time:

  • dilate - grows the mask. Fixes skinny edges, fills small gaps.
  • erode - shrinks it. Strips fringe and halo.
  • close (dilate then erode) - fills holes without growing the overall shape.
  • open (erode then dilate) - removes speckles without shrinking the main body.

Inputs: mask, operation (seven choices), radius (1..64, using an elliptical kernel), iterations (1..10, repeats the operation). Output: a single MASK.

The README is explicit that the kernels are elliptical, not square - that's the detail that separates this from cheap implementations; you don't get the boxy corners a naive morphology leaves behind. It's a small thing, but it shows up in the contour.

The workflow that makes it click: after segmenting a subject, run open at radius 2 to kill stray specks, then dilate at radius 1–2 so the mask extends slightly past the true edge (compensating for mattes that shrink), then finish with a soft edge pass from the pack's Sub-Pixel Mask Refiner or Edge Smoother. Sequence matters - clean the mask first, then soften.

Install is the shared pack install:

cd ComfyUI/custom_nodes
git clone https://github.com/jetthuangai/ComfyUI-JH-PixelPro.git
cd ComfyUI-JH-PixelPro
pip install -r requirements.txt

Restart and it's under ComfyUI-JH-PixelPro/mask, or use ComfyUI Manager → search "ComfyUI-JH-PixelPro".

Troubleshooting: know that iterations and radius compound - radius 4 × 2 iterations erodes a lot more than radius 4 once. If a mask is shrinking more than you expected, lower the radius before touching iterations. And morphology is fundamentally a binary operation: soft masks get thresholded along the way, so do your feathering after morphology, not before. It sits in the pack's mask group alongside the trimap and alpha-matte tools - part of the post-segmentation cleanup pipeline.

CategoryComfyUI-JH-PixelPro/mask

Inputs (4)

NameTypeDefaultDescription
maskMASK
operationCOMBOdilate7 options: dilate, erode, open, close, gradient, tophat, +1
radiusINT31–64
iterationsINT11–10

Outputs (1)

NameTypeDescription
maskMASK