Nodes/opencv-comfyui/OpenCV connectedComponentsWithAlgorithm_0
ComfyUI Node

OpenCV connectedComponentsWithAlgorithm_0

Faster blob counting when your mask has ten thousand pieces (connectedComponentsWithAlgorithm_0)

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV connectedComponentsWithAlgorithm_0
  • image
  • labels
  • int
  • nparray
connectivity
ltype
ccltype

connectedComponentsWithAlgorithm_0 is the version of connectedComponents_0 where you get to pick which algorithm does the labeling. That's the whole difference. The results are the same - a count and a label map - but when your mask is huge or absurdly noisy, the default labeling algorithm isn't always the fastest one, and this node lets you swap.

What it does

Same connected-components labeling: count the blobs in a binary mask, return the number and a per-pixel label map. The extra input is ccltype, OpenCV's connected-components-labeling algorithm selector:

  • 0 - CCL_DEFAULT (the library's choice; fine for almost everything)
  • 1 - CCL_WU (fast union-find; the usual modern default)
  • 2 - CCL_GRANA, 3 - CCL_BOLELLI (older but sometimes better on specific shapes)
  • 4 - CCL_SAUF (can be faster when there are very many labels - the "ten thousand pieces" case)
  • 5 - CCL_BBDT, 6 - CCL_SPAGHETTI

Honest advice: start with 0 or 1 and only start experimenting if you're processing big images in a loop and the node is actually your bottleneck. Labeling is fast. The algorithm choice matters about as often as it looks like it does - which is to say, rarely.

The inputs and outputs

  • image (NPARRAY) - 8-bit single-channel binary mask. Convert with Image2Nparray, then cvtColor code=6 (BGR2GRAY). Skip it and you'll meet (-215:Assertion failed) img.type() == CV_8UC1.
  • connectivity (INT) - 4 or 8. Use 8.
  • ltype (INT) - label dtype, 4 (CV_32S) or 2 (CV_16U). Use 4.
  • ccltype (INT) - the algorithm, above. 0 to start.
  • labels (NPARRAY, optional) - out-parameter; skip it.
  • Outputs: int (component count) and nparray (label map).

Where it fits

Exactly where connectedComponents_0 fits - this is the same tool with a tuning knob bolted on. Use it in mask-cleanup and object-counting pipelines where the default algorithm's performance isn't cutting it, or where you want the determinism of pinning a specific algorithm. Everything else - grayscale input, background label 0, don't preview the label map as an image - carries over unchanged.

Install

Same pack as all the OpenCV nodes:

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

or ComfyUI Manager → "opencv-comfyui". Requires opencv-python-contrib. Pack-wide gotchas apply: the guidedFilter OpenCV conflict, and Image2Nparray's batch-size-1 limit (split with ImageFromBatch). Start with ccltype=0, and only start reading algorithm papers when the profiler tells you to.

Categoryimage/OpenCV

Inputs (5)

NameTypeDefaultDescription
imageNPARRAY
connectivityINT
ltypeINT
ccltypeINT
labelsoptNPARRAY

Outputs (2)

NameTypeDescription
intINT
nparrayNPARRAY