forked from erhanbas/pipeline-stitching
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvisualizaiton_stiching.m
287 lines (241 loc) · 6.7 KB
/
visualizaiton_stiching.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
matfolder = '/nrs/mouselight/cluster/classifierOutputs/2018-07-02/matfiles/'
matfolder = '/data3/visualization_stitching'
load(fullfile(matfolder,'regpts.mat'))
load(fullfile(matfolder,'scopeparams_pertile'),'paireddescriptor', ...
'scopeparams', 'curvemodel','params')
load(fullfile(matfolder,'scopeloc'),'scopeloc','neighbors','experimentfolder','inputfolder')
addpath(genpath('./common'))
checkthese = [1 4 5 7]; % 0 - right - bottom - below
neigs = neighbors(:,checkthese);
% save(fullfile(matfolder,'vizdata.mat'))
targetfold_ = 'visualization_figures';
%%
clear ds
numtiles = length(paireddescriptor{1});
ds = zeros(numtiles,4);
for i=1:numtiles
[ds(i,1:2)] = paireddescriptor{1}{i}.count;
ds(i,3) = size(regpts{i}.X,1);
if regpts{i}.matchrate>0
ds(i,4) = regpts{i}.uni;
end
end
%%
inds=(find(ds(:,1)>100&ds(:,2)>100&ds(:,3)>500))
[ds(inds,:) inds(:)]
val = ds(:,1).*ds(:,2);
[aa,bb] = find(val==max(val));
ineig = aa;
%%
model = @(p,y) p(3) - p(2).*((y-p(1)).^2); % FC model
dims = [1024 1536 251];
idxcent = ineig;
final_out = zeros(2,3);
xy=[];
for iadj = 1:2;
idxadj = neigs(ineig,iadj+1);
if iadj==1
descent = paireddescriptor{1}{ineig}.onx.X+1;
descadj = paireddescriptor{1}{ineig}.onx.Y+1;
else
descent = paireddescriptor{1}{ineig}.ony.X+1;
descadj = paireddescriptor{1}{ineig}.ony.Y+1;
end
descent = util.correctTiles(descent,dims); % flip dimensions
descadj = util.correctTiles(descadj,dims); % flip dimensions
stgshift = 1000*(scopeloc.loc(idxadj,:)-scopeloc.loc(idxcent,:));
pixshift = round(stgshift.*(dims-1)./(params.imsize_um));
% descadj = descadj + ones(size(descadj,1),1)*pixshift; % shift with initial guess based on stage coordinate
outliers = zeros(size(descent,1),1);
iter = 1;
while true
X_ = descent(~outliers,:);
Y_ = descadj(~outliers,:);
% figure,
% myplot3(X_,'o')
% hold on
% myplot3(Y_,'o')
dispvec = X_-Y_;
y = dispvec(:,iadj);
validinds = 1:length(y);
X_ = X_(validinds,:);
Y_ = Y_(validinds,:);
y = y(validinds,:);
x = X_(:,setdiff([1 2],iadj));
dimcent = dims(setdiff([1:2],iadj))/2; % center of image along curvature axis
out = curvemodel{1}(iadj,:,idxcent);
options = optimoptions('fmincon','Display', 'off');
options.ConstraintTolerance = 1e-9;
options.OptimalityTolerance = 1e-9;
fun = @(p) sum((y-feval(model,p,x)).^2);
xyarr{iadj}.x = x;
xyarr{iadj}.y = y;
out2 = fmincon(fun,out,[],[],[],[],[],[],[],options);
yest = feval(model,out2,sort(x));
outliers = abs(y-yest)>=1;
if mean(outliers)<.1
break
end
iter =iter+1;
end
final_out(iadj,:) = out2;
end
%%
for iadj=1:2
x = xyarr{iadj}.x;
y = xyarr{iadj}.y;
if iadj == 1
% close all
figure(305),
clf
plot(y,x,'o','MarkerFaceColor','b')
hold on
% plot(x(outliers),y(outliers),'ro')
% plot(x_range,y_range,'g-')
plot(feval(model,final_out(iadj,:),-10:1500),-10:1500,'r-','LineWidth',4)
axis equal
daspect([1 10 1])
xlim(final_out(iadj,3)+[-10 10])
ylim([-10 1500])
xlabel('\Delta X')
ylabel('Y')
export_fig(fullfile(targetfold_,'deltaX.png'),'-transparent')
else
% close all
figure(306),
clf
plot(x,y,'o','MarkerFaceColor','b')
hold on
% plot(x(outliers),y(outliers),'ro')
% plot(x_range,y_range,'g-')
plot(-10:1100,feval(model,final_out(iadj,:),-10:1100),'r-','LineWidth',4)
axis equal
daspect([10 1 1])
ylim(final_out(iadj,3)+[-10 10])
xlim([-10 1100])
xlabel('\Delta Y')
ylabel('Y')
export_fig(fullfile(targetfold_,'deltaY.png'),'-transparent')
end
end
%%
order = params.order;
xlocs = 1:dims(1);
ylocs = 1:dims(2);
[xy2,xy1] = ndgrid(ylocs(:),xlocs(:));
xy = [xy1(:),xy2(:)];
numTiles = length(scopeloc.filepath);
% xy crop for minimal overlap
[st,ed] = util.getcontolpixlocations(scopeloc,params,scopeparams);
[corrctrlpnttmp_,xlim_cntrl,ylim_cntrl] = util.getCtrlPts(params.imagesize(1),params.imagesize(2),params,st,ed);
% zlimdefaults = [5 25 dims(3)-26 dims(3)-6];
subcorrctrlpnttmp = corrctrlpnttmp_+1;
%%
[locs,xshift2D,yshift2D] = util.fcshift(final_out(:,:),order,xy,dims,[1 1]);
figure(34),
imagesc(xshift2D)
caxis([-4 4])
axis equal tight
colorbar
export_fig(fullfile(targetfold_,'xy_X.png'),'-transparent')
figure(43)
imagesc(yshift2D)
caxis([-2 2])
axis equal tight
colorbar
export_fig(fullfile(targetfold_,'xy_Y.png'),'-transparent')
%%
% nbound = [0 0];
% nbound(1) = max(pixshift(iadj),min(descadj(:,iadj)));
% nbound(2) = min(dims(iadj),max(descent(:,iadj)))+0;
% [X_,Y_] = match.descriptorMatch(X,Y,params.matchparams);
paireddescriptor{1}{idxcent}.onx.X
%%
neigs_ = neighbors(ineig,:); %[2538 2539 2560 2797]
iter = 1;
clear Images
for iii = neigs_([1 4 5 7])
[pt,fi] = fileparts(scopeloc.filepath{iii});
Images{iter} = mytiffread(fullfile(pt,sprintf('%s.0.tif',fi)));
iter = iter + 1;
end
Ic = max(Images{1},[],3);
Ix = max(Images{2},[],3);
Iy = max(Images{3},[],3);
Iz = max(Images{4},[],3);
%%
% save stitching_vix Images ineig neigs_
%%
close all
figure(100),
imshow(Ic,[])
hold on
myplot3(paireddescriptor{2}{ineig}.onx.X+1,'bo'),
myplot3(paireddescriptor{2}{ineig}.ony.X+1,'bo'),
% myplot3(regpts{ineig}.X+1,'bo'),
% export_fig(fullfile(targetfold_,'center.png'))
%%
figure(101),
imshow(Ix,[])
hold on
myplot3(paireddescriptor{2}{ineig}.onx.Y+1,'ro'),
export_fig(fullfile(targetfold_,'Ix.png'))
%%
figure(102),
imshow(Iy,[])
hold on
myplot3(paireddescriptor{2}{ineig}.ony.Y,'ro'),
export_fig(fullfile(targetfold_,'Iy.png'))
%%
figure
imshow(Iz,[])
hold on,
myplot3(regpts{ineig}.Y,'r+')
%%
import // [73579.3, 19407.5, 31032.4]
%%
A = eye(4);
A(1:3,4)=[71067090,15072176,28216712]/1e3;
A(1:3,1:3) = diag([19384.43849206349,19238.116666666665,64652.963541666664])/2^6/1e3
%%
for i=1:2e4
[ds(i,1:2)] = paireddescriptor{2}{i}.count;
end
%%
[bb]=find([ds(:,1).*ds(:,2)]==aa)
%%
ds(bb,:)
%%
ineig = 2538;
figure,
myplot3(paireddescriptor{2}{ineig}.onx.X,'bo'),
hold on,
myplot3(paireddescriptor{2}{ineig}.onx.Y,'r+')
% paireddescriptor{end}{tile_case}.onx
%%
checkthese = [1 4 5 7]; % 0 - right - bottom - below
imsize_um = params.imsize_um;
neigs = neighbors(:,checkthese);%[id -x -y +x +y -z +z] format
%%
%%
x = paireddescriptor{2}{ineig}.onx.X;
y = paireddescriptor{2}{ineig}.onx.Y;
vec = x-y;
model = @(p,y) p(3) - p(2).*((y-p(1)).^2); % FC model
out = curvemodel{end}(1,:,ineig);
figure(304),
% subplot(2,1,iadj)
cla
plot(y,x,'+')
hold on
% plot(y(outliers),x(outliers),'ro')
% plot(y_range,x_range,'g-')
plot(feval(model,out,x),x,'go')
% daspect([1 100 1])
%%
for ii=1:1e5
if scopeloc.filepath{ii} == '/groups/mousebrainmicro/mousebrainmicro/data/acquisition/2018-07-02/2018-07-06/01/01484/01484-ngc.acquisition'
ii
break
end
end