OpenCV connectedComponentsWithStatsWithAlgorithm_0
Blob stats plus a pick-your-algorithm knob (connectedComponentsWithStatsWithAlgorithm_0)
- image
- labels
- stats
- centroids
- int
- nparray_1
- nparray_2
- nparray_3
If connectedComponentsWithStats_0 is the workhorse, this is the workhorse with every option ticked. connectedComponentsWithStatsWithAlgorithm_0 gives you the full stats package - labels, bounding boxes, areas, centroids - and lets you pick which labeling algorithm OpenCV uses. It's the superset node of the family: everything the plain versions do, plus the ccltype tuning knob.
What it does
It's cv2.connectedComponentsWithStatsWithAlgorithm, which is exactly connectedComponentsWithStats with an explicit algorithm parameter. Label every connected region of your binary mask, then report:
- stats -
N×5: left, top, width, height, area for each component. - centroids -
N×2: x/y centers. - labels - the per-pixel ID map.
The added input, ccltype, selects the labeling algorithm: 0 CCL_DEFAULT, 1 CCL_WU (the fast union-find, usually the modern default), 2 CCL_GRANA, 3 CCL_BOLELLI, 4 CCL_SAUF (worth trying with very large label counts), 5 CCL_BBDT, 6 CCL_SPAGHETTI. My take: 0 or 1 covers essentially all real workflows, and you reach for the others only when profiling a hot loop says labeling is actually your bottleneck - which, for typical ComfyUI mask work, it isn't.
The inputs and outputs
- image (NPARRAY) - 8-bit single-channel binary mask. Convert with
Image2NparraythencvtColorcode=6(BGR2GRAY), or get the(-215:Assertion failed) img.type() == CV_8UC1error. - connectivity (INT) -
4or8. Use8. - ltype (INT) -
4(CV_32S) or2(CV_16U). Use4. - ccltype (INT) - the algorithm list above.
0to start. - labels / stats / centroids (NPARRAY, optional) - out-parameters; the README says skip them.
- Outputs:
int(component count),nparray_1(labels),nparray_2(stats),nparray_3(centroids).
Where it fits
Everywhere connectedComponentsWithStats_0 fits - mask cleanup, object counting, speckle filtering by area - plus the option to pin a specific algorithm when you want determinism or a speed edge on big noisy masks. Since this pack doesn't ship findContours/drawContours, this is about as complete as blob analysis gets in the pack. The label map is data, not a picture; make decisions on the int and the stats, not by eyeballing a preview.
Install
Same pack, same routine: ComfyUI Manager (search "opencv-comfyui") or
cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui
Requires opencv-python-contrib. Pack-wide traps: the guidedFilter OpenCV conflict and Image2Nparray's batch-size-1 limit (split with ImageFromBatch, length=1). Start with ccltype=0; the algorithm knob is there for when you need it, and you mostly won't.
Inputs (7)
| Name | Type | Default | Description |
|---|---|---|---|
| image | NPARRAY | — | |
| connectivity | INT | — | |
| ltype | INT | — | |
| ccltype | INT | — | |
| labelsopt | NPARRAY | — | |
| statsopt | NPARRAY | — | |
| centroidsopt | NPARRAY | — |
Outputs (4)
| Name | Type | Description |
|---|---|---|
| int | INT | — |
| nparray_1 | NPARRAY | — |
| nparray_2 | NPARRAY | — |
| nparray_3 | NPARRAY | — |