MorphologicOperation
Erode, dilate, open, close — the mask-cleaning kit you keep reaching for
- src
- IMAGE
A messy mask is the enemy of a clean inpaint. Segmentation comes back with speckles, color ranges pick up stray pixels, and one little white dot outside your subject turns into a hallucinated region in the output. MorphologicOperation is the box of tools for fixing that, and it's worth its weight even if you only ever touch two of its seven operations.
It's a direct wrapper around OpenCV's morphologyEx - which is exactly why it exists: the author wrote it because they couldn't find another pack that exposed the top-hat and black-hat operations, so they built the generic version with all seven. The menu puts it under Bmad/CV/Morphology, and every operation here works on the same premise: a small "kernel" (a grid shape) sweeps across the image and, depending on the operation, grows, shrinks, or reshapes the white regions of your mask.
The operations, quickly
- MORPH_ERODE - shrinks white regions, eats thin specks.
kernel_sizecontrols how much gets eaten. - MORPH_DILATE - grows white regions, closes tiny gaps.
- MORPH_OPEN - erode then dilate: removes noise speckles outside the main shape without shrinking it.
- MORPH_CLOSE - dilate then erode: fills holes inside the shape.
- MORPH_GRADIENT - the difference between dilate and erode: outlines the mask's edges.
- MORPH_TOPHAT - the original minus its opening: isolates small bright detail on a light background.
- MORPH_BLACKHAT - the closing minus the original: the dark-detail sibling of tophat.
For mask cleanup, OPEN and CLOSE are the two you'll live in: OPEN strips the specks, CLOSE patches the holes. Run both and most masks go from garbage to usable without a single manual brush stroke.
The inputs
src(IMAGE) - your mask (or any grayscale-ish image).operation- the seven above.kernel_type- RECT, ELLIPSE, or CROSS. For organic subjects, ELLIPSE usually gives smoother results than RECT.kernel_size_x/kernel_size_y- the kernel dimensions (min 2, stepped in 2s). Bigger = more aggressive.iterations- how many passes. Two passes with a small kernel beat one pass with a big one, roughly speaking.
Output is a single IMAGE, ready to wire into your inpaint or ControlNet.
Installing it
cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_bmad_nodes
then restart, or find comfyui_bmad_nodes in ComfyUI Manager. It needs the pack's OpenCV requirement, which Manager pulls automatically. No model files.
The honest caveat
Because it's a generic OpenCV wrapper, it operates on brightness, not on your mask's meaning - over-dilate and your subject's mask swallows the background. Start with kernel_size at 4 and iterations at 1, and inspect the output before you wire it into anything expensive. And note the kernel sizes step by 2 and start at 2 - you can't have a 1×1 kernel here, which is fine because a 1×1 kernel does nothing anyway.
Inputs (6)
| Name | Type | Default | Description |
|---|---|---|---|
| src | IMAGE | — | |
| operation | COMBO | MORPH_ERODE | 7 options: MORPH_ERODE, MORPH_DILATE, MORPH_OPEN, MORPH_CLOSE, MORPH_GRADIENT, MORPH_TOPHAT, +1 |
| kernel_type | COMBO | MORPH_RECT | 3 options: MORPH_RECT, MORPH_ELLIPSE, MORPH_CROSS |
| kernel_size_x | INT | 4 | — |
| kernel_size_y | INT | 4 | — |
| iterations | INT | 1 | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| IMAGE | IMAGE | — |