forked from uw-loci/fiber-extraction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fire.m
242 lines (208 loc) · 6.13 KB
/
fire.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
function[data] = fire(p,im,plotflag)
%FIRE(p,im,plotflag)
%
%main fire algorithm p = parameter vector, im3 = 3d image,
%and plotflag = 1 gives lots of plots
if nargin < 3
plotflag = 1;
end
df;
if plotflag==1
rr = 3; cc = 3;
elseif plotflag==2
rr = 1; cc = 2;
else
rr = 1; cc = 1;
end
ifig = 0;
%plot initial figures
if plotflag == 1 || 2
str = 'a' + ifig;
ifig = ifig+1;
subplot(rr,cc,ifig);
flatten(im);
colormap gray
title(sprintf('%c) Flattened Image',str))
pause(0.1)
end
ax = [1 size(im,2) 1 size(im,3)];
%smoothing image
fprintf(' smoothing original image\n');
ims = round(smooth(im,p.sigma_im));
if plotflag == 1;
str = 'a'+ifig;
ifig = ifig+1;
subplot(rr,cc,ifig)
flatten(ims); colormap gray
title(sprintf('%c) Smoothed Image',str))
view(0,90)
axis(ax)
end
%threshold image
if ~isempty(p.thresh_im)
imt = ims>p.thresh_im*max(ims(:));
else
imt = ims>p.thresh_im2;
end
if plotflag == 1
str = 'a'+ifig;
ifig = ifig+1;
subplot(rr,cc,ifig)
flatten(imt*256); colormap gray
title(sprintf('%c) Thresholded Image',str))
view(0,90)
axis(ax)
end
%clear ims
%perform distance transform
fprintf([' calculating ' p.dtype ' distance to background\n'])
d = single(bwdist(~imt,p.dtype));
%clear imt
dsm = single(smooth(d,p.sigma_d));
clear d;
if plotflag==1
str = 'a'+ifig;
ifig = ifig+1;
subplot(rr,cc,ifig)
flatten(dsm); colormap gray
title(sprintf('%c) Smoothed Distance Function',str))
view(0,90)
axis(ax)
end
%find crosslinks
fprintf('finding nucleation points\n ')
xlink = findlocmax(dsm,p.s_xlinkbox,p.thresh_Dxlink);
if plotflag == 1
str = 'a'+ifig;
ifig = ifig+1;
subplot(rr,cc,ifig)
flatten(im);
hold on
plot3(xlink(:,1),xlink(:,2),xlink(:,3),'ro','MarkerFaceColor','r','MarkerSize',4)
view(0,90)
axis(ax)
title(sprintf('%c) Nucleation Points',str))
pause(0.1)
end
xlinkin = xlink;
%find network
fprintf('extending nucleation points\n')
[Xz Fz Vz Rz] = extend_xlink(dsm,round(xlinkin),p);
if plotflag == 1
str = 'a'+ifig;
ifig = ifig+1;
subplot(rr,cc,ifig)
%flatten(dsm);
hold on
%plot3bw(dsm)
plotfiber(Xz,Fz,1,0,'b')
plot3(xlink(:,1),xlink(:,2),xlink(:,3),'ro','MarkerFaceColor','r','MarkerSize',4)
axis(ax)
view(0,-90)
title(sprintf('%c) Prelim. Network',str))
axis image
pause(0.1)
end
%remove danglers and shorties
fprintf('remove danglers and shorties')
[Xz2 Fz2 Vz2 Rz2] = check_danglers(Xz,Fz,Vz,Rz,p);
%identify cross-links
xlinkind = zeros(length(Vz),1);
for vi=1:length(Vz)
if length(Vz(vi).f) > 1
xlinkind(vi) = 1;
end
end
xlinknew = Xz(xlinkind==1,:);
if plotflag == 1
str = 'a'+ifig;
ifig = ifig+1;
subplot(rr,cc,ifig)
hold on
plotfiber(Xz2,Fz2,2,0,'b')
plot3(xlinknew(:,1),xlinknew(:,2),xlinknew(:,3),'ro','MarkerFaceColor','r')
axis(ax)
axis image
view(0,-90)
title(sprintf('%c) Danglers Removed',str))
pause(0.1)
end
%return final values
X = Xz2;
F = Fz2;
V = Fz2;
R = Rz2;
1;
%fiberize network
fprintf('fiberproc\n');
[Xa Fa Ea Va Ra] = fiberproc(X,F,R,size(dsm),p);
%maketext(mfn,Xa,Fa)
if plotflag == 1 || plotflag == 2
str = 'a'+ifig;
ifig = ifig+1;
subplot(rr,cc,ifig)
%flatten(im3(p.zstart:p.zstop,:,:));
plotfiber(Xa,Fa,2,0,[]); axis image
title(sprintf('%c) Fiber Network',str))
set(gca,'XLim',[min(Xa(:,1)) max(Xa(:,1))],...
'YLim',[min(Xa(:,2)) max(Xa(:,2))]);
%cet(gca,'Color','k')
view(0,-90);
pause(0.1)
end
%plot full image (as opposed to slice as done earlier, in fire)
%{
if plotflag==1 && length(p.zrange)>2
str = 'a'+ifig;
ifig = ifig+1;
subplot(rr,cc,ifig)
flatten(im3)
title(sprintf('%c) Full Flattened Image',str))
end
%}
%compute network stats
Xas = zeros(size(Xa));
for k=1:size(Xa,1)
Xas(k,:) = Xa(k,:).*p.scale;
end
M = network_stat(Xas,Fa,Va,Ra);
%convert to beams for FEA
fprintf('beamproc\n');
[Xab Fab Vab] = beamproc(Xa,Fa,Va,Ra,p);
%{
if plotflag == 1
str = 'a'+ifig;
ifig = ifig+1;
subplot(rr,cc,ifig)
hold on
plotfiber(Xab,Fab,2,0,[])
title(sprintf('%c) Reduced Network',str))
%axis([min(Xab(:,1)) max(Xab(:,1)) min(Xab(:,2)) max(Xab(:,2))]);
view(0,-90)
pause(0.1)
%cet(gca,'Color','k')
axis equal
end
%}
for ii=1:ifig
subplot(rr,cc,ii)
set(gca,'XTick',[],'YTick',[])
end
[Xc Fc Vc] = fiberbreak(Xa,Fa,Va); %breaks fiber up at cross-links
%make output structure
data.X = X;
data.F = F;
data.R = R;
data.Xa= Xa;
data.Fa= Fa;
data.Va= Va;
data.Ea= Ea;
data.Ra= Ra;
data.Xab=Xab;
data.Fab=Fab;
data.Vab=Vab;
data.Xc = Xc;
data.Fc = Fc;
data.Vc = Vc;
data.M = M;
data.xlink = xlink;