forked from alex-golts/Video-Stabilization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstabilize_camera.m
80 lines (63 loc) · 2.35 KB
/
stabilize_camera.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
% Digital Video Stabilization and Rolling Shutter Correction using Gyroscopes
% Copyright (C) 2011 Alexandre Karpenko
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
%% extract data from captured video & gyro
clear all;
video_file = 'dolly2';
[gyro frame_time] = import_video_data(video_file);
run ../vlfeat-0.9.9/toolbox/vl_setup.m
warning('off', 'Images:initSize:adjustingMag');
%% meshwarp
clear meshwarpmex
load(['mat/camera_param_shake2.mat'], 'cp');
fl = cp(1);
td = cp(2);
ts = cp(3);
g = gyro(:,1:3);
sigma2 = 4000;
gauss = exp(-(-120:120).^2 / sigma2);
gauss = gauss ./ sum(gauss);
g(:,1) = g(:,1) - conv(gyro(:,1), gauss, 'same');
g(:,2) = g(:,2) - conv(gyro(:,2), gauss, 'same');
g(:,3) = g(:,3) - conv(gyro(:,3), gauss, 'same');
dgt = diff(gyro(:,4));
theta = ((g(1:end-1,:) + g(2:end,:)) / 2) .* dgt(:,[1 1 1]);
theta = [0 0 0; cumsum(theta, 1)];
dth = diff(interp1(gyro(:,4) - td, theta, frame_time, 'linear', 'extrap'));
theta = ((gyro(1:end-1,1:3) + gyro(2:end,1:3)) / 2) .* dgt(:,[1 1 1]);
theta = [0 0 0; cumsum(theta, 1)];
% read corresponding movie
xyloObj = mmreader(['data/' video_file '.mov']);
display(xyloObj);
num_frames = xyloObj.NumberOfFrames;
vid_height = xyloObj.Height;
vid_width = xyloObj.Width;
crop_amount = 50;
frame = read(xyloObj, 1);
outvid = VideoWriter(['mat/' video_file '_meshwarp_large.avi']);
open(outvid);
writeVideo(outvid, cropim(frame, crop_amount, crop_amount));
% Read one frame at a time.
for f = 1:num_frames-1
frame = read(xyloObj, f+1);
% imshow(warped);
% pause(0.1);
stabilized = meshwarp(frame, dth(f,:), theta, gyro(:,4), frame_time(f+1), td, ts, fl);
% imshow(stabilized);
% pause(0.1);
writeVideo(outvid, cropim(stabilized, crop_amount, crop_amount));
display([num2str(f+1) ' of ' num2str(num_frames)]);
end
close(outvid);