Nodes/ComfyUI CV/cv2.integral
ComfyUI Node

cv2.integral

The summed-area table, or 'how to average a rectangle in constant time'

By bmad4ever·Created 3 months ago·Updated 14 days ago· 1
cv2.integral
  • src
  • nparray
◄sdepthsame as input►

Here's the trick behind a surprising amount of computer vision: precompute a running sum, and then the sum of any rectangle in the image is four lookups and three subtractions, no matter how big the rectangle is. That precomputed table is an integral image, and this node builds one.

It's a raw wrapper in bmad4ever/comfyui_cv - the pack that exposes OpenCV as ComfyUI nodes. Be warned before you wire it: this is a data node, not an image filter. The output is not something you'd show anyone.

What it computes

Each output pixel holds the sum of everything above and to the left of it in the source. The result is one row taller and one column wider than the input - (W+1)×(H+1), with a zero first row and first column - which is the padding that makes the rectangle formula clean:

sum(x, y, w, h) = I(x+w, y+h) - I(x, y+h) - I(x+w, y) + I(x, y)

Divide by w*h and you have a box mean. Add a second table (cv2.integral2) and you have local variance the same way. This is how OpenCV implements adaptive thresholding internally, how Viola-Jones face detection evaluates its features, and how block-matching stereo aggregates costs - all of them are doing this arithmetic under the hood.

src takes an IMAGE, MASK or NPARRAY (8-bit or float, as the author's tooltip puts it), and the single optional widget is sdepth: same as input lets OpenCV choose (CV_32S for 8-bit input), or you can force CV_32F / CV_64F.

Output: nparray - NPARRAY, deliberately not typed as an image. The pack's source is explicit about why: the output format preservation machinery min-max normalizes anything outside 0..255, and doing that to an integral image would destroy the very numbers you built it for.

What you'd actually do with it in a workflow

You're not going to eyeball an integral image; you're going to consume the numbers. The pack's conversion and data nodes are the companions here: Preview CV Array (renders a raw array as imagery, normalized or as a heatmap), Inspect CV Data (shape, dtype, statistics - what you want first), CV Array Statistic for a whole-array reduction, CV Slice Array to window into it, and CV Array To Numbers to push values out to core FLOAT plumbing.

Realistic uses: computing a per-region mean for exposure or threshold decisions over sliding windows, getting an ROI sum without a mask-multiply, feeding a feature vector into cv2_kmeans, or implementing your own box blur when you want the O(1) version.

Installing the pack

Manager → search comfyui_cv, or:

cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_cv
pip install "opencv-contrib-python-headless~=5.0.0.93"

Python ≥ 3.12, a ComfyUI new enough for the V3 node API, and OpenCV ~=5.0.0.93 - the version the pack's behaviour is curated against. integral is core OpenCV, so it's available regardless of the contrib wheel situation.

Where people get burned

Dtype overflow. With sdepth left at "same as input", 8-bit input gives you CV_32S. A 4096×4096 frame of near-white pixels sums past 2³¹ and wraps without complaint. If you're working on anything large, or you're going to square things, force CV_64F.

Treating nparray as a picture. Wire it into an image consumer and you'll get a visual mess or a type error, depending on the consumer. It's a score table. CV Array → Image exists if you really want to look at a normalized version.

Off-by-one indexing. Every coordinate in the table is shifted by one because of that zero border. A formula that works for I fails by a border row when you index the raw source.

Building it per frame when you don't need to. It's cheap, but it is O(W×H) work per call; if you're chaining ten of these across a 300-frame batch, you'll feel it.

One caveat about the pack itself, since it applies to every article here: its README states it was written with heavy LLM involvement, that some code may be overfitted to its own tests, and that it isn't recommended for production without your own review. The maths in this node is OpenCV's, but the reporting around it is the author's - which is exactly why the tooltips are worth reading.

Categoryimage/CV/low-level/cv2 I

Inputs (2)

NameTypeDefaultDescription
srcNPARRAY,IMAGE,MASKinput image as $W \times H$, 8-bit or floating-point (32f or 64f). 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.
sdepthoptCOMBOsame as inputdesired depth of the integral and the tilted integral images, CV_32S, CV_32F, or CV_64F.

Outputs (1)

NameTypeDescription
nparrayNPARRAY—