how4rd / meshflow

An implementation of the MeshFlow video stabilization algorithm by S. Liu et al.
31 stars 8 forks source link
algorithms meshflow video-processing video-stabilization

MeshFlow

MeshFlow is a video stabilization algorithm described in the 2016 paper MeshFlow: Minimum Latency Online Video Stabilization by S. Liu et al. This Python implementation of MeshFlow stabilizes video offline (after recording).

At a high level, MeshFlow stabilizes a video in four steps.

1) MeshFlow estimates the unstabilized video's motion by placing a mesh over it and tracking how each mesh vertex moves. Each vertex moves with its nearby features. 2) MeshFlow computes how each mesh vertex should move in the stabilized video by minimizing an energy function. 3) MeshFlow stabilizes the video by warping it so that each mesh vertex follows its stabilized motion. 4) MeshFlow crops and resizes the video to fit its initial dimensions.

Because MeshFlow works with sparse mesh vertex motions instead of dense pixel motions, the algorithm is relatively computationally cheap. Note that this implementation was built for experimentation and is not optimized for speed.

Usage

See requirements.txt for the implementation's dependencies.

Basic usage

Example

Stabilize a video by constructing a MeshFlowStabilizer object as shown below.

stabilizer = MeshFlowStabilizer()

input_path = 'videos/video-1/video-1.m4v'
output_path = 'videos/video-1/stabilized-method-original.m4v'
stabilizer.stabilize(input_path, output_path)

Advanced usage

Constructor

The MeshFlowStabilizer constructor takes the following optional arguments.

MeshFlow variants

In addition, stabilize takes an optional adaptive_weights_definition argument. This argument specifies how to define the energy function's adaptive weights $\lambda_t$. The argument's four MeshFlowStabilizer.possible values, listed below, each describe a "variant" of MeshFlow.

Performance metrics

Finally, stabilize returns a tuple (cropping_ratio, distortion_score, stability_score) of three performance metrics describing the stabilized video. These metrics are described in the original paper. I made assumptions where the paper's definitions were vague.

An example of more advanced usage is shown below.

Example

stabilizer = MeshFlowStabilizer(mesh_row_count=20, mesh_col_count=20, visualize=True)

input_path = 'videos/video-1/video-1.m4v'
output_path = 'videos/video-1/stabilized-method-original.m4v'
stabilizer.stabilize(
    input_path,
    output_path,
    adaptive_weights_definition=MeshFlowStabilizer.ADAPTIVE_WEIGHTS_DEFINITION_CONSTANT_HIGH
)

Demos

Demo videos are available in the videos directory. Each subdirectory contains five videos: an unstabilized video, and four stabilized versions stabilized by the four MeshFlow variants described above.

Video credits are available in videos/credits.txt.