This repository has been archived by the owner on Dec 13, 2017. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
compensate.m
56 lines (52 loc) · 1.69 KB
/
compensate.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
%% Motion Compensation due to GMV
function [sumxs, sumys, an_fs, new_fs] = ...
compensate(movie, start_frame, num_frames, hx, hy, mvmad)
% input parameters
% movie: original frame sequence
% start_frame: start of frame
% num_frames: number of frames
% hx, hy: GMVs
% mvmad: minimal motion MAD
%% initialize
h = movie.Height;
w = movie.Width;
% new stabilized frame
new_fs = zeros(h, w, 3, num_frames-1);
% AGMV (absolute global motion vector)
sumx = 0;
sumy = 0;
sumxs = zeros(num_frames-1);
sumys = sumxs;
% frames of interest
fs = read(movie, [start_frame, start_frame+num_frames]);
%% sum of each frame's MAD to determine whether jiggling or panning
smad = sum(sum(mvmad));
smad = smad(:);
smad = smad(1:num_frames-1);
%% Generate new frame backwards due to AGMV(absolute global motion vector)
for index=1:num_frames-1
an_f = fs(:,:,:,index+1);
% accumulate each frame's GMV to get AGMV
sumx = sumx + hx(index);
sumy = sumy + hy(index);
if (index > 1 && index < num_frames-1)
if ((smad(index)/(smad(index-1)+eps) > 3) && ...
(smad(index)/smad(index+1)+eps) > 3)
% shake is caused by panning
sumy = 0;
sumx = 0;
end
end
% compensate the motion with AGMV
if (sumy >= 0 && sumx >= 0)
new_fs(1+sumy:h, 1+sumx:w,:,index) = an_f(1:h-sumy, 1:w-sumx,:);
elseif (sumy >= 0 && sumx < 0)
new_fs(1+sumy:h, 1:w+sumx,:,index) = an_f(1:h-sumy, 1-sumx:w,:);
elseif (sumy < 0 && sumx >= 0)
new_fs(1:h+sumy, 1+sumx:w,:,index) = an_f(1-sumy:h, 1:w-sumx,:);
else
new_fs(1:h+sumy, 1:w+sumx,:,index) = an_f(1-sumy:h, 1-sumx:w,:);
end
end
an_fs = fs(:,:,:,1:num_frames-1);
end