-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathassemble_EK_TifFiles.m
333 lines (313 loc) · 10.6 KB
/
assemble_EK_TifFiles.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
%% ASSEMBLE EK TIF FILES
% WRITTEN ON 11/1/2020 by ELIJAH R SHELTON
% Based on ASSEMBLE TIMELAPSE TIF FILES Written by Elijah R Shelton on July 28 2020 & Modified by Elijah R Shelton on September 2 2020
%
% AIM: The purpose of this script is to prepare tif z stacks from the
% original multichannel files.
% The tif sequences should be ready to be reconstructed using seg3D
% Additionally, MIP tif sequences will also be prepared for reconstruction
% with seg2D, or simply visualization.
close all
clear
clc
%% STEP 1:
% SPECIFY PATHS TO TIFS TO LOAD and TIFS TO SAVE
[paths2TifFiles,paths2Crop3DTifs,paths2MIPZTifs,paths2CropInfo] = getPaths2Tifs();
%% STEP 2:
% LOOP THROUGH EACH PATH
numOfTifs = numel(paths2TifFiles);
for n = 1:numOfTifs
%% STEP 2a.
% GET IMAGES AND IMFINFO PREVIEW
tic
[im_stack_ch1,im_stack_ch2,im_info] = getImagesAndInfo(paths2TifFiles{n},'full');
toc
%% STEP 2b.
% DETERMINE WHICH CHANNEL CONTAINS DROP
[im_dropCh,im_otherCh,dropChIndex] = specifyDropChannel(im_stack_ch1,im_stack_ch2);
%% STEP 2c.
% CLEAN UP DROPLET CHANNEL
[im_dropCh_clean] = cleanUpDropChannel(im_dropCh,im_otherCh);
%% STEP 2d.
% CREATE MIP
im_dropCh_clean_MIP = max(im_dropCh_clean,[],3);
%% STEP 2e.
% CROP 3D
[im_drop_crop,cropInfo] = crop3D(im_dropCh_clean,im_dropCh_clean_MIP);
%% STEP 2g.
% SAVE 3D CROP AS TIFF STACKS (W/ META DATA)
path2Crop3DTif = paths2Crop3DTifs{n};
saveTif(im_drop_crop,path2Crop3DTif,im_info);
% SAVE MIP_Z AS TIFF STACKS (W/ META DATA)
path2MIPZTif = paths2MIPZTifs{n};
saveTif(im_dropCh_clean_MIP,path2MIPZTif,im_info);
%% SAVE CROP INFO
path2CropInfo = paths2CropInfo{n};
saveCropInfo(path2CropInfo,cropInfo)
end
% SUPPORT FUNCTIONS
function[] = saveCropInfo(path2CropInfo,cropInfo)
rowMin = cropInfo.rMin;
rowMax = cropInfo.rMax;
colMin = cropInfo.cMin;
colMax = cropInfo.cMax;
save(path2CropInfo,'rowMin','rowMax','colMin','colMax');
end
function[] = saveTif(im,path2Tif,im_info)
[~,~,depth] = size(im);
Resolution = [im_info(1).XResolution, im_info(1).YResolution];
ImageDescription = im_info(1).ImageDescription;
imwrite(im(:,:,1),path2Tif,'Resolution',Resolution);
tic
for n = 2:depth
imwrite(im(:,:,n),path2Tif,'Resolution',Resolution,...
'WriteMode','append');
elapsedTime = toc;
if elapsedTime > 0.5
tic
fprintf('Saving %d of %d\n',n,depth)
end
end
t = Tiff(path2Tif,'r+');
setTag(t,'ImageDescription',ImageDescription);
close(t)
end
function[Im_crop,cropInfo] = crop3D(Im,Im_mip)
%[rows,cols] = size(Im_mip);
fprintf('Draw rectangle to CROP!\n')
figure(1)
imshow(Im_mip,[]);
polyrect = drawrectangle();
col = polyrect.Position(1);
row = polyrect.Position(2);
width = polyrect.Position(3);
height = polyrect.Position(4);
%Im_crop = Im_mip(row:row+height,col:col+width);
rMin = row;
cMin = col;
rMax = row+height;
cMax = col+width;
cropInfo.rMin = rMin;
cropInfo.cMin = cMin;
cropInfo.rMax = rMax;
cropInfo.cMax = cMax;
Im_crop = Im(rMin:rMax,cMin:cMax,:);
depth = size(Im_crop,3);
z_slice = round(depth/2);
imshow(Im_crop(:,:,z_slice),[])
%
% Im_thresh = median(Im_crop(:));
% [a,b,xCenter,yCenter,~] = getEllipseFromImage(Im_mip,Im_thresh);
% rMin = uint16(xCenter-1.5*max([a,b]));
% cMin = uint16(yCenter-1.5*max([a,b]));
% rMax = uint16(xCenter+1.5*max([a,b]));
% cMax = uint16(yCenter+1.5*max([a,b]));
% rMin = max([1,rMin]);
% cMin = max([1,cMin]);
% rMax = min([rows,rMax]);
% cMax = min([cols,cMax]);
% cropInfo.rMin = rMin;
% cropInfo.cMin = cMin;
% cropInfo.rMax = rMax;
% cropInfo.cMax = cMax;
% Im_crop = Im(rMin:rMax,cMin:cMax,:);
end
function[im_dropCh_clean] = cleanUpDropChannel(im_dropCh,im_otherCh)
numOfFactors = 5;
bleedFactors = logspace(-0.25,0.25,numOfFactors);
depth = size(im_dropCh,3);
z_slices = floor(depth/2) + (-1:2);
im_cat = [];
close all
for n = 1:numOfFactors
bleedFactor = bleedFactors(n);
im_dropCh_clean_z_slices = double(im_dropCh(:,:,z_slices)) - bleedFactor*double(im_otherCh(:,:,z_slices));
im_dropCh_clean_z_slices(im_dropCh_clean_z_slices<0) = 0;
im_dropCh_clean_z_slices = uint16(im_dropCh_clean_z_slices);
im_dropCh_clean_z_slice_4x4bin = binImage3D(im_dropCh_clean_z_slices,4,4);
im_cat = [im_cat,im_dropCh_clean_z_slice_4x4bin];
listString{n} = num2str(n);
fprintf('Processing %d of %d\n',n,numOfFactors);
end
imshow(im_cat,[]);
promptString = 'Select the best image (1-5 from left to right)';
list_rsp = listdlg('PromptString',promptString,'ListString',listString);
bleedFactor = bleedFactors(list_rsp);
im_dropCh_clean = uint16(im_dropCh) - uint16(bleedFactor*double(im_otherCh));
im_dropCh_clean(im_dropCh_clean<0) = 0;
close all
end
function [im3DOut] = binImage3D(im3DIn,binFactorXY,binFactorZ)
[rowsIn,colsIn,pagesIn] = size(im3DIn);
rowsOut = floor(rowsIn/binFactorXY);
colsOut = floor(colsIn/binFactorXY);
pagesOut = floor(pagesIn/binFactorZ);
switch class(im3DIn)
case 'uint8'
im_binned_pages = uint8(zeros(rowsOut,colsOut,binFactorZ));
im3DOut = uint8(zeros(rowsOut,colsOut,pagesOut));
case 'uint16'
im_binned_pages = uint16(zeros(rowsOut,colsOut,binFactorZ));
im3DOut = uint16(zeros(rowsOut,colsOut,pagesOut));
otherwise
error('Did not expect class %s.',class(im3DIn));
end
for p_in = 1:pagesIn
im2DIn = im3DIn(:,:,p_in);
p_mod = mod(p_in-1,binFactorZ)+1;
im_binned2D = binImage2D(im2DIn,binFactorXY);
im_binned_pages(:,:,p_mod) = im_binned2D;
if p_mod==binFactorZ
p_out = floor((p_in-1)./binFactorZ)+1;
im3DOut(:,:,p_out) = sum(im_binned_pages,3)/binFactorZ;
im_binned_pages(:) = 0;
end
end
end
function [imOut] = binImage2D(imIn,binFactor)
[rowsIn,colsIn] = size(imIn);
rowsOut = floor(rowsIn/binFactor);
colsOut = floor(colsIn/binFactor);
switch class(imIn)
case 'uint8'
imOut = uint8(zeros(rowsOut,colsOut));
case 'uint16'
imOut = uint16(zeros(rowsOut,colsOut));
otherwise
error('Did not expect class %s.',class(imIn));
end
for m = 1:rowsOut
for n = 1:colsOut
binRows = binFactor*(m-1) + (1:binFactor);
binCols = binFactor*(n-1) + (1:binFactor);
binInts = imIn(binRows,binCols);
imOut(m,n) = mean(binInts(:));
end
end
end
function[im_dropCh,im_otherCh,dropChIndex] = specifyDropChannel(im_stack_ch1,im_stack_ch2)
figure(1)
im_MIP_ch1 = max(im_stack_ch1,[],3);
im_MIP_ch2 = max(im_stack_ch2,[],3);
im_cat = [im_MIP_ch1,im_MIP_ch2];
imshow(im_cat,[])
ch_select = questdlg('Which image contains the drop?','Choose drop channel','Left', 'Right', 'Left');
switch ch_select
case 'Left'
dropChIndex = 1;
im_dropCh = im_stack_ch1;
im_otherCh = im_stack_ch2;
case 'Right'
dropChIndex = 2;
im_otherCh = im_stack_ch1;
im_dropCh = im_stack_ch2;
otherwise
error('Unexpected value for ch_select')
end
end
function[paths2TifFiles,paths2Crop3DTifs,paths2MIPZTifs,paths2CropInfo,ID_str] = getPaths2Tifs()
mainDir = cd; % starting directory
fprintf('Please locate TIF file\n');
[~,d] = uigetfile([mainDir,'*.tif']);
mainDir = d;
paths2Crop3DDir = [mainDir,'3D Crops', filesep];
if ~exist(paths2Crop3DDir,'dir')
mkdir(paths2Crop3DDir);
end
paths2MIPZDir = [mainDir,'MIPZ', filesep];
if ~exist(paths2MIPZDir,'dir')
mkdir(paths2MIPZDir);
end
files = dir([mainDir,'*.tif']);
numOfTifFiles = numel(files);
paths2TifFiles = cell(numOfTifFiles,1);
paths2Crop3DTifs = cell(numOfTifFiles,1);
paths2MIPZTifs = cell(numOfTifFiles,1);
paths2CropInfo = cell(numOfTifFiles,1);
ID_str = cell(numOfTifFiles,1);
for n = 1:numOfTifFiles
fname = files(n).name;
path2Tif = [mainDir,fname];
paths2TifFiles{n} = path2Tif;
indx = regexp(path2Tif,filesep);
ind1 = indx(end-1)+1;
ind2 = indx(end)-1;
ID_str{n} = [path2Tif(ind1:ind2),'_',fname(1:end-4)];
CROP3D_fname = ['CROP3D_',ID_str{n},'.tif'];
paths2Crop3DTifs{n} = [paths2Crop3DDir,CROP3D_fname];
MIPZ_fname = ['MIPZ_',ID_str{n},'.tif'];
paths2MIPZTifs{n} = [paths2MIPZDir,MIPZ_fname];
cropInfo_fname = ['cropInfo_',ID_str{n},'.mat'];
paths2CropInfo{n} = [paths2Crop3DDir,cropInfo_fname];
end
end
function[im_stack_ch1,im_stack_ch2,im_info] = getImagesAndInfo(path2TifFile,option1)
im_info = imfinfo(path2TifFile);
numOfSlices = numel(im_info);
Height = im_info.Height;
Width = im_info.Width;
numOfCh = 2;
Depth = numOfSlices/numOfCh;
switch option1
case {'full','MIP_Z'}
im_stack_ch1 = zeros(Height,Width,Depth);
im_stack_ch2 = zeros(Height,Width,Depth);
tic
for n = 1:Depth
indx_ch1 = 2*(n-1)+1;
indx_ch2 = 2*n;
im_stack_ch1(:,:,n) = imread(path2TifFile,indx_ch1);
im_stack_ch2(:,:,n) = imread(path2TifFile,indx_ch2);
elapsedTime = toc;
if elapsedTime > 0.5
fprintf('Loading slice %d of %d\n',n,Depth);
tic
end
end
fprintf('Finished %d of %d\n',Depth,Depth);
case 'preview'
n = 1;
indx_ch1 = 2*(n-1)+1;
indx_ch2 = 2*n;
im_stack_ch1 = imread(path2TifFile,indx_ch1);
im_stack_ch2= imread(path2TifFile,indx_ch2);
end
switch option1
case 'MIP_Z'
im_stack_ch1 = max(im_stack_ch1,[],3);
im_stack_ch2 = max(im_stack_ch2,[],3);
end
end
function[a,b,xCenter,yCenter,orientation] = getEllipseFromImage(Im,thresholdValue)
if nargin == 2
%thresholdFactor = 0.2;
%thresholdValue = thresholdFactor*max(Im(:));
V = double(Im > thresholdValue);
else
thresholdValue = 0.25*max(Im(:));
V = double(Im);
V(Im < thresholdValue) = 0;
end
sumV = sum(V(:));
[numRows,numCols] = size(Im);
[X,Y] = meshgrid(1:numRows,1:numCols);
xCenter = sum(X(:).*V(:))/sumV;
yCenter = sum(Y(:).*V(:))/sumV;
XY = [X(:)-xCenter,Y(:)-yCenter].*sqrt([V(:),V(:)]); % intensity weighted centered coordinates
S = 1/(sumV)*(XY'*XY);
[vec,D] = eig(S);
orientation = atan(vec(2)/vec(1))*180/pi;
if orientation < 0
orientation = orientation + 180;
end
a = 2*sqrt(D(1));
b = 2*sqrt(D(4));
xMin = uint16(max([1,xCenter - 1.5*max(a,b)]));
yMin = uint16(max([1,yCenter - 1.5*max(a,b)]));
xMax = uint16(min([numRows,xCenter + 1.5*max(a,b)]));
yMax = uint16(min([numCols,yCenter + 1.5*max(a,b)]));
figure(1)
hold off
imshow(Im(xMin:xMax,yMin:yMax),[]);
h = drawellipse('Center',[xCenter-xMin+1,yCenter-yMin+1],'SemiAxes',[a,b],'RotationAngle',orientation,'StripeColor','r');
end