Nodes/opencv-comfyui/OpenCV integral_0
ComfyUI Node

OpenCV integral_0

The summed-area table nobody looks at

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV integral_0
  • src
  • sum
  • nparray
sdepth

OpenCV integral_0 computes an integral image - a.k.a. a summed-area table - and it's the rare node here that does not produce something you'll ever look at. That's fine; it isn't supposed to be viewable. It's a mathematical staging step. You compute it once, and then any rectangle sum in the original image - "total brightness of this box," "average over this window" - becomes one or two subtractions instead of a loop over thousands of pixels. If you've ever used adaptive thresholding, box blur approximations, or lighting-correction that divides the image by its own local average, you've indirectly used this table.

Honest framing: in a ComfyUI workflow, you will almost never drop this node in directly. It's a building block that matters when you're chaining OpenCV operations - or when you've got a scripting node doing the interesting math and you want the table computed in C++ speed rather than Python. For everyone else, adaptiveThreshold_0, blur_0, and threshold_0 in the same pack give you the end results without the intermediate. Keep this page in your back pocket for the day the math becomes relevant.

How it works

cv2.integral(src, sum, sdepth) walks the image once and writes, at position (i, j), the sum of every source pixel in the rectangle from the top-left corner down to (i, j). Because of that single pass, the output is one row and one column larger than the input - an (H, W) image becomes (H+1, W+1) - and each cell holds a potentially large integer, not a color. The rectangle-sum trick falls straight out of it: sum of region [x1..x2] × [y1..y2] is T[x2+1][y2+1] - T[x1][y2+1] - T[x2+1][y1] + T[x1][y1]. Four table lookups, done.

Inputs and outputs

  • src - the image as an NPARRAY (via Image2Nparray). Works on grayscale or color.
  • sdepth - INT, the desired output depth. The values are OpenCV depth codes: 4 for CV_32S (32-bit int), 5 for CV_32F, 6 for CV_64F. For an 8-bit source, 4 is the standard choice; if you're summing a large image and worried about overflow, bump to 6.
  • sum - optional NPARRAY out-parameter; ignore it, the result comes back on the output socket anyway.
  • Output nparray - the (H+1, W+1) integral image.

Installing

From the opencv-comfyui pack, same install as all ~635 of its nodes:

cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui
cd opencv-comfyui
pip install -r requirements.txt

or ComfyUI Manager → search "opencv-comfyui". No model downloads; deps are opencv-contrib-python, numpy, torch.

Gotchas

Do not feed the integral output straight into Nparrays2Image and expect a picture. The values are sums that run far past 255, and the conversion node just divides by 255 with no scaling - you'll get a blown-out, clipped mess, which is exactly the "this nparray is not an image" trap the README warns about. Preview the source, not the table. And keep batch_size == 1; like everything here, the pack bails with Only images with batch_size==1 are supported! otherwise. One more thing: integral_0 vs integral_1 - identical behavior, an artifact of OpenCV listing the function twice in its stubs (MatLike vs UMat). Pick one and never think about it again.

Categoryimage/OpenCV

Inputs (3)

NameTypeDefaultDescription
srcNPARRAY
sdepthINT
sumoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY