This repository was archived by the owner on Dec 13, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathstabilize.m
More file actions
70 lines (64 loc) · 2.14 KB
/
stabilize.m
File metadata and controls
70 lines (64 loc) · 2.14 KB
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
%% main function for video stabilization
function stabilize(read_video_path, write_video_path, ...
start_frame, num_frames, range, accuracy, block_size)
% input parameters
% read_video_path: path to the original video
% write_video_path: path to the stabilized video
% start_frame: frame index to start with
% num_frames: number of frames
% range: motion search range
% accuracy: search step
% block_size: match block size (block is square)
tic
movie = VideoReader(read_video_path);
%% calculate block motion vectors for all frames
disp('start to calculate block motion vectors')
mvfs = EBMA(movie, range, accuracy, start_frame, num_frames, block_size);
toc
% save mvfs.mat -struct mvfs;
mvx = mvfs.mvx;
mvy = mvfs.mvy;
mad = mvfs.mad;
mvmad = mvfs.mvmad;
disp('block motion vectors calculation done')
%% show motion vector between two frames
% frame_idx = 5;
% visualise_BMV(movie, mvx(:,:,frame_idx), mvy(:,:,frame_idx), ...
% frame_idx+start_frame-1, block_size);
%% Deal with each frame
% initialize
fhx = zeros(num_frames-1); fhy = fhx; fc = fhx;
bhx = fhx; bhy = fhx; bc = fhx;
for i=1:num_frames-1
% split block motion vectors into foreground and background part
[f_mvx, f_mvy, b_mvx, b_mvy] = splitmv(mvx(:,:,i), mvy(:,:,i), range);
% get global motion vector for each frame
[fhx(i), fhy(i), fc(i)] = GMV(f_mvx, f_mvy, range, mad(:,:,i));
[bhx(i), bhy(i), bc(i)] = GMV(b_mvx, b_mvy, range, mad(:,:,i));
end
toc
%% Motion compensation
% initialize
h = movie.Height;
w = movie.Width;
[sumxs, sumys, an_fs, new_fs] = ...
compensate(movie, start_frame, num_frames, fhx, fhy, mvmad);
to_f_h = h + 20;
to_f_w = w * 2 + 30;
to_f = zeros(to_f_h, to_f_w, 3);
aviwriter = VideoWriter(write_video_path, 'MPEG-4');
open(aviwriter);
for i=1:num_frames-1
to_f(11:10+h,11:10+w,:) = an_fs(:,:,:,i);
to_f(11:10+h,21+w:20+2*w,:) = new_fs(:,:,:,i);
to_f(floor(to_f_h/2),:,:) = 0;
to_f(floor(to_f_h/2),:,2) = 255;
to_f(:, floor(to_f_w/4),:) = 0;
to_f(:, floor(to_f_w/4),2) = 255;
to_f(:, floor(to_f_w/4*3),:) = 0;
to_f(:, floor(to_f_w/4*3),2) = 255;
writeVideo(aviwriter, uint8(to_f(:,:,:)));
end
toc
close(aviwriter);
end