This repository has been archived by the owner on Jan 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcalibrate_globalworkflow.m
249 lines (218 loc) · 7.62 KB
/
calibrate_globalworkflow.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
function calibrate_globalworkflow(p)
if ~p.isglobalfit %only normal local calibratin, just call old proram
calibrate3D_g(p);
return
end
global S beadpos1 beadpos2
f=figure('Name','Bead calibration');
tg=uitabgroup(f);
if p.makeT || isempty(p.Tfile)
pr=getranges(p) ;
ph=p;
ph.isglobalfit=false;
%set spatial calibration
ph.outputfile={};
if contains(p.filelist{1},';') %each channel in separate file
for k=1:length(p.filelist)
ind=strfind(p.filelist{k},';');
filelist1{k}=p.filelist{k}(1:ind-1);
filelist2{k}=p.filelist{k}(ind+1:end);
end
else
filelist1=p.filelist; filelist2=p.filelist;
end
t1=uitab(tg,'Title','first channel');
ph.filelist=filelist1;
ph.tabgroup= uitabgroup(t1);
ph.yrange=pr.yrange1;ph.xrange=pr.xrange1;
ph.filechannel=1;
[S1,beadpos1,parameters1]=calibrate3D_g(ph);
t2=uitab(tg,'Title','second channel');
ph.filelist=filelist2;
ph.tabgroup= uitabgroup(t2);
ph.yrange=pr.yrange2;ph.xrange=pr.xrange2;
ph.filechannel=2;
[S2,beadpos2,parameters2]=calibrate3D_g(ph);
% [S,beadpos]=calibrate3D_g(ph);
% Later: also do test-fitting with corresponding spline coefficients
tt=uitab(tg,'Title','transformation');
p.tabgroup= uitabgroup(tt);
% p.separator=p.Tsplitpos+parameters1.roi{1}(pr.roiind);
p.separator=p.Tsplitpos;
% find transform
% if p.makeT || isempty(p.Tfile)
% transform=transform_locs_simple(beadpos1{1},beadpos2{1},p);
transform=makeglobalTransform(beadpos1{1},beadpos2{1},p);
else
l=load(p.Tfile);
transform=l.transformation;
p.Tmode=transform.tinfo.mirror.targetmirror;
p.Tsplitpos=transform.tinfo.separator;
pr=getranges(p) ;
% split= transform.mirror;
end
ph=p;
ph.outputfile=[];
ph.isglobalfit=true;
ph.Tfile=transform;
% ph.outputfile=p.outputfile;
ph.outputfile=[];
t4=uitab(tg,'Title','global cal');
ph.tabgroup= uitabgroup(t4);
% ph.yrange=yrange1;ph.xrange=xrange1;
ph.filelist=p.filelist;
[S,beadpos,parameters_g]=calibrate3D_g(ph);
% if ~exist('S1','var') %take global one apart...
% recover S1, S2 dfrom S_glob to ensure same z reference, and PSF from
% corresponding beads.
S1=S;%XXXXXX also take the right coefficients!!!
S1.PSF=S1.PSF(1);
S1.cspline.coeff={S1.cspline.global.coeffrawref};
S1.cspline.normf=S1.cspline.normf(1);
S1.cspline.mirror=0;
S2=S;
S2.PSF=S2.PSF(2);
S2.cspline.coeff={S2.cspline.global.coeffrawtar};
S2.cspline.normf=S2.cspline.normf(2);
S1.Xrange=pr.xrange1;S2.Xrange=pr.xrange2;
S1.Yrange=pr.yrange1;S2.Yrange=pr.yrange2;
% end
S1.Yrangeall=pr.yrangeall;S1.Xrangeall=pr.xrangeall;
S2.Yrangeall=pr.yrangeall;S2.Xrangeall=pr.xrangeall;
S2.posind=pr.XYpos;
if strcmp(pr.split,'rl')
SXY(1:length(S1),1)=S1;
SXY(end+1:end+length(S2),1)=S2;
else
SXY(1,1:length(S1))=S1;
SXY(1,end+1:end+length(S2))=S2;
end
SXY_g=S;
transformation=parameters_g.transformation;
calibrationfigure=f;
if ~isempty(p.outputfile)
if p.smap
parameters1.smappos.P=[]; parameters2.smappos.P=[]; parameters_g.smappos.P=[];
save(p.outputfile,'SXY','SXY_g','parameters_g','parameters1','parameters2','transformation');
else
save(p.outputfile,'gausscal','cspline_all','gauss_sx2_sy2','gauss_zfit','cspline','parameters');
end
filefig=strrep(p.outputfile,'.mat','.fig');
savefig(calibrationfigure,filefig,'compact');
end
end
function pr=getranges(p)
if ~isfield(p,'yrange')
p.yrange=[-inf inf];
end
if ~isfield(p,'xrange')
p.xrange=[-inf inf];
end
switch p.Tmode
case {'up-down','up-down mirror'}
splitpos=p.Tsplitpos(1);
if max(p.yrange)<splitpos %defined only in upper part
yrange1=p.yrange;
if contains(p.Tmode,'mirror')
yrange2=sort(-p.yrange+2*splitpos);yrange2(yrange2<splitpos)=splitpos;
else
yrange2=sort(p.yrange+splitpos);yrange2(yrange2<splitpos)=splitpos;
end
else
yrange1=([p.yrange splitpos]);yrange1(yrange1>splitpos)=splitpos;yrange1=unique(yrange1);
yrange2=([p.yrange+ splitpos]);yrange2(yrange2<splitpos)=splitpos;yrange2=unique(yrange2);
end
% yrange=unique([p.yrange splitpos p.yrange+splitpos]);
% yrange1=yrange(yrange<=splitpos);yrange2=yrange(yrange>=splitpos);
pr.xrange1=p.xrange;pr.xrange2=p.xrange;
yrangeall=[yrange1(1:end-1) splitpos yrange2(2:end)];
pr.yrangeall=yrangeall;
yrange1(end)=yrange1(end)-p.mindistance; %do not take into account locs too close to separator
yrange2(1)=yrange2(1)+p.mindistance;
pr.yrange1=yrange1;pr.yrange2=yrange2;
pr.xrangeall=p.xrange;
pr.XYpos=[1,2];
pr.split='ud';
pr.roiind=2;
case {'right-left','right-left mirror'}
splitpos=p.Tsplitpos(end);
if max(p.xrange)<splitpos %defined only in upper part
xrange1=p.xrange;
if contains(p.Tmode,'mirror')
xrange2=sort(-p.xrange+2*splitpos);xrange2(xrange2<splitpos)=splitpos;
else
xrange2=sort(p.xrange+splitpos);xrange2(xrange2<splitpos)=splitpos;
end
else
xrange1=([p.xrange splitpos]);xrange1(xrange1>splitpos)=splitpos;xrange1=unique(xrange1);
xrange2=([p.xrange+ splitpos]);xrange2(xrange2<splitpos)=splitpos;xrange2=unique(xrange2);
end
pr.yrange1=p.yrange;pr.yrange2=p.yrange;
xrangeall=[xrange1(1:end-1) splitpos xrange2(2:end)];
pr.xrangeall=xrangeall;
xrange1(end)=xrange1(end)-p.mindistance; %do not take into account locs too close to separator
xrange2(1)=xrange2(1)+p.mindistance;
pr.xrange1=xrange1;
pr.xrange2=xrange2;
pr.yrangeall=p.yrange;
pr.XYpos=[2,1];
pr.split='rl';
pr.roiind=1;
case {'2 cam','2 cam u-d mirror','2 cam r-l mirror'}
pr.xrange1=p.xrange;pr.yrange1=p.yrange;
pr.xrange2=p.xrange;pr.yrange2=p.yrange;
pr.roiind=1;
pr.xrangeall=p.xrange; pr.yrangeall=p.yrange;
pr.XYpos=[1,1];
pr.split ='none';
% if max(p.xrange)<splitpos %defined only in upper part
% xrange1=p.xrange;
% xrange2=p.xrange+splitpos;xrange2(xrange2<splitpos)=splitpos;
% else
% xrange1=([p.xrange splitpos]);xrange1(xrange1>splitpos)=splitpos;xrange1=unique(xrange1);
% xrange2=([p.xrange+ splitpos]);xrange2(xrange2<splitpos)=splitpos;xrange2=unique(xrange2);
% end
% yrange1=p.yrange;yrange2=p.yrange;
% yrangeall=p.yrange;
% xrangeall=[xrange1(1:end-1) splitpos xrange2(2:end)];
% XYpos=[2,1];
% split='rl';
end
end
%
function transform=makeglobalTransform(bead1,bead2,ph)
%calculate transformN
pp=getranges(ph);
transform=interfaces.LocTransformN;
pt.mirror=[false false]; %ref
pt.xrange=pp.xrange1;
pt.yrange=pp.yrange1;
pt.unit='pixel';
pt.type='projective';
transform.setTransform(1,pt)
if contains(pp.split,'rl')
pt.mirror= 1;
elseif contains(pp.split,'ud')
pt.mirror= 2;
else
pt.mirror=0;
end
pt.xrange=pp.xrange2;
pt.yrange=pp.yrange2;
transform.setTransform(2,pt)
th=ph.tabgroup;
ph.ax=th;
numf=5;
mp=round(size(bead1.x,1)+1)/2;
range=mp-numf:mp+numf;
bc1=horzcat(reshape(bead1.x(range,:),[],1),...
reshape(bead1.y(range,:),[],1),...
reshape(bead1.z(range,:),[],1),...
reshape(bead1.filenumber(range,:)*100+bead1.frame(range,:),[],1));
bc2=horzcat(reshape(bead2.x(range,:),[],1),...
reshape(bead2.y(range,:),[],1),...
reshape(bead2.z(range,:),[],1),...
reshape(bead2.filenumber(range,:)*100+bead2.frame(range,:),[],1));
[transform ,iAa,iBa]=transform_locs_simpleN(transform,1, bc1,2,bc2,ph);
end
%