-
Notifications
You must be signed in to change notification settings - Fork 2
/
crctdrift.m
49 lines (43 loc) · 1.79 KB
/
crctdrift.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
function crctdrift(file, toDebug)
% This function expect that the video file entered already has drift
% estimated using estdrift and existing mat file in
% drft_trc\<FILENAME>. The output file will be saved a mat file in the
% drft_crct\<FILENAME> folder.
%
% file is a .AVI file name
%
% toDebug is 0 or 1
%
% Created by SHALIN SHAH (shalin.shah@duke.edu)
% Date created 08/09/2018
frameSize = 512;
% Load life mat file
fileName = strsplit(file, '.');
lifMatFile = matfile(strcat('tmp/mat/', fileName{1}, '.mat'));
fprintf('Loading drift trace file...\n');
if ~exist(strcat('tmp/drft_trc/', fileName{1}, '.mat'), 'file')
fprintf('Drift trace file does not exists. \n');
return
end
% Load drift trace file
driftData = load(strcat('tmp/drft_trc/', fileName{1}, '.mat'));
estDrift = driftData.drift;
figure(); plot(estDrift); legend('X-direction', 'Y-direction')
% Create a new 3D array and correct drift frame-by-frame
fprintf('Loaded drift trace, correcting drift...\n');
dcMatFile = matfile(strcat('tmp/drft_crct/', fileName{1}, '.mat'));
dcMatFile.Properties.Writable = true;
[~, ~, nFrames] = size(lifMatFile,'data');
for iFrame = 1:2:nFrames
A = imtranslate(lifMatFile.data(:,:,iFrame), ...
[-estDrift(iFrame, 1) -estDrift(iFrame, 2)]);
B = imtranslate(lifMatFile.data(:,:,iFrame+1), ...
[-estDrift(iFrame+1, 1) -estDrift(iFrame+1, 2)]);
dcMatFile.data(1:frameSize,1:frameSize,iFrame:iFrame+1) = cat(3, A, B);
end
fprintf('Finished correcting for drift.\n')
if toDebug
% write every 500th frame and save it
mattovid(file, 'drft_crct', 500);
end
end