Nodes/comfyui_cv/CV Histogram
ComfyUI Node

CV Histogram

Counts how often each color value occurs (cv2.calcHist): picks one or more channels, splits their range into bins and returns the bin counts as an NPARRAY (1-D for one channel, 2-D for two...). Give it a mask to histogram only a region - e.g. the polygon of 'CV Annotate Points' over an object - and set normalize to 'peak = 255' to feed the result straight into 'CV Back Project' (the meanShift/CamShift tracking recipe). Convert to HSV first (cv2_cvtColor) to histogram hue rather than raw BGR. An empty mask yields an all-zero histogram and pixels=0 instead of an error. Preview 1-D/2-D histograms with 'Preview CV Array' (normalize/heatmap).

By bmad4ever·Created 3 months ago·Updated 2 days ago· 0
CV Histogram
  • image
  • mask
  • histogram
  • pixels
channels0
bins32
ranges0, 256
normalizenone (raw counts)
Categoryimage/CV/segmentation

Inputs (6)

NameTypeDefaultDescription
imageNPARRAY,IMAGEArray to histogram, in whatever color space you want to count (convert with cv2_cvtColor first). A ComfyUI IMAGE arrives as BGR uint8. 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.
channelsSTRING0Channel indices to histogram, comma-separated, e.g. '0' (hue of an HSV array) or '0, 1' (hue + saturation). Must exist in the image.
binsSTRING32Bin count per channel, comma-separated (a single count broadcasts), e.g. '180' for every 8-bit hue or '30, 32' for a 2-D hue x saturation histogram. Fewer bins = smoother, more tolerant matching.
rangesSTRING0, 256Value range per channel as 'lo, hi' pairs, comma-separated and flattened, e.g. '0, 180' for 8-bit hue or '0, 180, 0, 256' for hue + saturation. A single pair broadcasts to every channel. 'hi' is EXCLUSIVE (8-bit full range = 0, 256).
normalizeCOMBOnone (raw counts)Post-scaling: raw counts; peak stretched to 255 (what cv2.calcBackProject expects); or divided by the total so the bins sum to 1. An all-zero histogram stays zero in every mode.
maskoptNPARRAY,MASKOptional region mask: non-zero pixels are counted, the rest ignored. Resized to the image if needed. Omit to histogram the whole image. 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.

Outputs (2)

NameTypeDescription
histogramNPARRAYfloat32 bin counts, one axis per channel (shape (bins,) for one channel, (bins0, bins1) for two). Feed to 'CV Back Project' with the SAME channels + ranges.
pixelsINTHow many pixels were counted (inside the mask and the ranges) - 0 means the histogram is empty.