Video Stabilization using OpenCV
Project description
Python Video Stabilization
Python video stabilization using OpenCV.
This module contains a single class (VidStab
) used for video stabilization. This class is based on the work presented by Nghia Ho in SIMPLE VIDEO STABILIZATION USING OPENCV. The foundation code was found in a comment on Nghia Ho's post by the commenter with username koala.
Input | Output |
---|---|
Video used with permission from HappyLiving
Installation
+ Please report issues if you install/try to install and run into problems!
Install vidstab
without installing OpenCV
If you've already built OpenCV with python bindings on your machine it is recommended to install vidstab
without installing the pypi versions of OpenCV. The opencv-python
python module can cause issues if you've already built OpenCV from source in your environment.
The below commands will install vidstab
without OpenCV included.
From PyPi
pip install vidstab
From Github
pip install git+https://github.com/AdamSpannbauer/python_video_stab.git
Install vidstab
& OpenCV
If you don't have OpenCV installed already there are a couple options.
- You can build OpenCV using one of the great online tutorials from PyImageSearch, LearnOpenCV, or OpenCV themselves. When building from source you have more options (e.g. platform optimization), but more responsibility. Once installed you can use the pip install command shown above.
- You can install a pre-built distribution of OpenCV from pypi as a dependency for
vidstab
(see command below)
The below commands will install vidstab
with opencv-contrib-python
as dependencies.
From PyPi
pip install vidstab[cv2]
From Github
pip install -e git+https://github.com/AdamSpannbauer/python_video_stab.git#egg=vidstab[cv2]
Usage
The VidStab
class can be used as a command line script or in your own custom python code.
Using from command line
# Using defaults
python3 -m vidstab --input input_video.mov --output stable_video.avi
# Using a specific keypoint detector
python3 -m vidstab -i input_video.mov -o stable_video.avi -k GFTT
Using VidStab
class
from vidstab import VidStab
# Using defaults
stabilizer = VidStab()
stabilizer.stabilize(input_path='input_video.mov', output_path='stable_video.avi')
# Using a specific keypoint detector
stabilizer = VidStab(kp_method='ORB')
stabilizer.stabilize(input_path='input_video.mp4', output_path='stable_video.avi')
# Using a specific keypoint detector and customizing keypoint parameters
stabilizer = VidStab(kp_method='FAST', threshold=42, nonmaxSuppression=False)
stabilizer.stabilize(input_path='input_video.mov', output_path='stable_video.avi')
Plotting frame to frame transformations
from vidstab import VidStab
import matplotlib.pyplot as plt
stabilizer = VidStab()
stabilizer.stabilize(input_path='input_video.mov', output_path='stable_video.avi')
stabilizer.plot_trajectory()
plt.show()
stabilizer.plot_transforms()
plt.show()
Trajectories | Transforms |
---|---|
Using borders
from vidstab import VidStab
stabilizer = VidStab()
# black borders
stabilizer.stabilize(input_path='input_video.mov',
output_path='stable_video.avi',
border_type='black')
stabilizer.stabilize(input_path='input_video.mov',
output_path='wide_stable_video.avi',
border_type='black',
border_size=100)
stabilizer.stabilize(input_path='input_video.mov',
output_path='wide_stable_video.avi',
border_type='black',
border_size='auto')
# filled in borders
stabilizer.stabilize(input_path='input_video.mov',
output_path='ref_stable_video.avi',
border_type='reflect')
stabilizer.stabilize(input_path='input_video.mov',
output_path='rep_stable_video.avi',
border_type='replicate')
|
|
|
|
border_type='reflect' |
border_type='replicate' |
---|---|
Video used with permission from HappyLiving
Using Frame Layering
from vidstab import VidStab, layer_overlay, layer_blend
# init vid stabilizer
stabilizer = VidStab()
# use vidstab.layer_overlay for generating a trail effect
stabilizer.stabilize(input_path=input_vid,
output_path='trail_stable_video.avi',
border_type='black',
border_size=100,
layer_func=layer_overlay)
# create custom overlay function
# here we use vidstab.layer_blend with custom alpha
# layer_blend will generate a fading trail effect with some motion blur
def layer_custom(foreground, background):
return layer_blend(foreground, background, foreground_alpha=.8)
# use custom overlay function
stabilizer.stabilize(input_path=input_vid,
output_path='blend_stable_video.avi',
border_type='black',
border_size=100,
layer_func=layer_custom)
layer_func=vidstab.layer_overlay |
layer_func=vidstab.layer_blend |
---|---|
Video used with permission from HappyLiving
Working with live video
The VidStab
class can also process live video streams. The underlying video reader is cv2.VideoCapture
(documentation).
The relevant snippet from the documentation for stabilizing live video is:
Its argument can be either the device index or the name of a video file. Device index is just the number to specify which camera. Normally one camera will be connected (as in my case). So I simply pass 0 (or -1). You can select the second camera by passing 1 and so on.
The input_path
argument of the VidStab.stabilize
method can accept integers that will be passed directly to cv2.VideoCapture
as a device index. You can also pass a device index to the --input
argument for command line usage.
One notable difference between live feeds and video files is that webcam footage does not have a definite end point.
The options for ending a live video stabilization are to set the max length using the max_frames
argument or to manually stop the process by pressing the Esc key or the Q key.
If max_frames
is not provided then no progress bar can be displayed for live video stabilization processes.
Example
from vidstab import VidStab
stabilizer = VidStab()
stabilizer.stabilize(input_path=0,
output_path='stable_webcam.avi',
max_frames=1000,
playback=True)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for vidstab-1.5.4-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2e07fc8a116be102e473b65f72e06907132fcf496bce9b25b2f8ba37ff5549b3 |
|
MD5 | 38a79a1ae1ccb5762210ce27ead2d972 |
|
BLAKE2b-256 | badc69b38086c11150590599a15b9e09290017bf51f31157755c139cdc1542a3 |