Nodes/opencv-comfyui/OpenCV dilate_0
ComfyUI Node

OpenCV dilate_0

Grow your masks with OpenCV's dilate

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

dilate_0 is morphological dilation - the single most useful mask-editing primitive there is. If you've ever had an inpainting or detailing mask that's too tight - a hairline mask that leaves seams, a face mask that clips the jawline - this is the node that grows it outward by a few pixels so the region you're re-rendering has clean edges to blend against. It's the expansion half of the open/close operations that clean up masks, and it's one of the few genuinely worth reaching for in this pack.

How it works: dilation is a local maximum filter. For each pixel, the node looks at the neighborhood defined by a structuring element (the kernel) and takes the brightest value it finds. In a binary mask, that means every foreground pixel turns on everything within the kernel's reach - bright regions grow, holes shrink, and the mask puffs out uniformly. Run it a few times (that's what iterations does) and you get controlled growth instead of a one-shot jump. The mental model from the mask-and-detailing world: you're trading a tight mask for a safe mask, accepting a slightly larger inpaint region to avoid those telltale re-render seams.

The inputs that matter:

  • src - NPARRAY, your mask or image. Works on binary masks beautifully; on full-color images it dilates each channel.
  • kernel - NPARRAY, the structuring element. Here's the first gotcha: this pack has no getStructuringElement node, so you can't build a proper cross/ellipse kernel in-graph. The pragmatic move is an all-ones numpy array (a 3×3 or 5×5 block of 1s) fed in as a small NPARRAY from a numpy node in another pack. A ones block dilates uniformly, which is what you usually want anyway.
  • anchor - STRING, the kernel's reference point, entered as a Python literal, e.g. (-1, -1) for the center. This trips the pack's ast.parse-based parsing: type (-1, -1) with parens, or you get an invalid syntax error.
  • iterations - INT, how many passes. 1–3 is the typical range for mask growth.
  • borderType - INT (0 = BORDER_CONSTANT is the safe default) and borderValue - STRING (also a literal; OpenCV's "don't care" default is a special value, so passing something like 0 or (-1,-1,-1,-1) is fine here). These control how edges of the image are treated.

Optional dst is an out-parameter - leave it alone. Output: one nparray, the dilated image.

The bigger workflow point, and it applies to every node here: feed it an NPARRAY, not a Comfy IMAGE. Image2Nparray before (RGB→BGR), Nparrays2Image after, and only batch_size==1 is supported. If a mask from a MaskToImage-style node comes in as batch>1, split with ImageFromBatch first.

Install: ships in geroldmeisinger/opencv-comfyui - ComfyUI Manager → search "OpenCV", or git clone https://github.com/geroldmeisinger/opencv-comfyui into custom_nodes, restart. Needs opencv-contrib-python; the Cannot import name 'guidedFilter' error means conflicting OpenCV wheels and blocks the whole pack.

dilate_1 is the identical overload twin - same function, same inputs, use either.

Categoryimage/OpenCV

Inputs (7)

NameTypeDefaultDescription
srcNPARRAY
kernelNPARRAY
anchorSTRING
iterationsINT
borderTypeINT
borderValueSTRING
dstoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY