Nodes/opencv-comfyui/OpenCV integral2_0
ComfyUI Node

OpenCV integral2_0

The summed-area table, plus squared sums

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV integral2_0
  • src
  • sum
  • sqsum
  • nparray_0
  • nparray_1
sdepth
sqdepth

OpenCV integral2_0 is integral_0's smarter sibling. Where plain integral computes a single summed-area table, cv2.integral2 computes two at once: the ordinary sum table and a table of squared pixel sums. You get both in one pass, and the second one is what makes variance (and therefore standard deviation) computable over any rectangle in O(1) - because variance of a region is E[x²] − E[x]², and both of those expectations are just rectangle sums out of the two tables.

If that sounds like math, it is - this is the most "algorithmic" node in this pack, and you'll rarely wire it into a pure ComfyUI workflow. It earns its keep as a building block for adaptive operations: local contrast normalization, texture-based region analysis, tile-based statistics where you want to know "how much does this region vary" without looping over every pixel. In practice most people get those end results from adaptiveThreshold_0 or blur_0 and never meet integral2 - but if you're building a pipeline that needs per-window mean and variance, this is the one-stop node.

How it works

cv2.integral2(src, sum, sqsum, sdepth, sqdepth) makes one pass over the source and fills two (H+1, W+1) arrays. sum[i, j] is the total of all pixels in the top-left rectangle; sqsum[i, j] is the total of their squares. Two rectangle-sum lookups per region give you the mean; four give you mean of squares; a subtraction gives variance. The squared values grow fast - a 255² term per pixel - so sqdepth should be float (5 = CV_32F, 6 = CV_64F); integer squared-sums overflow embarrassingly quickly.

Inputs and outputs

  • src - NPARRAY, your image via Image2Nparray.
  • sdepth - INT, depth of the sum table: 4 (CV_32S) is fine for 8-bit input.
  • sqdepth - INT, depth of the squared-sum table. Use 5 or 6 (float) - see above.
  • sum, sqsum - optional out-parameters; the node returns both anyway.
  • Outputs nparray_0 (sum table) and nparray_1 (squared-sum table).

Installing

Part of opencv-comfyui; one install covers everything:

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

ComfyUI Manager search "opencv-comfyui" also works. No models; deps are opencv-contrib-python, numpy, torch.

Gotchas

Same family rules: don't preview these tables as images (sums exceed 255 and will clip into garbage), keep batch_size == 1, and don't stress about integral2_0 vs integral2_1 - the _1 is just the UMat overload duplicate and behaves identically. The one genuinely new footgun here is sqdepth: leave it on integer CV_32S and you can silently overflow on any reasonably large image. Float it.

Categoryimage/OpenCV

Inputs (5)

NameTypeDefaultDescription
srcNPARRAY
sdepthINT
sqdepthINT
sumoptNPARRAY
sqsumoptNPARRAY

Outputs (2)

NameTypeDescription
nparray_0NPARRAY
nparray_1NPARRAY