AdamSpannbauer / python_video_stab

A Python package to stabilize videos using OpenCV
https://adamspannbauer.github.io/python_video_stab/html/index.html
MIT License
682 stars 118 forks source link

Enhancement request? 'Tripod Mode' #102

Open ScissorHill opened 4 years ago

ScissorHill commented 4 years ago

FFMPGEG vidstab has a very useful feature called 'tripod mode'.

Essentially this simulates the effect of a camera placed on a tripod.

As I understand it, it works by taking one frame (typically the first) as reference.

Is there a way this can currently done with python_video_stab, through setting some arguments? If not, it would be nice to have it, as it seems to be a fairly common use case.

AdamSpannbauer commented 4 years ago

Currently, this is not a feature of this project. It sounds like a reasonable and doable request. Thanks for filing the issue. I'll put it on the TODO list.

Info from the above-linked project:

tripod Set reference frame number for tripod mode. If enabled, the motion of the frames is compared to a reference frame in the filtered stream, identified by the specified number. The intention is to compensate all movements in a more-or-less static scene and keep the camera view absolutely still. If set to 0, it is disabled. The frames are counted starting from 1. NOTE: If this mode is used in first pass then it should also be used in second pass.

AdamSpannbauer commented 4 years ago

Work on this has begun in tripod#102 branch

ScissorHill commented 4 years ago

Nice, appreciated!

Was about to suggest a translation-only option as well, but it's perhaps best to have that as a separate feature request.

mu22le commented 3 years ago

Hi, what is the status of this branch? Is it worth I try to use or it is completely broken right now?

AdamSpannbauer commented 3 years ago

I believe the logic in the tripod#102 is correct, but my test case video had poor results that make me wonder if there's an off by 1 error (or some other issue) somewhere in the base logic.

You might try installing and using the feature, but I don't want to guarantee great results.

w-m commented 3 years ago

I was wondering the same thing. How about something like this:

from vidstab import VidStab

input_path = "ostrich.mp4"
output_path = "ostrich_tripod.mp4"

stabilizer = VidStab()
stabilizer.gen_transforms(input_path=input_path) 

# reference frame is mostly important if we don't set `border_size=auto` later
reference_frame = 150

# trajectory already contains a cumsum of the frame-to-frame transforms
# let's use the inverse trajectory to map all frames to the reference
stabilizer.transforms = stabilizer.trajectory[reference_frame] - stabilizer.trajectory

# `use_stored_transforms` is important here, or it will generate them again, and overwrite .transforms
stabilizer.stabilize(input_path=input_path, output_path=output_path, border_size="auto", use_stored_transforms=True)

Result for the ostrich video. Look at the trees in the middle of the video, they generally stay at the same place with the camera panning around:

https://user-images.githubusercontent.com/1588593/116457107-4bb03580-a863-11eb-91f2-c6225df6f599.mp4