Nodes/ComfyUI-WaterMark-Detector/Morphological Operations
ComfyUI Node

Morphological Operations

Dilate, erode, open, close — the mask cleanup crew

By hotpizzatactics·Created 2 years ago·Updated 2 years ago· 0
Morphological Operations
  • image
  • IMAGE
operation
kernel_size5

Morphological Operations is the janitor of this pack. It does four classic binary-image jobs - dilate, erode, open, close - with a square kernel of your chosen size. On its own it's not glamorous and produces nothing you'd want as a final image. Where it earns its keep is right after a thresholding or edge-detection step, when your mask looks like it's been through a pepper grinder.

The context: this pack's thresholding and edge nodes give you binary output, and binary output is noisy. A watermark text stroke extracted by adaptive thresholding comes out speckled with white flecks from JPEG artifacts; the text itself has holes where the threshold wobbled. Morphology is the standard fix for both. It's also exactly what the pack does internally - AdvancedWaveletWatermarkEnhancement's "denoise" is open-then-close morphology, looped.

The four operations

  • dilate - grows bright regions, fills small holes, thickens strokes. When your mask text is too thin to be useful, dilate it.
  • erode - shrinks bright regions, strips isolated specks. The counterpart to dilate; good for thinning fat blobs.
  • open - erode then dilate. Kills the small speckle noise without shrinking the main object much. This is the one you'll use most.
  • close - dilate then erode. Fills small holes and gaps in your mask while keeping overall size roughly the same.

kernel_size (1–21, default 5, odd only) sets the side of the square structuring element. Bigger kernel = more aggressive effect.

How it works

Straight OpenCV under the hood: cv2.dilate, cv2.erode, or cv2.morphologyEx with the matching MORPH_OPEN/MORPH_CLOSE, all with a square all-ones kernel. The image passes through as uint8, then back to a float IMAGE tensor. No parameters beyond the two, nothing hidden.

Using it well

The classic sequence is: adaptive threshold → open with a small kernel (3 or 5) to kill specks → optionally close to fill the text holes → then hand the mask to inpainting or use it as a control map. Canny-style edge maps from this pack also feed ControlNet preprocessors, and cleaning them with an open is the difference between a decent condition and a noisy one.

Two cautions. First, this operates on the whole image as one binary blob - it doesn't know what's watermark and what isn't, so on a mask full of unrelated white regions, morphology will happily clean those too. And second, don't confuse "cleaner mask" with "sharper mask": over-dilating makes the watermark region bloated, which then bleeds outside the true mark when you inpaint. Small kernels, one operation at a time, preview between each.

Installing

No models, CPU-only, nothing exotic:

cd ComfyUI/custom_nodes
git clone https://github.com/hotpizzatactics/ComfyUI-WaterMark-Detector

or search ComfyUI-WaterMark-Detector in ComfyUI Manager. The pack's install.py pip-installs torch, numpy, opencv-python, scipy, and PyWavelets; opencv is the only substantial dependency and you almost certainly have it. Restart ComfyUI after installing.

Categoryimage/watermark

Inputs (3)

NameTypeDefaultDescription
imageIMAGE
operationCOMBO4 options: dilate, erode, open, close
kernel_sizeINT51–21

Outputs (1)

NameTypeDescription
IMAGEIMAGE