Nodes/opencv-comfyui/OpenCV watershed_1
ComfyUI Node

OpenCV watershed_1

The twin of the segmentation node — identical inputs, no wrong choice

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV watershed_1
  • image
  • markers
  • nparray

watershed_1 is the _1 overload of cv2.watershed, which in this pack makes it a functional duplicate of watershed_0. Same two inputs, same label-map output, same marker-based segmentation math. The generator numbered the overloads and they collapsed into identical wrappers, so there's no version to prefer - whichever your graph picks up, that's the one.

What it does, once you're past the naming: marker-based image segmentation that splits clumped-together objects into individual regions. It's the classic answer to "my mask has three overlapping apples as one blob." You provide the image plus an int32 marker map where 0 means unknown, positive integers label the seeds of each region you're confident about, and -1 is reserved for boundaries. The node floods from those seeds along image gradients, and where floods collide it carves the dividing line. Output is the updated marker map - a label image, not a pretty picture - with unknowns assigned and -1 marking the boundaries.

Inputs and outputs

  • image - 8-bit, 3-channel NPARRAY (BGR).
  • markers - int32 single-channel NPARRAY, same size as the image, seeded with 0 unknown / positive labels / optional -1.
  • Output: nparray - the updated int32 label map.

The classic marker-building chain ships in this pack: threshold a mask, distanceTransform it, connectedComponents to label the centers, then feed that to watershed. All of it runs on NPARRAY, so wire it through Image2Nparray on the way in and colorize/normalize on the way out.

Installing

Pack-standard:

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

or ComfyUI Manager, search "opencv-comfyui", install, restart. Dependencies: opencv-contrib-python, numpy, torch.

The caveats worth repeating

The int32 markers are non-negotiable - a regular RGB image as markers fails or produces garbage. The output is a label map, so don't expect a viewable image until you colorize it. And seeds determine everything: too few and regions merge, too many and they fragment. If you only need a foreground cutout, a background-removal model is the sane path; watershed_1 is specifically for separating touching instances inside a mask you already have. That's a narrow, real job, and when it's the job, this (or its _0 twin) is the node.

Categoryimage/OpenCV

Inputs (2)

NameTypeDefaultDescription
imageNPARRAY
markersNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY