OpenCV

class byotrack.implementation.optical_flow.opencv.OpenCVOpticalFlow(optical_flow: DenseOpticalFlow, downscale: float | ndarray = 2, blur: None | float | ndarray = None)

Bases: OpticalFlow

Wraps Open-CV optical flow implementations

Currently ByoTrack only supports dense optical flow which computes the displacement of every pixel of the reference image. And OpenCV only supports 2D images.

Usage:

import cv2
from byotrack.implementation.optical_flow.opencv import OpenCVOpticalFlow

# See the documentation of Open-CV for each algorithm to correctly set the parameters
parameters = {}

# Farneback
optflow = OpenCVOpticalFlow(cv2.FarnebackOpticalFlow.create(**parameters))

# TVL1 => It requires to install opencv-contrib-python (and not just opencv-python)
optflow = OpenCVOpticalFlow(cv2.optflow.DualTVL1OpticalFlow.create(**parameters))

# Other methods from opencv-contrib could probably be used (RLOF, DIS, ...) but have not been tested yet
optical_flow

Any implementation of the cv2 DenseOpticalFlow class

Type:

cv2.DenseOpticalFlow

compute(reference: ndarray, moving: ndarray) ndarray

Compute the optical flow map from reference to the moving frame

It computes the displacement of each pixel from the reference frame to be in the moving one.

Parameters:
  • reference (np.ndarray) – Reference frame Shape: ([D’, ]H’, W’, C), dtype: float

  • moving (np.ndarray) – Moving frame Shape: ([D’, ]H’, W’, C), dtype: float

Returns:

Optical flow map from reference to moving

The flow field is stored in pixel coordinates ([dk’, ]di’, dj’) (!= xyz) Shape: (dim, [D’, ]H’, W’), dtype: float32

Return type:

np.ndarray