Nodes/comfyui_cv/CV Optical Flow (TV-L1)
ComfyUI Node

CV Optical Flow (TV-L1)

Dense optical flow by TV-L1 energy minimization (cv2.optflow.DualTVL1OpticalFlow) - the accuracy reference of the classic (non-learned) methods. Its L1 data term tolerates illumination change and occlusion far better than Farneback's least squares, and its total-variation prior keeps motion discontinuities SHARP instead of smearing them. The price is speed: expect seconds per megapixel, roughly an order of magnitude slower than 'CV Optical Flow (DIS)'. Use it for offline quality work and DIS for anything interactive. Connect 'init_flow' (the previous pair's flow) to warm-start a video sequence.

By bmad4ever·Created 3 months ago·Updated 2 days ago· 0
CV Optical Flow (TV-L1)
  • frame_a
  • frame_b
  • init_flow
  • flow
  • magnitude
  • angle
lambda_0.15
scales5
warps5
epsilon0.010
tau0.25
theta0.30
gamma0.00
scale_step0.80
inner_iterations30
outer_iterations10
median_filtering5
Categoryimage/CV/contrib

Inputs (14)

NameTypeDefaultDescription
frame_aNPARRAY,IMAGEFirst frame (earlier in time); converted to grayscale. 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.
frame_bNPARRAY,IMAGESecond frame; resized to frame_a if the sizes differ. 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.
lambda_FLOAT0.150.001–2Weight of the data term against the smoothness term. LOWER = smoother flow (the prior wins); higher follows the pixels and picks up noise.
scalesINT51–10Pyramid levels. More levels capture larger motion; each costs time.
warpsINT51–20Warping steps per pyramid level - the outer linearization loop. More is more accurate and linearly slower; this is the main speed knob.
epsilonFLOAT0.0100.0001–1Stopping threshold. Tighter (smaller) is more accurate and much slower.
init_flowoptNPARRAYOptional HxWx2 float32 flow to start from - pass the previous frame pair's flow when processing video. It is copied, never written to, so the upstream node's cached array stays safe.
tauoptFLOAT0.250.01–1Time step of the dual formulation. The theory needs tau < 0.125 for guaranteed convergence; cv2 ships 0.25 because it works in practice.
thetaoptFLOAT0.300.01–2Tightness of the coupling between the two variables the dual scheme splits the problem into. Small values slow convergence.
gammaoptFLOAT0.000–1Weight of the gradient-constancy term. Above 0 it adds robustness to brightness changes at some cost; cv2's default is 0 (off).
scale_stepoptFLOAT0.800.1–0.95Size ratio between consecutive pyramid levels.
inner_iterationsoptINT301–200Inner iterations used to solve the linearized problem at each warp.
outer_iterationsoptINT101–100Outer iterations of the whole scheme.
median_filteringoptINT51–9Aperture of the median filter applied between iterations to reject outliers. 1 disables it.

Outputs (3)

NameTypeDescription
flowNPARRAYHxWx2 float32 (dx, dy) displacement per pixel.
magnitudeNPARRAYHxW float32 motion magnitude in pixels.
angleNPARRAYHxW float32 motion direction in RADIANS - feed 'CV Flow To Color' in its default radians mode.