Nodes/opencv-comfyui/OpenCV erode_0
ComfyUI Node

OpenCV erode_0

The mask-cleanup move that removes tiny white specks

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV erode_0
  • src
  • kernel
  • dst
  • nparray
anchor
iterations
borderType
borderValue

Erosion is the morphological operation that eats away at the bright pixels in a binary image. A white pixel survives only if the structuring element fits entirely inside it - so thin white streaks vanish, small specks disappear, and the borders of bright regions shrink. It's the inverse of dilation, and in a ComfyUI context it's mostly a mask-cleanup tool: knock the stray 1-pixel noise off a mask, separate blobs that are barely touching, or strip the thin spurs off the edge of a subject before you feed the mask downstream.

This node wraps cv2.erode from opencv-comfyui (geroldmeisinger/opencv-comfyui), the pack that auto-generates a node for every standalone OpenCV function. Same pack, same quirks as the rest: it runs on nparray, not Comfy's IMAGE, and every knob is a raw number or a Python-literal string. No models involved - pure morphology on pixels.

Inputs that matter

  • src (NPARRAY) - your image or mask.
  • kernel (NPARRAY) - the structuring element, as an nparray. This is the part beginners trip on. You can't just type a size; you feed in an actual matrix, usually built with the pack's getStructuringElement_0 node (grab a shape like MORPH_RECT / 0, MORPH_ELLIPSE / 1, MORPH_CROSS / 2 and a (3, 3)-style size). A bigger kernel = more aggressive erosion.
  • anchor (STRING) - the kernel's reference point, typed as a Python literal. Default (-1, -1) means "center" and is what you want 99% of the time.
  • iterations (INT) - how many times to erode in a row. Crank this instead of making a giant kernel; it's cheaper and easier to tune.
  • borderType (INT) - border extrapolation enum. 0 (BORDER_CONSTANT) is the safe default.
  • borderValue (STRING) - the constant border value, also a literal string like 0.

The optional dst is an OpenCV out-parameter - leave it unconnected, the README says so explicitly. Output is nparray, the eroded image/mask.

Wiring it up

Convert your IMAGE with Image2Nparray, and feed that into src. For the kernel, chain getStructuringElement_0 into kernel. On the way out, Nparrays2Image turns the result back into a Comfy IMAGE. If your mask is BGR instead of single-channel, you may want cvtColor code 6 first - and remember this pack thinks in BGR, because Image2Nparray flips RGB→BGR under the hood.

Install

ComfyUI Manager, search "opencv-comfyui", or:

cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui

Restart, and make sure opencv-contrib-python is installed (pip install opencv-contrib-python) - it's in the pack's requirements along with numpy and torch. No model downloads.

Common issues

The string inputs are parsed with Python's literal_eval, so wrong syntax gives you invalid syntax (<unknown>, line 0) - write (-1, -1), not (-1,-1,) or words. If nothing visibly changes, your kernel is probably tiny relative to the image; bump iterations. And if you wanted to grow bright regions instead of shrinking them, that's the sibling dilate_0 node, not this one. One more thing: the author's warning for this whole pack is "Expect dragons!" - auto-generated, raw enums, no hand-holding. It's powerful, but it's OpenCV with the safety labels in German.

Categoryimage/OpenCV

Inputs (7)

NameTypeDefaultDescription
srcNPARRAY
kernelNPARRAY
anchorSTRING
iterationsINT
borderTypeINT
borderValueSTRING
dstoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY