Nodes/ComfyUI CV/cv2.integral2
ComfyUI Node

cv2.integral2

Two summed-area tables, so local variance is also free

By bmad4ever·Created 4 months ago·Updated 15 days ago· 1
cv2.integral2
  • src
  • sum
  • sqsum
◄sdepthsame as input►
◄sqdepthsame as input►

Plain cv2.integral gives you rectangle sums. cv2.integral2 gives you rectangle sums and sums of squares - which is the pair you need for mean and variance over any window, in constant time.

That second table is the interesting half. Variance from an integral pair is the standard trick: E[x²] - E[x]², both terms computed from four corner lookups each. Suddenly "how busy is this region?" and "how much does this patch deviate from its mean?" are O(1) questions, which is why normalized cross-correlation, texture measures and a whole family of detectors are built on this shape of precomputation.

This is one of the ~470 auto-generated cv2.* wrappers in bmad4ever/comfyui_cv, and like its sibling it's a data-produced-here, consumed-elsewhere node.

Inputs and outputs

  • src - IMAGE, MASK or NPARRAY. 8-bit or floating-point input is what OpenCV expects.
  • sdepth - depth of the sum table. same as input (the default) lets OpenCV pick, which for 8-bit input means CV_32S. You can force CV_32F or CV_64F.
  • sqdepth - depth of the squared table: CV_32F or CV_64F, with same as input defaulting to CV_64F (the author's tooltip spells this out).

Two outputs, both NPARRAY: sum and sqsum. Both are (W+1)×(H+1) with a zero border, same as cv2_integral, so the four-corner rectangle formula applies to each.

Why squares matter

For any rectangle, mean = sum / area and variance = sqsum / area − mean². Two lookups each, then arithmetic. That gives you:

  • Sliding-window statistics without a convolve: local contrast maps, "is this region flat or busy" decisions, adaptive thresholds chosen per region rather than globally.
  • Normalized matching, where you compare a patch to a template with brightness invariance. That's the np-arith you'd otherwise do per-candidate.
  • Variance normalization of a score map, e.g. before deciding which outlier is "really" an outlier - which is the same reason the pack's own deblur subgraphs saturate before stretching rather than stretching raw floats.

Because the outputs are NPARRAY, the workflow around them is arithmetic and data plumbing: cv2_subtract / cv2_divide, CV Scalar Array for constants, CV Array Statistic, Inspect CV Data when you want numbers rather than vibes, and CV Array → Image if you want to see a normalized variance map.

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 and a recent ComfyUI (V3 node API). Behaviour is curated against OpenCV 5.0.0.93. Core function, no models, no contrib needed for this node specifically.

Where people get burned

Overflow, and it's worse here. The squared sums grow far faster than the sums. A CV_32S table over a big frame will wrap, and wrapped variance is worse than no variance: the numbers look plausible and are meaningless. If you're on anything larger than a small crop, force CV_64F for both depths.

Float rounding in E[x²] − E[x]². For nearly-constant regions, that subtraction is two large nearly-equal numbers. In CV_32F you'll sometimes get a small negative variance. Clamp at zero, or work in CV_64F.

Reading sqsum as a precomputed patch energy. It's sums of squared pixel values, not anything perceptual. Photometric magnitudes are deceiving; normalizing is on you.

Forgetting the border. Same off-by-one as cv2_integral - every rectangle formula indexes the table, not the source.

Standard disclosure for this pack: the README says it was built with heavy LLM use, that some behaviour may be overfitted to its own test cases, and that production use needs your own review. Take the tooltips as genuinely useful documentation and verify your arithmetic on a known case - it's a two-line sanity check.

Categoryimage/CV/low-level/cv2 I

Inputs (3)

NameTypeDefaultDescription
srcNPARRAY,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.
sdepthoptCOMBOsame as input - - -
sqdepthoptCOMBOsame as input - - -

Outputs (2)

NameTypeDescription
sumNPARRAY—
sqsumNPARRAY—