-
Notifications
You must be signed in to change notification settings - Fork 5
/
aedes_createmosaic.m
542 lines (493 loc) · 14.3 KB
/
aedes_createmosaic.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
function aedes_createmosaic(DATA,ROI,overlay,varargin)
% AEDES_CREATEMOSAIC - Create a figure containing mosaic of MR images
%
%
% Synopsis:
%
% Description:
%
% Examples:
%
% See also:
% AEDES, AEDES_EXPORT_GUI
% This function is a part of Aedes - A graphical tool for analyzing
% medical images
%
% Copyright (C) 2006 Juha-Pekka Niskanen <Juha-Pekka.Niskanen@uku.fi>
%
% Department of Physics, Department of Neurobiology
% University of Kuopio, FINLAND
%
% This program may be used under the terms of the GNU General Public
% License version 2.0 as published by the Free Software Foundation
% and appearing in the file LICENSE.TXT included in the packaging of
% this program.
%
% This program is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
% WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
% Defaults
sliceDir = 3; % Z-direction
roi_ind = 1:length(ROI);
drawSliceNbr = false;
drawColorbar = true;
drawFileName = false;
drawOverlay = true;
drawRois = true;
colmap = gray(256);
sliceInd = [];
rows = [];
cols = [];
Clim_in = [];
roiTransp = 1;
showRoiEdges = false;
Dat.currentVol = 1;
% Go through varargin
for ii=1:2:length(varargin)
switch lower(varargin{ii})
case 'slicedir'
sliceDir = varargin{ii+1};
case 'drawslicenbr'
drawSliceNbr = varargin{ii+1};
case 'drawrois'
drawRois = varargin{ii+1};
case 'drawoverlay'
drawOverlay = varargin{ii+1};
case 'drawcolorbar'
drawColorbar = varargin{ii+1};
case 'drawfilename'
drawFileName = varargin{ii+1};
case 'roiind'
roi_ind = varargin{ii+1};
case 'colormap'
colmap = varargin{ii+1};
case 'cols'
cols = varargin{ii+1};
case 'rows'
rows = varargin{ii+1};
case 'sliceind'
sliceInd = varargin{ii+1};
case 'vol'
Dat.currentVol = varargin{ii+1};
case 'clim'
Clim_in = varargin{ii+1};
case 'roitransp'
roiTransp = varargin{ii+1};
case 'showRoiEdges'
showRoiEdges = varargin{ii+1};
end
end
if isempty(ROI)
drawRois = false;
roi_ind = [];
end
if nargin < 3 || isempty(overlay)
drawOverlay = false;
end
if ~iscell(DATA)
DATA = {DATA};
end
if length(DATA)>1
Dat.isDataMixed = true;
else
Dat.isDataMixed = false;
end
% Store data sizes in all directions
if Dat.isDataMixed
Dat.XSize = 0;
Dat.YSize = 0;
Dat.ZSize = length(DATA);
else
Dat.XSize = size(DATA{1}.FTDATA,1);
Dat.YSize = size(DATA{1}.FTDATA,2);
Dat.ZSize = size(DATA{1}.FTDATA,3);
Dat.VSize = size(DATA{1}.FTDATA,4);
end
if isempty(sliceInd)
if sliceDir==1
Dat.SliceInd = 1:Dat.XSize;
elseif sliceDir==2
Dat.SliceInd = 1:Dat.YSize;
else
Dat.SliceInd = 1:Dat.ZSize;
end
else
Dat.SliceInd = sliceInd;
end
if isempty(rows) && isempty(cols)
rows = floor(sqrt(length(Dat.SliceInd)));
cols = ceil(length(Dat.SliceInd)/rows);
elseif isempty(rows)
rows = ceil(length(Dat.SliceInd)/cols);
elseif isempty(cols)
cols = ceil(length(Dat.SliceInd)/rows);
end
% Estimate space for the mosaic
if Dat.isDataMixed
data_sz=size(DATA{Dat.SliceInd(1)}.FTDATA);
else
if sliceDir==3
data_sz=size(DATA{1}.FTDATA(:,:,Dat.SliceInd(1),Dat.currentVol));
elseif sliceDir==2
data_sz=size(squeeze(DATA{1}.FTDATA(:,Dat.SliceInd(1),:,Dat.currentVol)));
else
data_sz=size(squeeze(DATA{1}.FTDATA(Dat.SliceInd(1),:,:,Dat.currentVol)));
end
end
% Determine figure size
scrsz = get(0,'screensize'); % Get current display resolution
scrsz(4) = scrsz(4)-45-70; % The 47px is for taskbar,
scrsz(3) = scrsz(3)-16;
gap=3;
fig_w = cols*data_sz(2)+(cols+1)*gap;
fig_h = rows*data_sz(1)+(rows+1)*gap;
if fig_w>scrsz(3) || fig_h>scrsz(4)
aratio_w=[fig_w fig_h]./fig_w;
aratio_h=[fig_w fig_h]./fig_h;
for ii=1:2
if fig_w>scrsz(3)
tmp=aratio_w*scrsz(3);
fig_w = tmp(1);
fig_h = tmp(2);
elseif fig_h>scrsz(4)
tmp=aratio_h*scrsz(4);
fig_w = tmp(1);
fig_h = tmp(2);
end
end
end
%% Determine paper orientation
if fig_w>fig_h
paperorient = 'landscape';
%% Determine paperposition
papersize = [29.7 21.0];
tmp=[fig_w,fig_h]./fig_h;
pap_h = tmp(2)*(papersize(2)-2);
pap_w = tmp(1)*(papersize(2)-2);
if pap_w>(papersize(1)-2)
pap_h=((papersize(1)-2)/pap_w)*pap_h;
pap_w = papersize(1)-2;
end
paperpos = [papersize(1)/2-pap_w/2 ...
papersize(2)/2-pap_h/2 ...
pap_w ...
pap_h];
else
paperorient = 'portrait';
%% Determine paperposition
papersize = [21.0 29.7];
tmp=[fig_w,fig_h]./fig_w;
pap_h = tmp(2)*(papersize(1)-2);
pap_w = tmp(1)*(papersize(1)-2);
if pap_h>(papersize(2)-2)
pap_w=((papersize(2)-2)/pap_h)*pap_w;
pap_h = papersize(2)-2;
end
paperpos = [papersize(1)/2-pap_w/2 ...
papersize(2)/2-pap_h/2 ...
pap_w ...
pap_h];
end
%% Show wait dialog
[wait_h,txh]=aedes_calc_wait('Creating mosaic from slices...');
% Draw the mosaic figure
fh = figure('position',[scrsz(3)/2-fig_w/2+4 ...
scrsz(4)/2-fig_h/2+45 fig_w fig_h],...
'visible','on',...
'inverthardcopy','off',...
'renderer','opengl',...%'painters',...
'numbertitle','off',...
'name','Mosaic View',...
'colormap',colmap,...
'color','w',...
'papertype','a4',...
'paperpositionmode','manual',...
'paperorientation',paperorient,...
'paperunits','centimeters',...
'paperposition',paperpos);
set(fh,'paperunits','inches')
if ( roiTransp==0 | not(drawRois)) && not(drawOverlay)
set(fh,'renderer','painters');
end
% Set resolution information in the figure
if ispref('Aedes','ExportMosaicImageResolution')
default_resolution = getpref('Aedes','ExportMosaicImageResolution');
else
default_resolution = 300;
end
setappdata(fh,'Exportsetup',struct('Resolution',default_resolution))
%% Set header text
if ~Dat.isDataMixed
hs = getappdata(fh,'PrintHeaderHeaderSpec');
if isempty(hs)
hs = struct('dateformat','none',...
'string',[date,', ',...
strrep(strrep(DATA{1}.HDR.fpath,'\','\\'),'_','\_'),...
strrep(strrep(DATA{1}.HDR.fname,'\','\\'),'_','\_')],...
'fontname','Times',...
'fontsize',12,... % in points
'fontweight','normal',...
'fontangle','normal',...
'margin',15); % in points
end
setappdata(fh,'PrintHeaderHeaderSpec',hs)
end
% Look for custom aspect ratio
if ispref('Aedes','ExportMosaicUseCustomAspectRatio')
data_aspectratio = getpref('Aedes','ExportMosaicUseCustomAspectRatio');
else
data_aspectratio = [1 1 1];
end
%% images and axes
count=1;
gap_w=gap/fig_w;
gap_h=gap/fig_h;
for ii=1:rows
for kk=1:cols
ax=axes('parent',fh,...
'units','normal',...
'position',[(kk-1)*((1-(cols+1)*gap_w)/cols)+kk*gap_w ...
(rows-ii)*((1-(rows+1)*gap_h)/rows)+(rows+1-ii)*gap_h ...
(1-(cols+1)*gap_w)/cols (1-(rows+1)*gap_h)/rows],...
'visible','off',...
'ydir','reverse',...
'xtick',[],...
'ytick',[],...
'xticklabel',[],...
'yticklabel',[],...
'DataAspectRatio',data_aspectratio,...
'PlotBoxAspectRatio',[data_sz(2) data_sz(1) 1],...
'PlotBoxAspectRatioMode','manual');
if strcmpi(get(fh,'renderer'),'opengl')
roi_ax = axes('parent',fh,...
'units','normal',...
'position',[(kk-1)*((1-(cols+1)*gap_w)/cols)+kk*gap_w ...
(rows-ii)*((1-(rows+1)*gap_h)/rows)+(rows+1-ii)*gap_h ...
(1-(cols+1)*gap_w)/cols (1-(rows+1)*gap_h)/rows],...
'visible','off',...
'ydir','reverse',...
'xtick',[],...
'ytick',[],...
'xticklabel',[],...
'yticklabel',[],...
'DataAspectRatio',data_aspectratio,...
'PlotBoxAspectRatio',[data_sz(2) data_sz(1) 1],...
'PlotBoxAspectRatioMode','manual');
overlay_ax = axes('parent',fh,...
'units','normal',...
'position',[(kk-1)*((1-(cols+1)*gap_w)/cols)+kk*gap_w ...
(rows-ii)*((1-(rows+1)*gap_h)/rows)+(rows+1-ii)*gap_h ...
(1-(cols+1)*gap_w)/cols (1-(rows+1)*gap_h)/rows],...
'visible','off',...
'ydir','reverse',...
'xtick',[],...
'ytick',[],...
'xticklabel',[],...
'yticklabel',[],...
'DataAspectRatio',data_aspectratio,...
'PlotBoxAspectRatio',[data_sz(2) data_sz(1) 1],...
'PlotBoxAspectRatioMode','manual');
end
if count<=length(Dat.SliceInd)
if Dat.isDataMixed
imdata=DATA{Dat.SliceInd(count)}.FTDATA;
else
if sliceDir==3
imdata=DATA{1}.FTDATA(:,:,Dat.SliceInd(count),Dat.currentVol);
if drawOverlay
if overlay.isOverlayRGB
overlay_data = squeeze(overlay.ImageOverlay(:,:,Dat.SliceInd(count),:));
else
overlay_data = overlay.ImageOverlay(:,:,Dat.SliceInd(count),Dat.currentVol);
end
end
elseif sliceDir==2
imdata=squeeze(DATA{1}.FTDATA(:,Dat.SliceInd(count),:,Dat.currentVol));
if drawOverlay
if overlay.isOverlayRGB
overlay_data = squeeze(overlay.ImageOverlay(:,Dat.SliceInd(count),:,:));
else
overlay_data = squeeze(overlay.ImageOverlay(:,Dat.SliceInd(count),:,Dat.currentVol));
end
end
else
imdata=squeeze(DATA{1}.FTDATA(Dat.SliceInd(count),:,:,Dat.currentVol));
if drawOverlay
if overlay.isOverlayRGB
overlay_data = squeeze(overlay.ImageOverlay(Dat.SliceInd(count),:,:,:));
else
overlay_data = squeeze(overlay.ImageOverlay(Dat.SliceInd(count),:,:,Dat.currentVol));
end
end
end
end
%% Plot image
h=image('parent',ax,...
'cdata',imdata,...
'cdatamapping','scaled');
axis(ax,'image')
% Plot overlay
if drawOverlay
if ~overlay.isOverlayRGB
ov_cmap = overlay.ImageOverlayCmap;
ov_clim = round(((overlay.ImageOverlayClim-overlay.ImOverlayMin)*256)./...
(overlay.ImOverlayMax-overlay.ImOverlayMin));
if ov_clim(1)==ov_clim(2)
if ov_clim(1)==0
ov_clim(2)=1;
elseif ov_clim(1)==256
ov_clim(1)=255;
end
end
ov_thold = round(((overlay.ImageOverlayThold-overlay.ImOverlayMin)*256)/...
(overlay.ImOverlayMax-overlay.ImOverlayMin));
end
ov_alpha_val = overlay.ImageOverlayAlpha;
if ~overlay.isOverlayRGB
% Convert indexed image to RGB image
%slice1_ind=overlay.ImageOverlay(:,:,Dat.Slices(1),Dat.CurrentVol);
overlay_ind=double(overlay_data);
% Get thresholded alpha indices
if overlay.ImageOverlayTholdDirPos==1
overlay_alpha_th = overlay_ind<ov_thold;
else
overlay_alpha_th = overlay_ind>ov_thold;
end
% Get clim alpha indices
overlay_alpha_clim = ( overlay_ind>=ov_clim(1) & overlay_ind<=ov_clim(2) );
overlay_ind(overlay_ind<ov_clim(1))=ov_clim(1);
overlay_ind(overlay_ind>ov_clim(2))=ov_clim(2);
overlay_ind=ceil((overlay_ind-ov_clim(1))./diff(ov_clim)*255+1);
sz = size(overlay_ind);
overlay_im = zeros([sz(1) sz(2) 3],'single');
overlay_im(:,:,1)= reshape(ov_cmap(overlay_ind,1),sz);
overlay_im(:,:,2)= reshape(ov_cmap(overlay_ind,2),sz);
overlay_im(:,:,3)= reshape(ov_cmap(overlay_ind,3),sz);
overlay_alpha = zeros(size(overlay_ind));
overlay_alpha(overlay_alpha_clim) = ov_alpha_val;
overlay_alpha(overlay_alpha_th) = 0;
else
overlay_im = overlay_data;
overlay_alpha = ov_alpha_val;
end
h=image('parent',overlay_ax,...
'cdata',overlay_im,...
'cdatamapping','scaled',...
'AlphaDataMapping','none',...
'AlphaData',overlay_alpha,...
'visible','on');
end
%% Check Clim
if isempty(Clim_in)
clim = [min(min(imdata)) max(max(imdata))];
set(ax,'clim',clim)
elseif size(Clim_in,1)>1
set(ax,'clim',Clim_in(Dat.SliceInd(count),:))
else
clim = Clim_in;
set(ax,'clim',clim)
end
% Draw ROIs
if drawRois
for tt=1:length(roi_ind)
if Dat.isDataMixed
roidata = ROI(roi_ind(tt)).voxels{Dat.SliceInd(count)};
else
if sliceDir==3
roidata = ROI(roi_ind(tt)).voxels{1}(:,:,Dat.SliceInd(count),Dat.currentVol);
elseif sliceDir==2
roidata = squeeze(ROI(roi_ind(tt)).voxels{1}(:,Dat.SliceInd(count),:,Dat.currentVol));
else
roidata = squeeze(ROI(roi_ind(tt)).voxels{1}(Dat.SliceInd(count),:,:,Dat.currentVol));
end
if isempty(find(roidata))
continue;
end
end
if strcmpi(get(fh,'renderer'),'opengl')
roidata=uint8(roidata);
cdata = zeros([size(roidata) 3],'uint8');
if ROI(roi_ind(tt)).color(1)~=0
cdata(:,:,1) = roidata*ROI(roi_ind(tt)).color(1);
end
if ROI(roi_ind(tt)).color(2)~=0
cdata(:,:,2) = roidata*ROI(roi_ind(tt)).color(2);
end
if ROI(roi_ind(tt)).color(3)~=0
cdata(:,:,3) = roidata*ROI(roi_ind(tt)).color(3);
end
alphadata=double(roidata)*roiTransp;
h=image('parent',roi_ax,'cdata',cdata,...
'AlphaDataMapping','none',...
'cdatamapping','scaled',...
'AlphaData',alphadata,...
'visible','on');
end
if showRoiEdges
B=bwboundaries(roidata,4,'holes');
for jj=1:length(B)
boundary = B{jj};
line('parent',ax,...
'xdata',boundary(:,2),'ydata',boundary(:,1),...
'color',ROI(roi_ind(tt)).color./255,...
'tag','roiedge',...
'linewidth',0.5,...
'linestyle','-',...
'hittest','off');
end % for jj=1:length(B)
end
end % for tt=1:length(roi_ind)
end % if drawRois
% Draw slice number and filename
if drawSliceNbr || drawFileName
if (rows*cols)<=50
fontsz=8;
elseif (rows*cols)>50 & (rows*cols)<100
fontsz=7;
else
fontsz=6;
end
if drawSliceNbr && drawFileName
if Dat.isDataMixed
fname = DATA{Dat.SliceInd(count)}.HDR.fname;
fpath = DATA{Dat.SliceInd(count)}.HDR.fpath;
else
fname=DATA{1}.HDR.fname;
fpath = DATA{1}.HDR.fpath;
end
if strcmp(fname,'fid')
[fp,fn,fe]=fileparts(fpath(1:end-1));
fname = [fn,fe];
end
slicetxt = sprintf('%03d: %s',Dat.SliceInd(count),fname);
elseif drawFileName
if Dat.isDataMixed
fname = DATA{Dat.SliceInd(count)}.HDR.fname;
fpath = DATA{Dat.SliceInd(count)}.HDR.fpath;
else
fname=DATA{1}.HDR.fname;
fpath = DATA{1}.HDR.fpath;
end
if strcmp(fname,'fid')
[fp,fn,fe]=fileparts(fpath(1:end-1));
fname = [fn,fe];
end
slicetxt = fname;
elseif drawSliceNbr
slicetxt = sprintf('%03d',Dat.SliceInd(count));
end
tx_slice=text('parent',ax,...
'units','normal',...
'position',[0.01 0.99],...
'verticalalign','top',...
'horizontalalign','left',...
'interpreter','none',...
'string',slicetxt,...
'backgroundcolor','w',...
'clipping','off',...
'fontsize',fontsz);
end
count=count+1;
end % if count<=length(Dat.
end % for kk=1:cols
end % for ii=1:rows
delete(wait_h)