Nodes/opencv-comfyui/OpenCV floodFill_0
ComfyUI Node

OpenCV floodFill_0

The flood fill node that speaks in tuples and strings

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV floodFill_0
  • image
  • mask
  • int
  • nparray_1
  • nparray_2
  • literal
seedPoint
newVal
loDiff
upDiff
flags

Remember the paint bucket in MS Paint? That's flood fill. You pick a pixel, pick a color, and everything connected to that pixel within a color tolerance gets repainted. floodFill_0 wraps OpenCV's cv2.floodFill for ComfyUI, and it's a genuinely useful tool once you get past its awkward, auto-generated interface - because here's the thing: ComfyUI's mask tooling is powerful, but sometimes you just want to fill a connected region in an image, and flood fill is the classic, deterministic way to do it.

Where would you use it? Removing a background patch by filling it with a neighboring color. Cleaning up a small artifact inside a detected region. Seeding a region for further processing. It's a post-processing primitive in the same "no model, no seed, milliseconds" bucket as everything else in this pack - deterministic, cheap, and exactly as surprising as the docs say.

How it works

You give it an image, a seed point, and a new color. It looks at the seed pixel, then floods outward to every pixel whose color is within a tolerance - between loDiff and upDiff of the seed - recoloring the whole connected blob. OpenCV returns the number of pixels it filled, the modified image, an updated mask, and the bounding rectangle of the filled region.

Inputs that matter

This node shows the pack's "composite types as strings" design in full flower:

  • image (NPARRAY) - the image to fill (via Image2Nparray).
  • seedPoint (STRING) - a tuple literal, e.g. (100, 200). Note it's parsed with Python's literal_eval, so the exact syntax matters.
  • newVal (STRING) - the fill color as a BGR tuple, e.g. (255, 0, 0) for pure red... in BGR. Remember OpenCV order.
  • loDiff / upDiff (STRING) - lower/upper color tolerance from the seed, e.g. (10, 10, 10).
  • flags (INT) - connectivity and mode. 4 or 8 is connectivity; add 65536 (1<<16) for fixed-range mode (compare to the seed color rather than the running fill color). 4 is the usual starting point.

Optional mask (NPARRAY) is a per-pixel mask that limits where filling can happen - ignore it at first.

Outputs

Four of them: int (pixels filled), nparray_1 (the filled image - wire this into Nparrays2Image), nparray_2 (the updated mask), and literal (the bounding rect of the filled region as a string). The rect comes out as text because Rect is a composite type the pack serializes rather than parses.

Installing

Part of geroldmeisinger/opencv-comfyui. ComfyUI Manager: search "opencv-comfyui". Or:

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

Restart ComfyUI. requirements.txt installs opencv-contrib-python, numpy, and torch.

Common issues

If you see invalid syntax on this node, it's your string literals - (10, 10, 10) and (100, 200), with real parentheses and commas. Also: Image2Nparray is single-batch only, keep the IMAGE → Image2Nparray → … → Nparrays2Image chain intact, and remember OpenCV is BGR while Comfy is RGB, so newVal colors are written in BGR order.

One flood-fill-specific trap: if the fill "leaks" everywhere, your tolerance (loDiff/upDiff) is too big or your image has smooth gradients - lower the tolerance. And if nothing fills at all, check that seedPoint is inside the image. The node is verbose and the syntax is finicky, but for connected-region fills it beats hand-drawing a mask almost every time.

Categoryimage/OpenCV

Inputs (7)

NameTypeDefaultDescription
imageNPARRAY
seedPointSTRING
newValSTRING
loDiffSTRING
upDiffSTRING
flagsINT
maskoptNPARRAY

Outputs (4)

NameTypeDescription
intINT
nparray_1NPARRAY
nparray_2NPARRAY
literalSTRING