cv2.connectedComponentsWithStatsWithAlgorithm
Stats plus an algorithm picker, minus the good names
- image
- int
- nparray_1
- nparray_2
- nparray_3
What it is
The most specific of the four connected-components wrappers: per-blob statistics and a choice of labelling algorithm. Same output data as cv2.connectedComponentsWithStats, plus a ccltype dropdown that lets you force a particular union-find implementation instead of letting OpenCV choose.
That's a narrow reason to exist, and it's honest to say so: unless you're benchmarking labelling algorithms on your own images, cv2.connectedComponentsWithStats is the node you want. Reach for this one when you have a measured reason, or when you're reproducing someone else's numbers.
It's a raw wrapper from ComfyUI CV (bmad4ever/comfyui_cv), category image/CV/low-level/cv2 C.
How it works
Exactly as the family does. The input must be an 8-bit single-channel binary mask - every non-zero pixel is foreground - and connected foreground regions get merged into a label map with background as label 0. On top of that, OpenCV accumulates a bounding box, pixel area and centroid per label. The labelling variant is the only difference: the named algorithms are all exact, so they produce the same labels and differ only in speed on different image shapes (dense speckle versus a few large blobs). Choosing one does not change your answer.
And the standing warning: threshold before you label. Hand it a greyscale photo and everything non-zero joins into one component - no error, just a useless answer.
Inputs and outputs that matter
Read the inputs carefully, because this variant's aren't optional the way the plain WithStats node's are:
- image - required, 8-bit single-channel.
- connectivity - required INT with no preset: it reads 0. cv2 accepts only
4or8, and 0 is not shorthand for the default. Set it first; the failure otherwise is an OpenCV assertion, not a tidy message. - ltype - required COMBO, default
CV_32S(OpenCV supportsCV_32SandCV_16Ufor the labels). - ccltype - required COMBO, default
CCL_DEFAULT, plusCCL_WU,CCL_GRANA,CCL_BOLELLI,CCL_SAUF,CCL_BBDT,CCL_SPAGHETTI.CCL_DEFAULTis OpenCV's own pick - a sensible resting place.
Outputs, in order:
- int - label count, background included (five blobs → 6).
- nparray_1, nparray_2, nparray_3 - and here's the wart: the generator gave this variant generic names while the plain
WithStatsnode names the same three outputslabels,statsandcentroids. The data is identical and in that order - label map, N×5 stats table (left, top, width, height, area), N×2 centroid table. But nothing on the node tells you that. Write it down the first time you wire it, or just use the sibling node, which names them.
Row 0 of stats and centroids is background in all of these. Skip it when you reduce.
For actual work the curated nodes save you the trouble: CV Connected Components (Split Mask) to get one MASK per blob, CV Keep Largest Component, CV Components Touching Border, CV Select Component At Point, CV Region Properties when you want shape features (area, convexity, eccentricity, hole count...) rather than just a box. And Preview CV Array in heatmap mode is how you look at a label map without pretending 40 shades of grey are distinguishable.
Installing the pack
Manager → search ComfyUI CV, or:
cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_cv
Restart. Python ≥ 3.12, a V3 node API ComfyUI, and opencv-contrib-python-headless~=5.0.0.93 (plus numpy/torch).
Where people get burned
- connectivity = 0. Same trap as the other
WithAlgorithmvariant. It has no default for a reason: OpenCV has no default either. - The unnamed outputs. If your downstream node is receiving a label map where you expected centroids, this is why. Output order is
labels,stats,centroids. - This node may simply not be worth it. If you can't say why you need a specific CCL algorithm, you don't. The plain node is clearer and has better output names.
- The pack's stated risks are relevant precisely here. The README flags LLM-assisted development and a real instance of test-driven overfitting (Hu moments), warns that the registry is generated from whatever OpenCV build is installed and can therefore expose functions your build lacks, and says plainly that no updates are planned and production use needs independent review. This node is the long tail of the registry - the least-exercised corner - so treat it as "verify before trusting". There's no Reddit corpus for the pack to lean on either: the search comes back empty.
Inputs (4)
| Name | Type | Default | Description |
|---|---|---|---|
| image | NPARRAY,IMAGE,MASK | - - - Accepts a ComfyUI IMAGE/MASK directly (frame 0 of a batch) or an NPARRAY. Arithmetic ops (add, multiply, etc.) process the full IMAGE batch when both inputs have the same batch size. | |
| connectivity | INT | 0-2147483648–2147483647 | - - - |
| ltype | COMBO | CV_32S | - - - |
| ccltype | COMBO | CCL_DEFAULT | - - - |
Outputs (4)
| Name | Type | Description |
|---|---|---|
| int | INT | — |
| nparray_1 | NPARRAY | — |
| nparray_2 | NPARRAY | — |
| nparray_3 | NPARRAY | — |