flow_utils.py

Basic functions to handle optical flow.

ptlflow.utils.flow_utils.flow_read(input_file: str | Path | IO, format: str | None = None) ndarray[source]

Read optical flow from file.

This is just a wrapper for flowpy (for .flo and .png) or raft (for pfm), added for convenience.

Parameters:
input_file: str, pathlib.Path or IO

Path of the file to read or file object.

format: str, optional

Specify in what format the flow is read, accepted formats: “png”, “flo”, “pfm”, or “flo5”. If None, it is guessed on the file extension.

Returns:
numpy.ndarray

3D flow in the HWF (Height, Width, Flow) layout. flow[…, 0] is the x-displacement. flow[…, 1] is the y-displacement.

See also

ptlflow.utils.external.flowpy.flow_read
ptlflow.utils.external.raft.read_pfm
ptlflow.utils.external.flow_IO.readFlo5Flow
write_pfm
ptlflow.utils.flow_utils.flow_to_rgb(flow: ndarray | Tensor, flow_max_radius: float | Tensor | None = None, background: str = 'bright', custom_colorwheel: Tensor | None = None) ndarray | Tensor[source]

Convert flows to RGB images.

The input can be either numpy or torch tensors. This function is just a wrapper for flowpy and flowpy_torch.

Parameters:
flownp.ndarray or torch.Tensor

If flow is a numpy array, then it must have 3 dimensions HWC (Height, Width, Channels) - notice it is channels last. If it is a torch tensor, then it has at least 3 dimensions in the …CHW (…, Channels, Height, Width) layout, where … represents any number of dimensions. Channel 0 should be the x-displacement. Channel 1 should be the y-displacement.

flow_max_radiusfloat or torch.Tensor, optional

Set the radius that gives the maximum color intensity, useful for comparing different flows. Default: The normalization is based on the input flow maximum radius per batch element.

backgroundstr, default ‘bright’

States if zero-valued flow should look ‘bright’ or ‘dark’.

custom_colorwheelnp.ndarray or torch.Tensor

Use a custom colorwheel for specific hue transition lengths. By default, the default transition lengths are used.

Returns:
np.ndarray or torch.Tensor

The RGB image representing the flow. It keeps the same dimensions and type as the input.

See also

ptlflow.utils.external.flowpy.flow_to_rgb
ptlflow.utils.flowpy_torch.flow_to_rgb
ptlflow.utils.flow_utils.flow_write(output_file: str | Path | IO, flow: ndarray, format: str | None = None) None[source]

Write optical flow to file.

This is just a wrapper for flowpy (for .flo and .png) or selflow (for pfm), added for convenience.

Parameters:
output_file: str, pathlib.Path or IO

Path of the file to write or file object.

flow: numpy.ndarray

3D flow in the HWF (Height, Width, Flow) layout. flow[…, 0] should be the x-displacement flow[…, 1] should be the y-displacement

format: str, optional

Specify in what format the flow is written, accepted formats: “png” or “flo” If None, it is guessed on the file extension

See also

ptlflow.utils.external.flowpy.flow_write