Nodes/opencv-comfyui/OpenCV accumulateWeighted_0
ComfyUI Node

OpenCV accumulateWeighted_0

OpenCV accumulateWeighted_0

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV accumulateWeighted_0
  • src
  • dst
  • mask
  • nparray
alpha

accumulateWeighted_0 is the useful one of the accumulate family. Where the others build raw sums, this one computes dst = (1 - alpha) * dst + alpha * src - an exponential moving average. Every frame you feed it gets blended into the running result, and old frames fade out over time. That's the standard recipe for a background model: average a video's static parts until they stabilize, and anything that moves shows up as a difference you can detect with absdiff_0.

It's also a clean noise-reduction tool. If your source is noisy but static, running a long moving average over it smooths the noise away while keeping the scene intact - the same family of trick as temporal denoising, minus the model.

How it works

The alpha parameter is the whole game. It's the weight of the new frame: small alpha (say 0.010.05) means new frames barely nudge the average, so it converges slowly and is very robust to transient motion; larger alpha adapts fast but wiggles with noise. The formula (1-alpha)*dst + alpha*src guarantees the accumulator is always a weighted average, never a runaway sum, which is why this one doesn't need the "divide by count" step the raw accumulators do. Keep dst the same size as src, and - the family constraint - keep it float32, because OpenCV asserts on uint8.

The inputs that matter

  • src (NPARRAY) - the new frame to fold in.
  • dst (NPARRAY, required) - the running average. Float32, same size as src, usually zeros at the start.
  • alpha (FLOAT, required) - how much of the new frame to blend in. Small for stability, bigger for responsiveness.
  • mask (NPARRAY, optional) - only update the average where the mask is nonzero; handy for excluding moving foreground from the background model.

Output is a single nparray - the updated running average.

The workflow that makes it shine

Feed frames of a fixed-camera video through Image2Nparray (one at a time - batch_size 1) into accumulateWeighted_0, and after a while dst is a clean background. Diff that against the current frame with absdiff_0, threshold the result, and you have a motion/foreground mask without any ML. That's a real classic-vision pipeline you can build entirely from this pack, and this node is its heart.

Installing opencv-comfyui

ComfyUI Manager → search "opencv-comfyui", or:

cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui

Restart ComfyUI. Dependency:

pip install opencv-contrib-python

You probably already have OpenCV from another pack.

Gotchas

  • dst.type() == CV_32F assertion - your accumulator is uint8. Make it float32.
  • Batch_size == 1 - slice video batches with ImageFromBatch first.
  • Alpha is a blend weight, not a rate - 0.5 isn't "medium"; it's "half the image is gone every frame". Start around 0.01–0.1 for a background model.
Categoryimage/OpenCV

Inputs (4)

NameTypeDefaultDescription
srcNPARRAY
dstNPARRAY
alphaFLOAT
maskoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY