-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshow_saveResult.m
300 lines (251 loc) · 11.6 KB
/
show_saveResult.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
function project=show_saveResult(project,TaskIndex,MethodIndex,varargin)
% show_saveResult function is a simpel way to converte few slices of a brain image to
% a bitmap or like. Converted images can be shown in the DisplayWindow in the pipeline program.
% Brain images must be in the Analyze format filenames found in 'project.taskDone.outputfiles'.
% Image info are saved in 'project.taskDone{TaskIndex}.show'
%
% Input:
% project : Structure containing all information of actual pipeline
% TaskIndex : Index to active task in the project structure
% MethodIndex : Index to used method in a TaskIndex in the project structure
% varargin{1} : Output format of image: 'bmp', 'fig', 'jpg' or 'png' (DEFAULT='png')
%
% Output:
% project : Return updated project
%
% Uses special functions:
% [img,hdr]=ReadAnalyzeImg(name)
% supercolorbar
% logProject
%____________________________________________
% T. Rask og T. Dyrby, 010403, NRU
%
%SW version: 201103TR,TD
%_________ Which format to save the figures to shown in progress/result window
if(nargin==4)
outputformat=varargin{1};
switch outputformat
case {'bmp','fig','jpg'}
otherwise
outputformat='png';
end
else
outputformat='png';
end
ImageIndex=project.pipeline.imageIndex(1);
scrsz = get(0,'ScreenSize');
h_subfig=figure('resize','off','position',[50 50 scrsz(3)-100 scrsz(4)-100],'visible','off');
%set(h_subfig,);
%set(h_subfig,'visible','off'); %This line cannot run in windows
%_______Find out if there is anything to show already
showNumber=1;
howbig=size(project.taskDone{TaskIndex}.show);
for (i=1:howbig(2))
if ~isempty(project.taskDone{TaskIndex}.show{ImageIndex,i}.name)
showNumber=showNumber+1;
end
end
%_______ Get loaded number of modalities (= number of outputfile fields)
noModalities=length(project.pipeline.imageModality);
%_______Run through outputfiles
for(ModalityIndex=1:noModalities)
filename=fullfile('',project.taskDone{TaskIndex}.outputfiles{ImageIndex,ModalityIndex}.path,project.taskDone{TaskIndex}.outputfiles{ImageIndex,ModalityIndex}.name);
fid=fopen(filename,'r');
if(fid>0)
fclose(fid);
%_________ Show results
Title_msg=project.taskDone{TaskIndex}.outputfiles{ImageIndex,ModalityIndex}.name;
MakeShow(h_subfig,filename,Title_msg);
%_________ Save results as Matlab fig
[file_pathstr,file_name,file_ext]=fileparts(filename);
info='';
if (~isempty(project.taskDone{TaskIndex}.outputfiles{ImageIndex,ModalityIndex}.info) &...
~isstruct(project.taskDone{TaskIndex}.outputfiles{ImageIndex,ModalityIndex}.info))
info=[project.taskDone{TaskIndex}.outputfiles{ImageIndex,ModalityIndex}.info,' - ']
end
project.taskDone{TaskIndex}.show{ImageIndex,showNumber}.name=fullfile('',['Show-',project.pipeline.taskSetup{TaskIndex,MethodIndex}.task,'_',file_name,'.',outputformat]);
project.taskDone{TaskIndex}.show{ImageIndex,showNumber}.path=project.sysinfo.workspace;
project.taskDone{TaskIndex}.show{ImageIndex,showNumber}.info=[info,'Image generated from ',Title_msg];
filenameShow=fullfile('',project.taskDone{TaskIndex}.show{ImageIndex,showNumber}.path,project.taskDone{TaskIndex}.show{ImageIndex,showNumber}.name);
showNumber=1+showNumber;
%figure(h_subfig); %Bring figure to be current
%set(h_subfig,'visible','off');
%Only save if workspace is know. Not alwas the case of fileload
if(~isempty(project.sysinfo.workspace))
%print('-dpng','-r500',filenameShow)%save as tiff
saveas(h_subfig, fullfile('',filenameShow),outputformat);
end
end
end%ModalityIndex
%Find Imagefiles in userdata field
ud=struct2entries(['project.taskDone{',num2str(TaskIndex),'}.userdata'],project.taskDone{TaskIndex}.userdata);
l=length(ud);
for i=1:l
f1=strfind(lower(ud{i}.entry),'.name');
if f1
for j=-2:2 %look two forward and backwards after path
if ((i+j)>0 & (i+j)<=l)
f2=strfind(lower(ud{i+j}.entry),'.path');
if f2
cmp=strcmp(ud{i}.entry(1:f1),ud{i+j}.entry(1:f2));
if (cmp & ~isempty(eval(ud{i}.entry)))
file=fullfile('',eval(ud{i+j}.entry),eval(ud{i}.entry));
[pa,name,ext]=fileparts(file);
if strcmp(lower(ext),'.img')
fid=fopen(file,'r');
if(fid>0)
fclose(fid);
%_____ Find info
info='';
for k=-2:2 %look two forward and backwards after info
if ((i+k)>0 & (i+k)<=l)
f3=strfind(lower(ud{i+k}.entry),'.info');
if f3
cmp=strcmp(ud{i}.entry(1:f1),ud{i+k}.entry(1:f3));
if cmp & ~isempty(eval(ud{i+k}.entry))
info=[eval(ud{i+k}.entry),' - '];
end
end
end
end
%_________ Show results
Title_msg=[name,ext];
MakeShow(h_subfig,file,Title_msg);
%_________ Save results as Matlab fig
project.taskDone{TaskIndex}.show{ImageIndex,showNumber}.name=fullfile('',['Show-',project.pipeline.taskSetup{TaskIndex,MethodIndex}.task,'_',name,'.',outputformat]);
project.taskDone{TaskIndex}.show{ImageIndex,showNumber}.path=project.sysinfo.workspace;
project.taskDone{TaskIndex}.show{ImageIndex,showNumber}.info=[info,'Image generated from ',Title_msg];
filenameShow=fullfile('',project.taskDone{TaskIndex}.show{ImageIndex,showNumber}.path,project.taskDone{TaskIndex}.show{ImageIndex,showNumber}.name);
showNumber=1+showNumber;
%figure(h_subfig); %Bring figure to be current
%set(h_subfig,'visible','off');
%Only save if workspace is know. Not alwas the case of fileload
if(~isempty(project.sysinfo.workspace))
%print('-dpng','-r500',filenameShow)%save as tiff
saveas(h_subfig, fullfile('',filenameShow),outputformat);
end
end
end
end
end
end
end
end
end
close(h_subfig);
return %show_saveResult
% _____ Add to pdf or postscript
%
% %print('-dtiff','-r200',imagefile)%save as tiff
% %Cimage=imread(imagefile,'png');%load tiff for later use
% %delete([imagefile,'.tif']);%Delete tmpfile
% %figure(h_showImg),hold on;
% %subplot(length(filenames),1,i), image(Cimage), axis off, axis tight;
% close(h_fig);
%
% %__________ print to pdf
% %set(h_showImg,'Papertype','A4')
% %print(gcf,'-dpdf',filenameShowRes);
function h_fig=MakeShow(h_fig,filenames,Title_msg)
% NOT FINISH BUT WORKs!!!! 230503TD
% Setup what to show slides and orientation
%
% Input:
% h_fig: Handles to figure where to draw
% filenames: cell array of filenames in analyseformat
% filenames{1}=inputfilename
% filenames{2}=outputfilename
% title_msg: cellarray w. title for each filename
%
% Output:
% h_fig: handle to figure where result is shown
%________________________________________________________
% SW label: 070404TD, NRU
%__________ Slices to show
slcShowPerc=[0.5];
timeframePerc=0.5; %if 4D.
x=length(filenames);
%__________ Load image with given slides
[img,hdr]=ReadAnalyzeImg(filenames);
img=reshape(img,double(hdr.dim'));
if length(hdr.dim)>3 & hdr.dim>1
timeframe=ceil(hdr.dim(4)*timeframePerc);
Title_msg=[Title_msg,'-frame ',num2str(timeframe)];
else
timeframe=1;
end
img1=squeeze(img(ceil(slcShowPerc.*hdr.dim(1)),:,:,timeframe));
img2=squeeze(img(:,ceil(slcShowPerc.*hdr.dim(2)),:,timeframe));
img3=img(:,:,ceil(slcShowPerc.*hdr.dim(3)),timeframe);
[scale_min,scale_max,imgType]=findLim(img(:,:,:,timeframe));%subfunction to scale intensities/counts
clear img;
if(strcmp(imgType,'MR')) %Select color for MR or PET
colo='gray';
else
colo='hot';
end
x=hdr.dim(1)*hdr.siz(1); %Picture dimensions
y=hdr.dim(2)*hdr.siz(2);
z=hdr.dim(3)*hdr.siz(3);
%scale axes to fit images
if y>x, xa=y; else xa=x; end;
if z>y, ya=z; else ya=y; end;
%_____ First axes
h1=subplot('position',[0.01 0.51 0.48 0.48]);
imagesc([0,x],[0,z],img2',[scale_min, scale_max]);
set(gca,'xlim',[(x-xa)/2 x-(x-xa)/2],'ylim',[(z-ya)/2 z-(z-ya)/2],'xaxislocation','top','yaxislocation','left');
axis xy;
axis off;
axis equal;
% xlabel('X');
% ylabel('Z');
%_____ Second axes
h2=subplot('position',[0.51 0.51 0.48 0.48]);
imagesc([0,y],[0,z],img1',[scale_min, scale_max]);
set(gca,'xlim',[(y-xa)/2 y-(y-xa)/2],'ylim',[(z-ya)/2 z-(z-ya)/2],'xaxislocation','top','yaxislocation','right');
axis xy;
axis off;
axis equal;
% xlabel('Y');
% ylabel('Z');
%_____ Third axes
h3=subplot('position',[0.01 0.01 0.48 0.48]);
imagesc([0,x],[0,y],img3',[scale_min, scale_max]);
set(gca,'xlim',[(x-xa)/2 x-(x-xa)/2],'ylim',[(y-ya)/2 y-(y-ya)/2],'xaxislocation','bottom','yaxislocation','left');
axis xy;
axis off;
axis equal;
% xlabel('X');
% ylabel('Y');
%_____ Fourth axes
h4=subplot('position',[0.51 0.01 0.48 0.48]);
img4(1,1)=scale_min;
img4(2,2)=scale_max;
h_joke=imagesc(img4,[scale_min, scale_max]); %There have to be an image, else the colorbar wont have the right limits
axis([0 1 0 1]);
axis xy;
axis off;
set(h_joke,'visible','off');
%______ Set text
h_title=text(0,1,Title_msg,'fontsize',15,'fontweight','bold','interpreter','none');
text(0,0.9,['Description:'],'fontsize',15);
text(0,0.8,['"',hdr.descr,'"'],'fontsize',15,'interpreter','none');
if length(hdr.dim)>3
text(0,0.7,['Dimensions: ',num2str(hdr.dim(1)),'x',num2str(hdr.dim(2)),'x',num2str(hdr.dim(3)),'x',num2str(hdr.dim(4))],'fontsize',15);
else
text(0,0.7,['Dimensions: ',num2str(hdr.dim(1)),'x',num2str(hdr.dim(2)),'x',num2str(hdr.dim(3))],'fontsize',15);
end
text(0,0.6,['Voxel size [mm]:'],'fontsize',15);
text(0,0.5,[num2str(hdr.siz(1)),'x',num2str(hdr.siz(2)),'x',num2str(hdr.siz(3))],'fontsize',15);
text(0,0.4,['Color depth: ',num2str(hdr.pre),'bit'],'fontsize',15);
text(0,0.3,['Color limits: ',num2str(hdr.lim(2)),' to ',num2str(hdr.lim(1))],'fontsize',15);
text(0,0.2,['Origin: ',num2str(hdr.origin(1)),'x',num2str(hdr.origin(2)),'x',num2str(hdr.origin(3))],'fontsize',15);
text(0,0.1,['Offset: ',num2str(hdr.offset),' Scale: ',num2str(hdr.scale)],'fontsize',15);
text(0,0.01,['Endian: ',hdr.endian],'fontsize',15,'interpreter','none');
colormap(colo);
h_bar=colorbar('vert','peer',h4); % set colormap
posi=get(h_bar,'position');
posi(1)=posi(1)-0.05;
set(h_bar,'position',posi);
%get(h_bar);
return %MakeShow