-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathremovebg.m
125 lines (104 loc) · 3.51 KB
/
removebg.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
function [ Holo ] = removebg( BgFileName, varargin )
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
%% Set Defaults and detect GPU and initial image size
tic
% avgNframesDefault = 401; %for first test sample average somewhere between 200-400 frames (100 leaves streaking of slow particles, 500 makes the mean too different)
BgPathStr = 'background\';
OutputPathStr = 'holofiles\';
% HoloExt = '.tiff';
BgExt = '.mat';
[~, firstname, HoloExt] = fileparts(BgFileName);
firstname = strrep(firstname, '*', '');
skipframes = 1;
firstframe = 1;
testflag = false;
nanflag = false;
try
gpu_num = gpuDeviceCount; %Determines if there is a CUDA enabled GPU
catch err
gpu_num = 0;
end
filesort = dir([BgPathStr,firstname,'*',BgExt]);
numfiles = numel(filesort);
lastframe = numfiles;
for La = 1:numfiles
[filesort(La).pathstr, filesort(La).firstname, filesort(La).ext] = ...
fileparts([filesort(La).name]);
%filesort(i).matname=strcat(filesort(L).matname,'.mat');
end
% Import Background file
backgroundfile = [BgPathStr,filesort(1, 1).name];
varnam = who('-file',backgroundfile);
background = load([BgPathStr,filesort(1, 1).name],varnam{1});
background = background.(varnam{1});
[m,n] = size(background);
cropregion = [1,1,m-1,n-1];
while ~isempty(varargin)
switch upper(varargin{1})
case 'TEST'
testflag = true;
numframesTEST = varargin{2};
varargin(1:2) = [];
case {'CROP', 'IMCROP'}
cropregion = varargin{2};
varargin(1:2) = [];
case 'FIRSTFRAME'
firstframe = varargin{2};
varargin(1:2) = [];
case 'SKIPFRAMES'
skipframes = varargin{2};
varargin(1:2) = [];
case 'LASTFRAME'
lastframe = varargin{2};
varargin(1:2) = [];
case 'NANINF'
nanflag = true;
varargin(1) = [];
otherwise
error(['Unexpected option: ' varargin{1}])
end
end
numframes = floor((1+lastframe-firstframe)/skipframes);
if testflag == true;
numframes = numframesTEST;
lastframe = (numframes-1)*skipframes+firstframe;
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(['Hologram files in ',OutputPathStr,' already exist']);
end
end
wb = waitbar(0,'Removing background');
for La = firstframe:skipframes:lastframe
background = load([BgPathStr,filesort(La, 1).name],varnam{1});
background = background.(varnam{1});
% if gpu_num > 0;
% background=gpuArray(background);
% end
HoloIn = single(imread([filesort(La, 1).firstname,HoloExt]));
maxval = max([HoloIn(:);background(:)]);
Holo = HoloIn./background;
Holo = imcrop(Holo,cropregion);
if nanflag == true
Holo(isnan(Holo)) = 0;
HoloTemp = Holo;
% HoloTemp(isinf(Holo)) = 0;
HoloTemp(Holo>maxval) = maxval;
for Lb = 1:2
replaceInf = mean(HoloTemp(:));
% HoloTemp(isinf(Holo)) = 4*replaceInf;
HoloTemp(Holo>4*replaceInf) = 4*replaceInf;
end
Holo = HoloTemp;
end
% Holo = gather(Holo);
save([OutputPathStr,filesort(La).firstname,'.mat'],'Holo','-v7.3');
waitbar((La-firstframe+1)/numframes,wb);
end
close(wb);
toc2
end