Nodes/RyanOnTheInside/Optical Flow Direction Mask βš‘πŸ…‘πŸ…žπŸ…£πŸ…˜
ComfyUI Node

Optical Flow Direction Mask βš‘πŸ…‘πŸ…žπŸ…£πŸ…˜

Mask only the pixels moving a specific way

By ryanontheinsideΒ·Created 2 years agoΒ·Updated 5 months agoΒ· 852
Optical Flow Direction Mask βš‘πŸ…‘πŸ…žπŸ…£πŸ…˜
  • masks
  • images
  • MASK
β—„strength1.00β–Ί
β—„invertfalseβ–Ί
β—„subtract_original0.00β–Ί
β—„grow_with_blur0.0β–Ί
β—„flow_methodβ–Ύβ–Ί
β—„flow_threshold0.10β–Ί
β—„magnitude_threshold0.05β–Ί
β—„directionβ–Ύβ–Ί
β—„angle_threshold45β–Ί
β—„blur_radius3β–Ί

Most motion-masking tools give you "moving vs. not moving." This one is fussier, in a useful way - it masks only the pixels moving in a specific direction. Point it at footage of a crowd and isolate just the people walking left, or point it at a spinning object and mask only what's rotating clockwise. Under the hood it's the same optical-flow analysis behind MotionFeatureNode and OpticalFlowParticleSystem elsewhere in this pack, just aimed at producing a mask instead of a number or a particle trail.

How it works

flow_method picks the algorithm - Farneback (dense, the safe general default), LucasKanade/PyramidalLK (sparse point tracking, faster but can miss motion in flat, low-texture regions), or DIS (a modern dense method tuned for speed). flow_threshold and magnitude_threshold filter out weak or noisy motion before it's considered at all.

direction is the whole point of this node, and it's more than left/right: horizontal and vertical cover straight-line motion, but radial_in/radial_out catch things moving toward or away from the frame center (a zoom, or something approaching/receding), and clockwise/counterclockwise catch rotational motion around the center. angle_threshold (default 45Β°) is the tolerance - how far a pixel's motion angle can deviate from your target direction and still count. blur_radius softens the resulting mask edges so you don't get a jagged, pixel-exact cutout.

This node requires an existing masks input too - not optional - combined with the shared strength/invert/subtract_original/grow_with_blur controls you'll see across this pack's mask-processing nodes. If you just want the raw direction analysis over the whole frame with nothing subtracted, feed it a plain white mask.

The inputs and outputs that matter

  • images (required, IMAGE) - the sequence to compute flow from.
  • masks (required, MASK) - the base mask this refines; use an all-white mask to consider the whole frame.
  • direction - horizontal / vertical / radial_in / radial_out / clockwise / counterclockwise.
  • angle_threshold (default 45Β°) - how strict the direction match is.
  • blur_radius (default 3) - edge softening.
  • strength, invert, subtract_original, grow_with_blur - the pack's standard mask post-processing set.

Output is a single MASK.

How to install it

ComfyUI Manager: search "RyanOnTheInside", install, restart. Manually:

cd ComfyUI/custom_nodes
git clone https://github.com/ryanontheinside/ComfyUI_RyanOnTheInside
cd ComfyUI_RyanOnTheInside
pip install -r requirements.txt

then restart. Optical flow runs on OpenCV, pinned in requirements.txt - no model download, but if your pack install didn't complete cleanly, this is one of the nodes most likely to fail at load rather than at runtime.

Common issues & troubleshooting

Mask comes back empty even though there's obvious motion in the right direction. Loosen angle_threshold first - 45Β° sounds generous, but real-world motion (a walking gait, a hand gesture) rarely moves in a perfectly straight line, and a strict threshold can reject most of it. Also double-check flow_threshold/magnitude_threshold aren't filtering the motion out before direction is even evaluated.

Rotational directions (clockwise/counterclockwise) never trigger. Those depend on motion actually curving around the frame center - if your subject is off to one side rather than centered, or the rotation is around a point far from frame-center, this direction analysis won't line up with what you're picturing. It's evaluating rotation relative to the image center, not relative to your subject.

Result looks jagged at the edges. Raise blur_radius - the raw direction match is a per-pixel binary decision, and without smoothing it can look harsh, especially on noisy footage.

CategoryRyanOnTheInside/OpticalFlow

Inputs (12)

NameTypeDefaultDescription
masksMASKInput mask or sequence of masks to be processed (MASK type)
strengthFLOAT1.000–1Overall strength of the mask effect (0.0 to 1.0)
invertBOOLEANfalseWhen enabled, reverses the mask output
subtract_originalFLOAT0.000–1Amount of the original mask to subtract from the result (0.0 to 1.0)
grow_with_blurFLOAT0.00–10Amount of Gaussian blur to apply for mask growth (0.0 to 10.0)
imagesIMAGESequence of images to calculate optical flow from (IMAGE type)
flow_methodCOMBOAlgorithm used to calculate optical flow ('Farneback', 'LucasKanade', 'PyramidalLK')
flow_thresholdFLOAT0.100–1Minimum flow magnitude to consider (0.0 to 1.0)
magnitude_thresholdFLOAT0.050–1Relative threshold for flow magnitude as fraction of maximum (0.0 to 1.0)
directionCOMBODirection of motion to detect ('horizontal', 'vertical', 'radial_in', 'radial_out', 'clockwise', 'counterclockwise')
angle_thresholdFLOAT450–180Maximum angle deviation from target direction (0.0 to 180.0 degrees)
blur_radiusINT30–20Amount of smoothing applied to the mask (0 to 20 pixels)

Outputs (1)

NameTypeDescription
MASKMASKβ€”