-
Notifications
You must be signed in to change notification settings - Fork 1
/
rollingbg.m
93 lines (78 loc) · 3.13 KB
/
rollingbg.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
81
82
83
84
85
86
87
88
89
90
91
92
93
function [ background ] = rollingbg( inputFileName, avgNframes, numframesTEST )
%rollingbg.m: Creates a background image for each hologram frame
% Sum N/2 frames before current frame and N/2 frames after current frame
% and divide the result by N. Do this for (NumberOfFrames-N)
% Do not include current frame in this calculation.
%% SHOULD I INCLUDE THE OPTION TO EXCLUDE SOME FRAMES BEFORE AND AFTER CURRENT FRAME?
%%
tic
avgNframesDefault = 400; %for first test sample average somewhere between 200-400 frames (100 leaves streaking of slow particles, 500 makes the mean too different)
OutputPathStr = 'background';
[PathStr, firstname, ext] = fileparts(inputFileName);
firstname = strrep(firstname, '*', '');
if nargin < 2
avgNframes = avgNframesDefault;
elseif mod(avgNframes,2) ~= 0
avgNframes = 2*round(round(avgNframes)/2);
end
gpu_num = gpuUseful; %Determines if there is a CUDA enabled GPU
[filesort,numfiles] = filesortstruct([firstname,'*',ext]);
background = double(imread(filesort(1, 1).name));
[m,n]=size(background);
rect = [1,1,m-1,n-1];
% Do NOT skip frames (unless you want to)
if ~exist('trimframes','var')
trimframes = [1 numfiles 1];
firstframe = trimframes(1);
lastframe = trimframes(2);
skipframes = trimframes(3);
numframes = floor((1+lastframe-firstframe)/skipframes);
end
if nargin > 2
numframes = numframesTEST+avgNframes;
end
if ~exist(['.\', OutputPathStr], 'dir') && ~isempty(['.\', OutputPathStr])
mkdir(OutputPathStr);
else
overwriteflag = input(['Directory named ',OutputPathStr,' already exists. Would you like to overwrite (y/n) '],'s');
if upper(overwriteflag) ~= 'Y'
error(['Background files in ',OutputPathStr,' already exist']);
end
end
if gpu_num > 0;
background=gpuArray(background);
end
wb = waitbar(0,'Creating individual background files');
% Create rolling background for first frame
for loop = 2:(avgNframes/2)
L = loop*skipframes;
background = background + double(imread(filesort(L, 1).name));
waitbar((loop-1)/numframes,wb);
end
for loop = (avgNframes/2+2):(avgNframes+1)
L = loop*skipframes;
background = background + double(imread(filesort(L, 1).name));
waitbar((loop-1)/numframes,wb);
end
% Save rolling background file for first frame
background = gather(background/avgNframes);
save([OutputPathStr,'\',filesort(avgNframes/2+1).firstname,'.mat'],'background','-v7.3');
% Create and Save rolling background file for remaining frames
for loop = avgNframes+2:numframes
background = background*avgNframes;
if gpu_num > 0;
background = gpuArray(background);
end
L = loop*skipframes;
background = background + double(imread(filesort(L-avgNframes/2-1, 1).name));
background = background + double(imread(filesort(L, 1).name));
background = background - double(imread(filesort(L-avgNframes-1, 1).name));
background = background - double(imread(filesort(L-avgNframes/2, 1).name));
background(background<0) = 0;
waitbar((loop-1)/numframes,wb);
background = gather(background/avgNframes);
save([OutputPathStr,'\',filesort(L-avgNframes/2).firstname,'.mat'],'background','-v7.3');
end
close(wb);
toc2
end