cv2.sumElems
Sum every pixel, get back a Scalar you can actually reuse
- src
- sum
cv2.sumElems adds up every element of an array and gives you the total, per channel. That's it. In a workflow tool it sounds useless until you notice the two things it's genuinely good at: counting mask area in pixels, and handing a Scalar to a node that wants one.
It's the sibling of cv2.trace in this pack - same idea, same output type, different reduction.
One of roughly 470 auto-generated raw cv2.* wrappers in ComfyUI CV (bmad4ever/comfyui_cv). Same disclaimer as the rest: LLM-generated, uncurated, don't ship it blind.
Inputs and outputs
src takes NPARRAY, IMAGE or MASK. An IMAGE resolves to 8-bit BGR (so you get three sums, one per channel, in BGR order - not RGB, which trips people who then read the numbers back as colours). A MASK resolves to a single-channel 8-bit array.
The output is called sum, and its type is the part worth understanding: it's a STRING holding a cv2 Scalar in the pack's literal spelling, "(c0, c1, c2, c3)" - one number per channel, exactly the format every colour and borderValue input in this pack accepts. CV Scalar and this node share one parser, so what comes out of here plugs straight into a drawing node's colour, or into a border value, or into anything else that takes a Scalar literal. It will not plug into a numeric socket, and that's deliberate: a Scalar is up to four numbers, and pretending it's one number would be a lie.
Batches: this is not one of the per-frame wrappers, so an IMAGE batch is read as frame 0. If you need a per-frame sum over a clip, split the batch and loop, or use CV Array To Numbers on a stacked array.
Two uses that justify the node
Area in pixels. Take a MASK, and the sum of one channel divided by 255 is the number of masked pixels - a ComfyUI MASK is 0..1 in memory but resolves to 0/255 here. That gives you a filter for "is this mask even worth processing": if the sum is zero, there's no subject, and you can gate the rest of the graph. It's a poor man's region-property node, and for a one-number check it's the shortest path.
A Scalar for a colour input. The sum string is a legal value anywhere a Scalar literal is expected, so a per-channel sum can be fed to a drawing node's colour field, or to borderValue on one of the padding wrappers. Weird, occasionally handy.
What it is not is a mean, a histogram, or a statistic suite. There's a real node for that - CV Array Statistic - and if you want numbers rather than a Scalar literal, CV Array To Numbers turns an array into a list of floats you can wire into ordinary maths nodes, and Inspect CV Data prints a summary you can read in the graph.
Installing the pack
cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_cv
cd comfyui_cv && pip install "opencv-contrib-python-headless~=5.0.0.93"
Manager → search ComfyUI CV → install → restart works too. Needs Python ≥ 3.12 and a ComfyUI with the V3 node API; older installs never list the nodes. Use a contrib OpenCV build - all four distributions share one site-packages/cv2, so installing plain opencv-python on top empties the contrib submodules, and the pack ships tools/repair_opencv_contrib.py (--check, then --apply) for exactly that. Curated against 5.0.0.93.
Watch out for
The BGR trap: three sums come back in OpenCV channel order, so the first number is blue. Big numbers are fine - the Scalar holds doubles, and an 8-bit image sums into the hundreds of millions without complaint. And if you were hoping to wire sum into a threshold comparison, you can't: convert the array first with CV Array To Numbers or hand the array to a curating node that reduces it for you.
Inputs (1)
| Name | Type | Default | Description |
|---|---|---|---|
| src | 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. |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| sum | STRING | - - - A cv2 Scalar as the pack's literal spelling "(c0, c1, c2, c3)" - one value per channel, and exactly what every colour / borderValue input takes. |