This project implements video stabilization using OpenCV. It estimates motion between frames and smooths the trajectory using a Kalman filter, generating a stabilized video output with side-by-side comparison.
- Motion estimation via optical flow.
- Affine transformation model (translation + rotation).
- Kalman filtering to smooth the trajectory.
- Side-by-side visualization of original vs. stabilized video.
- Outputs transformation, trajectory, and smoothed data logs for analysis.
- Feature Tracking: Good features are selected and tracked across frames using Lucas-Kanade optical flow.
- Motion Estimation: Affine transform (translation + rotation) is computed between each frame.
- Trajectory Building: The cumulative motion builds a raw trajectory.
- Kalman Filtering: The trajectory is smoothed to reduce jitter.
- Transform Correction: Differences between the raw and smoothed trajectory are used to compute new frame transforms.
- Video Rendering: The corrected frames are shown side-by-side with original ones and saved as a new video (
compare.avi
).
prev_to_cur_transformation.txt
: Raw frame-to-frame transformations.trajectory.txt
: Raw cumulative trajectory.smoothed_trajectory.txt
: Kalman-filtered smooth trajectory.new_prev_to_cur_transformation.txt
: Final stabilized frame transformations.
- Python 3.x
- OpenCV
- NumPy
pip install opencv-python numpy
python rec_stabi.py your_video_file.avi
You will see a window displaying original vs. stabilized frames side-by-side. The stabilized video is saved as compare.avi
.
HORIZONTAL_BORDER_CROP
: Controls cropping to avoid black borders.- Kalman filter parameters:
pstd
: Process noisecstd
: Measurement noise
Original Video | Stabilized Video |
---|---|
Shaky frames | Smooth motion |
(Side-by-side comparison is shown in real time during execution.)
This project is open-source and available under the MIT License.