-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetiminof2.m
342 lines (287 loc) · 10.1 KB
/
getiminof2.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
%% Get Normalized Image of first Hologram
tic
dock
%% Setup Constants
%
% ext = '.tiff';
z1 = -7.3E-3;
z2 = -2.5E-3;
zstepsize = 5E-6;
zsteps = 1+(z2-z1)/zstepsize;
% lambda = 632.8E-9;
% refractindex = 1.33;
% ps = 5.5E-6;
% mag = 4;
zpad_xy = 200;
useHOLOnum = 102;
createbackgroundflag = true;
backgroundfilerangeflag = true;
backgroundfilerange = [2:202];
% cropflag = true; bottom = 2048; top = 1;
% latecropflag = true;
latecropbox = [256 256 1023 1023];
% pauseflag = false;
% maskflag = true;
% mask = 1;
% % mask = mask;
% vortflag = true;
% vortloc = NaN;
% vortimg = NaN;
% iminflag = true;
% bringtomeanflag = false;
% maskfile = nan;
% fignum = 4321;
%
% z0 = 0;
% z3 = 0;
% z4 = 0;
% % derstr = 0;
% % thparam = 0.4;
% % thlevel = 0;
%
%
% % Constants to save
% namesofconstants = {'latecropflag','latecropbox','iminflag','toporbottom','pauseflag','wouldyouliketocrop','croparea','earlycropregion','ext','z0','z1','z2','z3','z4','zsteps','zstepsize','lambda','refractindex','ps','mag','mask','Imin','zmap','HOLO','HOLOBGR','useHOLOnum','backgroundfilerangeflag','backgroundfilerange','cropflag','bottom','top','zpad','background','maskflag','masktype','maskfile','vortflag','vortloc','vortimg','bringtomeanflag','rect_xydxdy'};
% [~,nocorder] = sort(lower(namesofconstants));
% namesofconstants = namesofconstants(nocorder);
%
%% Import Hologram
%
while useHOLOnum~=round(useHOLOnum) || useHOLOnum < 1 || useHOLOnum > 9999
useHOLOnum = input('Invalid Hologram file number. Choose an integer between 1 and 9999: ');
end
useHOLOnumstr = num2str(useHOLOnum,'%04.0f');
filesort = dir(['*',ext]);
numfiles = numel(filesort);
HOLO0001 = (imread(filesort(useHOLOnum).name));
% rect_xydxdy = [1 1 size(HOLO0001)-1];
% croparea = length(HOLO0001)+1;
% wouldyouliketocrop = true;
% wouldyouliketocropinput = input('Would you like to crop a region of interest? (y/n): ','s');
% switch upper(wouldyouliketocropinput)
% case 'N'
% wouldyouliketocrop = false;
% case 'Y'
% wouldyouliketocrop = true;
% figure(fignum)
% otherwise
% rect_xydxdy = str2num(wouldyouliketocropinput);
% wouldyouliketocrop = false;
% end
% while wouldyouliketocrop == true && rect_xydxdy(4) < croparea-1
% [~, rect_xydxdy] = imcrop(HOLO0001); rect_xydxdy = ceil(rect_xydxdy)
%
% croparea = input('How big do you want the cropped region (2048 default): ');
% if isempty(croparea);
% croparea = 2048;
% end
% toporbottom = input('Do you want to keep top or bottom? ','s');
% switch upper(toporbottom)
%
% case 'BOTTOM'
% top = rect_xydxdy(2)+rect_xydxdy(4)-croparea;
% left = round((rect_xydxdy(1)+rect_xydxdy(3)/2)-croparea/2);
% bottom = -1+rect_xydxdy(2)+rect_xydxdy(4);
% rect_xydxdy = [left,top,croparea-1,croparea-1];
%
% case 'TOP'
% top = rect_xydxdy(2);
% left = round((rect_xydxdy(1)+rect_xydxdy(3)/2)-croparea/2);
% bottom = -1+rect_xydxdy(2)+croparea;
% rect_xydxdy = [left,top,croparea-1,croparea-1];
%
% otherwise
% error(['Unexpected option: ' toporbottom])
% end
%
% end
HOLO0001 = imcrop(HOLO0001,rect_xydxdy);
vortflag = true;
vortimg(1).img = [];
vortidx = 0;
while vortflag == true;
vortidx = vortidx + 1;
vortflag = input('Is there a(nother) Vorticella object? (y/n) ','s');
switch upper(vortflag)
case 'Y'
vortflag = true;
figure;
% [~, vortloc(vortidx,1:4)] = imcrop(HOLO0001);
% vortloc(vortidx,1:4) = ceil(vortloc(vortidx,1:4));
vortlocRel(vortidx,1:4) = vortloc(vortidx,1:4);
vortimg(vortidx).img = double(HOLO0001(vortlocRel(vortidx,2):vortlocRel(vortidx,2)+vortlocRel(vortidx,4)-1,vortlocRel(vortidx,1):vortlocRel(vortidx,1)+vortlocRel(vortidx,3)-1));
% vortloc(vortidx,1) = vortloc(vortidx,1) + left - 1;
% vortloc(vortidx,2) = vortloc(vortidx,2) + top - 1;
case 'N'
vortflag = false;
otherwise
error(['Unexpected option: ' vortflag])
end
end
HOLO0001 = double(HOLO0001);
%% Create Mask
%
masktype = 'OPEN'; %default value - open means there is no mask.
zpad = 2*zpad_xy + length(HOLO0001);
maskflag = input('Apply a Fourier Tansform aperture mask? (y/n) ','s');
switch upper(maskflag)
case 'Y'
maskflag = true;
masktype = input('Which mask do you want to use? ("KNIFE", "OPEN", "FILE", "VAR", etc.) ','s');
switch upper(masktype)
case 'FILE'
maskfile = input('File name: ','s');
mask = makemask(zpad, masktype, maskfile);
case 'VAR'
maskfile = input('Local variable name: ','s');
mask = makemask(zpad, masktype, eval(maskfile));
case 'OPEN'
mask = 1;
varargin(1:2) = [];
otherwise
mask = makemask(zpad, masktype);
% mask = imresize(mask,[zpad,zpad],'nearest');
end
case 'N'
maskflag = false;
otherwise
error(['Unexpected option: ' maskflag])
end
%% Create Background
%
if createbackgroundflag == true && backgroundfilerangeflag == false && exist('.\background.mat', 'file') > 0
overwritebackground = input('"background.mat" already exists. Are you sure you want to overwrite it? (y/n) ','s');
switch upper(overwritebackground)
case 'Y'
createbackgroundflag = true;
case 'N'
createbackgroundflag = false;
otherwise
error(['Unexpected option: ' overwritebackground])
end
end
if createbackgroundflag == true;
if backgroundfilerangeflag == true
background=avgbg('filename',['*',ext],'output',['background',num2str(backgroundfilerange(1)),'to',num2str(backgroundfilerange(end))],'filerange',backgroundfilerange);
else
background=avgbg('filename',['*',ext],'output','background');
end
background = imcrop(background,rect_xydxdy);
else
try
load('background.mat');
background = imcrop(background,rect_xydxdy);
catch
background = 1;
end
% varnam = who('-file','background.mat');
% background = load('background.mat',varnam{1});
% background = background.(varnam{1});
end
HOLO0001BG = HOLO0001;
HOLO0001 = HOLO0001./background;
if vortflag == true;
[m2,~] = size(vortlocRel);
for L2 = 1:m2
vortimg(L2).img = vortimg(L2).img/mean(background(:));
HOLO0001(vortlocRel(L2,2):vortlocRel(L2,2)+vortlocRel(L2,4)-1,vortlocRel(L2,1):vortlocRel(L2,1)+vortlocRel(L2,3)-1) = mean(HOLO0001(:));
end
end
HOLO0001nozeros = HOLO0001; HOLO0001nozeros(HOLO0001nozeros==0)=NaN;
if bringtomeanflag == true
HOLO0001(HOLO0001 > 2*nanmean(HOLO0001nozeros(:))) = nanmean(HOLO0001nozeros(:));
HOLO0001(isnan(HOLO0001)) = nanmean(HOLO0001nozeros(:));
else
HOLO0001(HOLO0001 > 2*nanmean(HOLO0001nozeros(:))) = 2*nanmean(HOLO0001nozeros(:));
HOLO0001(isnan(HOLO0001)) = 0;
end
%% test zmax and zmin
%
testzmflag = true;
bigzsteps = 1+round((-1+zsteps)/40);
if bigzsteps < 15
bigzsteps = 30;
end
% imagepropagain = input('Do you want to step through z? (y/n): ','s');
% switch upper(imagepropagain)
% case 'N'
% testzmflag = false;
% end
%
% while testzmflag == true;
% figure(7312)
% imageprop(HOLO0001,lambda/refractindex,linspace(z1,z2,bigzsteps),ps/mag,'mask',mask,'zpad',zpad,'real','imcrop',[256 256 1023 1023],'pause',1);
% imagepropagain = input('Do you want to run the video again? (y/n): ','s');
% switch upper(imagepropagain)
% case 'Y'
% testzmflag = true;
% lastz1 = z1;
% lastz2 = z2;
% z1 = input(['New z1(',num2str(lastz1),'): ']);
% z2 = input(['New z2(',num2str(lastz2),'): ']);
% if isempty(z1);
% z1 = lastz1;
% end
% if isempty(z2);
% z2 = lastz2;
% end
% zsteps = 1+(z2-z1)/zstepsize;
% case 'N'
% testzmflag = false;
% otherwise
% error(['Unexpected option: ' testzmflag])
% end
% end
% if cropflag == true
% HOLO0001 = imcrop(HOLO0001,[top,top,bottom-top,bottom-top]);
% end
%% Plot and Save HOLO
%
HOLO4Imin = HOLO0001;
if latecropflag == true;
HOLO0001 = imcrop(HOLO0001,latecropbox);
HOLO0001BG = imcrop(HOLO0001BG,latecropbox);
background = imcrop(background,latecropbox);
end
figure
imagesc(HOLO0001); colormap gray; colorbar; axis image; axis ij;title([useHOLOnumstr,pwd])
figure
imagesc(HOLO0001BG); colormap gray; colorbar; axis image; axis ij;title([useHOLOnumstr,pwd])
figure
imagesc(background); colormap gray; colorbar; axis image; axis ij;title([useHOLOnumstr,pwd])
if pauseflag == true;
pause;
end
HOLO0001norm = (HOLO0001 - min(HOLO0001(:)))./(max(HOLO0001(:)) - min(HOLO0001(:)));
HOLO0001BGnorm = (HOLO0001BG - min(HOLO0001BG(:)))./(max(HOLO0001BG(:)) - min(HOLO0001BG(:)));
imwrite(uint8(HOLO0001BGnorm*255), ['HOLO',useHOLOnumstr,'.png']);
imwrite(uint8(HOLO0001norm*255), ['HOLOBGR',useHOLOnumstr,'.png']);
%% Get Imin of Normalized Image of first Hologram
%
if iminflag == false
return
else
[Imin0001, zmap0001] = imin((HOLO4Imin),lambda/refractindex,linspace(z1,z2,zsteps),ps/mag,'mask',mask,'zpad',zpad);
if latecropflag == true;
Imin0001 = imcrop(Imin0001,latecropbox);
zmap0001 = imcrop(zmap0001,latecropbox);
end
end
%% Save Constants
%
HOLO = HOLO0001BG;
HOLOBGR = HOLO0001;
Imin = Imin0001;
zmap = zmap0001;
save(['iminSingle',useHOLOnumstr,'constants.mat'],namesofconstants{:},'namesofconstants');
imwrite(uint8(background), ['background',num2str(backgroundfilerange(1)),'to',num2str(backgroundfilerange(end)),'.png']);
%% Plot and Save Imin
%
figure
imagesc(Imin0001); colormap gray; colorbar; axis image; axis ij;title(pwd)
Imin0001norm = (Imin0001 - min(Imin0001(:)))./(max(Imin0001(:)) - min(Imin0001(:)));
imwrite(uint8(Imin0001norm*255), ['Imin',useHOLOnumstr,'.png']);
%%
%
toc2
dock