Nodes/opencv-comfyui/OpenCV connectedComponentsWithStatsWithAlgorithm_0
ComfyUI Node

OpenCV connectedComponentsWithStatsWithAlgorithm_0

Blob stats plus a pick-your-algorithm knob (connectedComponentsWithStatsWithAlgorithm_0)

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV connectedComponentsWithStatsWithAlgorithm_0
  • image
  • labels
  • stats
  • centroids
  • int
  • nparray_1
  • nparray_2
  • nparray_3
connectivity
ltype
ccltype

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 Image2Nparray then cvtColor code=6 (BGR2GRAY), or get the (-215:Assertion failed) img.type() == CV_8UC1 error.
  • connectivity (INT) - 4 or 8. Use 8.
  • ltype (INT) - 4 (CV_32S) or 2 (CV_16U). Use 4.
  • ccltype (INT) - the algorithm list above. 0 to 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.

Categoryimage/OpenCV

Inputs (7)

NameTypeDefaultDescription
imageNPARRAY
connectivityINT
ltypeINT
ccltypeINT
labelsoptNPARRAY
statsoptNPARRAY
centroidsoptNPARRAY

Outputs (4)

NameTypeDescription
intINT
nparray_1NPARRAY
nparray_2NPARRAY
nparray_3NPARRAY