forked from erhanbas/pipeline-stitching
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreateThumb.m
467 lines (423 loc) · 15.1 KB
/
createThumb.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
function [outputArgs] = createThumb(regpts,scopeparams,scopeloc,videofile)
%DESCRIPTORMATCHQUALITY Summary of this function goes here
%
% [OUTPUTARGS] = DESCRIPTORMATCHQUALITY(INPUTARGS) Explain usage here
%
% Inputs:
%
% Outputs:
%
% Examples:
%
% Provide sample usage code here
%
% See also: List related files here
% $Author: base $ $Date: 2016/11/15 11:13:34 $ $Revision: 0.1 $
% Copyright: HHMI 2016
%%
if nargin<4
videofile = 'myvideo.avi';
videofile='./videos/2017-09-25-heatmap-XYZ'
end
createThumb = 1;
% populate afftile
numTiles = size(regpts,2)
afftile = zeros(3, 4, numTiles);
afftransform = scopeparams.affineglFC;
mflip = -eye(3);%mflip(3,3)=1;
afftransform = afftransform*mflip;
for idxt = 1:numTiles
%%
% form an affine matrix
tform = [afftransform scopeloc.loc(idxt,:)'*1e6];
afftile(:,:,idxt) = tform;
end
%%
[neighbors] = buildNeighbor(scopeloc.gridix(:,1:3)); %[id -x -y +x +y -z +z] format
checkthese = [1 4 5 7]; % 0 - below
D = zeros(1,size(neighbors,1));
for ii=1:size(neighbors,1)
if any(isnan(neighbors(ii,[1 7])))
D(ii) = nan;
continue
end
D(ii)=diff(scopeloc.loc(neighbors(ii,[1 7]),3));
end
%%
checkthis = find(all(neighbors(:,[4 5]),2),1); % both x/y neighbors exist
these = scopeloc.loc(neighbors(checkthis,[1 4 5]),:);
overlap1 = these(1,:)*1e3 + [scopeparams(1).imsize_um(1) 0 0]-these(2,:)*1e3;
overlap2 = these(1,:)*1e3 + [0 scopeparams(1).imsize_um(2) 0]-these(3,:)*1e3;
overlap=[overlap1(1) overlap2(2) 0];
%%
latticeZRange = unique(scopeloc.gridix(:,3));
clear xyz_t xyz_tp1 XYZ_t XYZ_tp1 ranXY ranXYZ
for t = latticeZRange(1:end-1)'
%%
disp([' Layer ' num2str(t) ' of ' num2str(max(scopeloc.gridix(:,3)))]);
ix = (scopeloc.gridix(:,3)'==t);
%%
cnt = 0;
Npts = 0;
clear layer layerp1 poslayer_t poslayer_tp1
[layer layerp1 poslayer_t poslayer_tp1]=deal([]);
for id_ix = find(ix)
%%
if isempty(regpts{id_ix}.X)
continue
end
%%
cnt = cnt+1;
bb(cnt)=id_ix;
neigs = regpts{id_ix}.neigs;
layer{cnt} = regpts{id_ix}.X;
layerp1{cnt} = regpts{id_ix}.Y;
% apply tforms
Layer_t = [regpts{id_ix}.X ones(size(regpts{id_ix}.X,1),1)]*afftile(:,:,neigs(1))';
Layer_tp1 = [regpts{id_ix}.Y ones(size(regpts{id_ix}.Y,1),1)]*afftile(:,:,neigs(4))';
Npts = Npts+size(Layer_t,1);
poslayer_t{cnt} = Layer_t;
poslayer_tp1{cnt} = Layer_tp1;
dL = Layer_t-Layer_tp1;
ranXY(t) = max(sqrt(sum(dL(:,1:2).^2,2)))/1e3;
ranXYZ(t) = max(sqrt(sum(dL(:,1:3).^2,2)))/1e3;
vec{t}{id_ix} = median(dL(:,1:3));
% ranXY(t) = max(sqrt(sum((Layer_t-Layer_tp1).^2,2)))/1000;
% ranXYZ(t) = max(sqrt(sum((Layer_t-Layer_tp1).^2,2)))/1000;
end
%%
if isempty(layer)
continue
end
xyz_t{t} = cat(1,layer{:});
xyz_tp1{t} = cat(1,layerp1{:});
XYZ_t{t} = cat(1,poslayer_t{:});
XYZ_tp1{t} = cat(1,poslayer_tp1{:});
end
%%
centerPoint = 25;
scalingIntensity = 4;
dataMax = max(ranXY(ranXY>0));
dataMin = min(ranXY(ranXY>0));
newMap = newColMap(centerPoint,scalingIntensity,dataMin,dataMax);
trmax=cellfun(@max,XYZ_t(latticeZRange(1):end),'UniformOutput',false);
trmin=cellfun(@min,XYZ_t(latticeZRange(1):end),'UniformOutput',false);
Rmax = max(cat(1,trmax{:}))+scopeparams(1).imsize_um*1e3.*[1 1 0];
Rmin = min(cat(1,trmin{:}))-scopeparams(1).imsize_um*1e3.*[1 1 0];
% plot desctiptors
myfig = 100;
figure(myfig), cla, clf
hold on
loops = latticeZRange(end)-latticeZRange(1);
F(loops) = struct('cdata',[],'colormap',[]);
iter=1;
overlap(1)
for t = latticeZRange(1:end-1)'
%%
% idxtest = sliceinds(375)
ix = (scopeloc.gridix(:,3)'==t);
sliceinds = find(ix);
if any(sliceinds)
end
if t>size(XYZ_t,2)
F(iter) = getframe;
iter=iter+1;
continue
end
%%
figure(myfig), cla, clf
if isempty( XYZ_t{t})
continue
end
X = XYZ_t{t}(:,1);
Y = XYZ_t{t}(:,2);
Z = XYZ_t{t}(:,3);
U = XYZ_tp1{t}(:,1)-XYZ_t{t}(:,1);
V = XYZ_tp1{t}(:,2)-XYZ_t{t}(:,2);
W = XYZ_tp1{t}(:,3)-XYZ_t{t}(:,3);
%%
MXYZ = sqrt(U.^2+V.^2+W.^2)/1e3;
MXY = sqrt(U.^2+V.^2)/1e3;
FxXY = scatteredInterpolant(XYZ_tp1{t},MXY,'linear','nearest');
FxXYZ = scatteredInterpolant(XYZ_tp1{t},MXYZ,'linear','nearest');
xyz = scopeloc.loc(ix,:)*1e6;
valsXY = FxXY(xyz);
valsXYZ = FxXYZ(xyz);
x = scopeloc.loc(ix,1)*1e6-scopeparams(1).imsize_um(1)*1e3;
y = scopeloc.loc(ix,2)*1e6-scopeparams(1).imsize_um(2)*1e3;
w = scopeparams(1).imsize_um(1)*ones(sum(ix),1)*1e3;
h = scopeparams(1).imsize_um(2)*ones(sum(ix),1)*1e3;
colormap(newMap)
fix = find(ix);
figure(myfig), clf
h1=subaxis(2, 1, 1, 'sh', 0.03, 'sv', -0.03, 'padding', .04, 'margin', 0);
cla, hold on
disp([' Layer ' num2str(t) ' of ' num2str(max(scopeloc.gridix(:,3)))]);
caxis([min(ranXYZ(ranXYZ>0)) max(ranXYZ)*.9])
for ii=1:sum(ix)
% rectangle('Position', [x(ii) y(ii) w(ii) h(ii)])
xp = [x(ii) x(ii) x(ii)+w(ii) x(ii)+w(ii)];
yp = [y(ii) y(ii)+h(ii) y(ii)+h(ii) y(ii)];
patch(xp,yp,sqrt(valsXYZ(ii)^2-valsXY(ii)^2))
alpha(.9)
end
axis equal
scatter(XYZ_t{t}(:,1),XYZ_t{t}(:,2),6,'filled', ...
'MarkerFaceAlpha',.2,'MarkerFaceColor',[1 1 1]*.3)
set(h1, 'XTick', []);
set(h1, 'YTick', []);
set(h1,'Ydir','reverse')
view(0,90)
axis equal
%%
targetfold = fileparts(videofile);
for ii=1:sum(ix)
xlim([x(ii)+overlap(1)/2 x(ii)+w(ii)-overlap(1)/2])
ylim([y(ii)+overlap(2)/2 y(ii)+h(ii)-overlap(2)/2])
targetfold_ = fullfile(targetfold,'thumbs',scopeloc.relativepaths{fix(ii)});
mkdir(targetfold_)
try
export_fig(fullfile(targetfold_,'Thumbs.png'),'-r54')
catch
pause(.5)
end
end
end
end
function h=subaxis(varargin)
%SUBAXIS Create axes in tiled positions. (just like subplot)
% Usage:
% h=subaxis(rows,cols,cellno[,settings])
% h=subaxis(rows,cols,cellx,celly[,settings])
% h=subaxis(rows,cols,cellx,celly,spanx,spany[,settings])
%
% SETTINGS: Spacing,SpacingHoriz,SpacingVert
% Padding,PaddingRight,PaddingLeft,PaddingTop,PaddingBottom
% Margin,MarginRight,MarginLeft,MarginTop,MarginBottom
% Holdaxis
%
% all units are relative (i.e. from 0 to 1)
%
% Abbreviations of parameters can be used.. (Eg MR instead of MarginRight)
% (holdaxis means that it wont delete any axes below.)
%
%
% Example:
%
% >> subaxis(2,1,1,'SpacingVert',0,'MR',0);
% >> imagesc(magic(3))
% >> subaxis(2,'p',.02);
% >> imagesc(magic(4))
%
% 2001-2014 / Aslak Grinsted (Feel free to modify this code.)
f=gcf;
UserDataArgsOK=0;
Args=get(f,'UserData');
if isstruct(Args)
UserDataArgsOK=isfield(Args,'SpacingHorizontal')&isfield(Args,'Holdaxis')&isfield(Args,'rows')&isfield(Args,'cols');
end
OKToStoreArgs=isempty(Args)|UserDataArgsOK;
if isempty(Args)&&(~UserDataArgsOK)
Args=struct('Holdaxis',0, ...
'SpacingVertical',0.05,'SpacingHorizontal',0.05, ...
'PaddingLeft',0,'PaddingRight',0,'PaddingTop',0,'PaddingBottom',0, ...
'MarginLeft',.1,'MarginRight',.1,'MarginTop',.1,'MarginBottom',.1, ...
'rows',[],'cols',[]);
end
Args=parseArgs(varargin,Args,{'Holdaxis'},{'Spacing' {'sh','sv'}; 'Padding' {'pl','pr','pt','pb'}; 'Margin' {'ml','mr','mt','mb'}});
if (length(Args.NumericArguments)>2)
Args.rows=Args.NumericArguments{1};
Args.cols=Args.NumericArguments{2};
%remove these 2 numerical arguments
Args.NumericArguments={Args.NumericArguments{3:end}};
end
if OKToStoreArgs
set(f,'UserData',Args);
end
switch length(Args.NumericArguments)
case 0
return % no arguments but rows/cols....
case 1
if numel(Args.NumericArguments{1}) > 1 % restore subplot(m,n,[x y]) behaviour
[x1 y1] = ind2sub([Args.cols Args.rows],Args.NumericArguments{1}(1)); % subplot and ind2sub count differently (column instead of row first) --> switch cols/rows
[x2 y2] = ind2sub([Args.cols Args.rows],Args.NumericArguments{1}(end));
else
x1=mod((Args.NumericArguments{1}-1),Args.cols)+1; x2=x1;
y1=floor((Args.NumericArguments{1}-1)/Args.cols)+1; y2=y1;
end
% x1=mod((Args.NumericArguments{1}-1),Args.cols)+1; x2=x1;
% y1=floor((Args.NumericArguments{1}-1)/Args.cols)+1; y2=y1;
case 2
x1=Args.NumericArguments{1};x2=x1;
y1=Args.NumericArguments{2};y2=y1;
case 4
x1=Args.NumericArguments{1};x2=x1+Args.NumericArguments{3}-1;
y1=Args.NumericArguments{2};y2=y1+Args.NumericArguments{4}-1;
otherwise
error('subaxis argument error')
end
cellwidth=((1-Args.MarginLeft-Args.MarginRight)-(Args.cols-1)*Args.SpacingHorizontal)/Args.cols;
cellheight=((1-Args.MarginTop-Args.MarginBottom)-(Args.rows-1)*Args.SpacingVertical)/Args.rows;
xpos1=Args.MarginLeft+Args.PaddingLeft+cellwidth*(x1-1)+Args.SpacingHorizontal*(x1-1);
xpos2=Args.MarginLeft-Args.PaddingRight+cellwidth*x2+Args.SpacingHorizontal*(x2-1);
ypos1=Args.MarginTop+Args.PaddingTop+cellheight*(y1-1)+Args.SpacingVertical*(y1-1);
ypos2=Args.MarginTop-Args.PaddingBottom+cellheight*y2+Args.SpacingVertical*(y2-1);
if Args.Holdaxis
h=axes('position',[xpos1 1-ypos2 xpos2-xpos1 ypos2-ypos1]);
else
h=subplot('position',[xpos1 1-ypos2 xpos2-xpos1 ypos2-ypos1]);
end
set(h,'box','on');
%h=axes('position',[x1 1-y2 x2-x1 y2-y1]);
set(h,'units',get(gcf,'defaultaxesunits'));
set(h,'tag','subaxis');
if (nargout==0), clear h; end;
end
function ArgStruct=parseArgs(args,ArgStruct,varargin)
% Helper function for parsing varargin.
%
%
% ArgStruct=parseArgs(varargin,ArgStruct[,FlagtypeParams[,Aliases]])
%
% * ArgStruct is the structure full of named arguments with default values.
% * Flagtype params is params that don't require a value. (the value will be set to 1 if it is present)
% * Aliases can be used to map one argument-name to several argstruct fields
%
%
% example usage:
% --------------
% function parseargtest(varargin)
%
% %define the acceptable named arguments and assign default values
% Args=struct('Holdaxis',0, ...
% 'SpacingVertical',0.05,'SpacingHorizontal',0.05, ...
% 'PaddingLeft',0,'PaddingRight',0,'PaddingTop',0,'PaddingBottom',0, ...
% 'MarginLeft',.1,'MarginRight',.1,'MarginTop',.1,'MarginBottom',.1, ...
% 'rows',[],'cols',[]);
%
% %The capital letters define abrreviations.
% % Eg. parseargtest('spacingvertical',0) is equivalent to parseargtest('sv',0)
%
% Args=parseArgs(varargin,Args, ... % fill the arg-struct with values entered by the user
% {'Holdaxis'}, ... %this argument has no value (flag-type)
% {'Spacing' {'sh','sv'}; 'Padding' {'pl','pr','pt','pb'}; 'Margin' {'ml','mr','mt','mb'}});
%
% disp(Args)
%
%
%
%
% Aslak Grinsted 2004
% -------------------------------------------------------------------------
% Copyright (C) 2002-2004, Aslak Grinsted
% This software may be used, copied, or redistributed as long as it is not
% sold and this copyright notice is reproduced on each copy made. This
% routine is provided as is without any express or implied warranties
% whatsoever.
persistent matlabver
if isempty(matlabver)
matlabver=ver('MATLAB');
matlabver=str2double(matlabver.Version);
end
Aliases={};
FlagTypeParams='';
if (length(varargin)>0)
FlagTypeParams=lower(strvcat(varargin{1})); %#ok
if length(varargin)>1
Aliases=varargin{2};
end
end
%---------------Get "numeric" arguments
NumArgCount=1;
while (NumArgCount<=size(args,2))&&(~ischar(args{NumArgCount}))
NumArgCount=NumArgCount+1;
end
NumArgCount=NumArgCount-1;
if (NumArgCount>0)
ArgStruct.NumericArguments={args{1:NumArgCount}};
else
ArgStruct.NumericArguments={};
end
%--------------Make an accepted fieldname matrix (case insensitive)
Fnames=fieldnames(ArgStruct);
for i=1:length(Fnames)
name=lower(Fnames{i,1});
Fnames{i,2}=name; %col2=lower
Fnames{i,3}=[name(Fnames{i,1}~=name) ' ']; %col3=abreviation letters (those that are uppercase in the ArgStruct) e.g. SpacingHoriz->sh
%the space prevents strvcat from removing empty lines
Fnames{i,4}=isempty(strmatch(Fnames{i,2},FlagTypeParams)); %Does this parameter have a value?
end
FnamesFull=strvcat(Fnames{:,2}); %#ok
FnamesAbbr=strvcat(Fnames{:,3}); %#ok
if length(Aliases)>0
for i=1:length(Aliases)
name=lower(Aliases{i,1});
FieldIdx=strmatch(name,FnamesAbbr,'exact'); %try abbreviations (must be exact)
if isempty(FieldIdx)
FieldIdx=strmatch(name,FnamesFull); %&??????? exact or not?
end
Aliases{i,2}=FieldIdx;
Aliases{i,3}=[name(Aliases{i,1}~=name) ' ']; %the space prevents strvcat from removing empty lines
Aliases{i,1}=name; %dont need the name in uppercase anymore for aliases
end
%Append aliases to the end of FnamesFull and FnamesAbbr
FnamesFull=strvcat(FnamesFull,strvcat(Aliases{:,1})); %#ok
FnamesAbbr=strvcat(FnamesAbbr,strvcat(Aliases{:,3})); %#ok
end
%--------------get parameters--------------------
l=NumArgCount+1;
while (l<=length(args))
a=args{l};
if ischar(a)
paramHasValue=1; % assume that the parameter has is of type 'param',value
a=lower(a);
FieldIdx=strmatch(a,FnamesAbbr,'exact'); %try abbreviations (must be exact)
if isempty(FieldIdx)
FieldIdx=strmatch(a,FnamesFull);
end
if (length(FieldIdx)>1) %shortest fieldname should win
[mx,mxi]=max(sum(FnamesFull(FieldIdx,:)==' ',2));%#ok
FieldIdx=FieldIdx(mxi);
end
if FieldIdx>length(Fnames) %then it's an alias type.
FieldIdx=Aliases{FieldIdx-length(Fnames),2};
end
if isempty(FieldIdx)
error(['Unknown named parameter: ' a])
end
for curField=FieldIdx' %if it is an alias it could be more than one.
if (Fnames{curField,4})
if (l+1>length(args))
error(['Expected a value for parameter: ' Fnames{curField,1}])
end
val=args{l+1};
else %FLAG PARAMETER
if (l<length(args)) %there might be a explicitly specified value for the flag
val=args{l+1};
if isnumeric(val)
if (numel(val)==1)
val=logical(val);
else
error(['Invalid value for flag-parameter: ' Fnames{curField,1}])
end
else
val=true;
paramHasValue=0;
end
else
val=true;
paramHasValue=0;
end
end
if matlabver>=6
ArgStruct.(Fnames{curField,1})=val; %try the line below if you get an error here
else
ArgStruct=setfield(ArgStruct,Fnames{curField,1},val); %#ok <-works in old matlab versions
end
end
l=l+1+paramHasValue; %if a wildcard matches more than one
else
error(['Expected a named parameter: ' num2str(a)])
end
end
end