Nodes/comfyui_cv/CV Reduce Array By Label
ComfyUI Node

CV Reduce Array By Label

Collapses an array to ONE row per label: the mean, sum, min, max or median of every element carrying that label. The classic use is a region palette - feed an image plus the label map of 'CV Superpixels' (or watershed, connected components, HFS, k-means) and get the average colour of each region, then paint it back with 'CV Take By Index' for a flat, non-photorealistic render. Rows are indexed BY LABEL VALUE (row i = label i), which is what makes that round trip work; empty labels get 'empty_value'. For per-POINT attributes with circular or densest-window statistics use 'CV Reduce Values By Label' instead.

By bmad4ever·Created 3 months ago·Updated 2 days ago· 0
CV Reduce Array By Label
  • values
  • labels
  • mask
  • table
  • counts
  • count
reductionmean
num_labels0
empty_value0.00
Categoryimage/CV/low-level

Inputs (6)

NameTypeDefaultDescription
valuesNPARRAYArray to reduce: (N,), (N,C), [H,W] or [H,W,C]. Its leading axes must match 'labels' - the trailing axis is the channel axis and is reduced independently.
labelsNPARRAYInteger label per element: (N,) or [H,W], matching the leading axes of 'values'. NEGATIVE labels are excluded (watershed writes -1 on boundaries).
reductionCOMBOmeanHow the members of each label are collapsed. mean is the region average (the palette case); median is the robust version when a region straddles an edge; sum with an all-ones input counts members (the 'counts' output does that for you).
num_labelsoptINT00–2147483647Number of rows K to emit. 0 = derive it from the largest label present. Wire the 'count' output of 'CV Superpixels' here to pin the table height even when a region ends up empty; labels >= K are then ignored.
empty_valueoptFLOAT0.00-1000000000000–1000000000000Value written for labels with no members (and for every row when the input is empty). 0 paints such a region black.
maskoptNPARRAYOptional inclusion mask, same shape as 'labels': nonzero elements take part in the reduction, zeros are ignored (e.g. drop the sky before averaging).

Outputs (3)

NameTypeDescription
tableNPARRAY(K,C) float32, row i = the reduction over label i. Always 2-D, so a single-channel input gives (K,1). Feed it to 'CV Take By Index' with the same label map to paint the result back over the image.
countsNPARRAY(K,) int32: how many elements carried each label. 0 marks a row that is 'empty_value', not a measurement.
countINTK, the number of rows in the table.