-
Notifications
You must be signed in to change notification settings - Fork 1
/
ps_LST_create_gif.m
executable file
·406 lines (361 loc) · 12.8 KB
/
ps_LST_create_gif.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
function [nameGIF, r] = ps_LST_create_gif(varargin)
%ps_LST_create_gif Creates PNG images for given images.
% Part of the LST toolbox, www.statistical-modeling.de/lst.html
%
% Sorry, there is no further documentation at this moment.
%
% Check input
nonewname = 0;
r = 0;
or = [1 2 3];
fl = 0;
if nargin == 0
V = spm_select(1, 'image', 'Select image.');
Vover = spm_select(Inf, 'image', 'Select overlay images (optional).');
end
if nargin > 0
if ischar(varargin{1})
V = varargin{1};
else
fprintf('Input for V must be a character, like from spm_select.\n');
return;
end
if nargin > 1
if ischar(varargin{2})
Vover = varargin{2};
else
fprintf('Input for Vover must be a character, like from spm_select.\n');
return;
end
end
if nargin > 2
nameGIF = varargin{3};
nonewname = 1;
%V = varargin{1};
%Vover = varargin{2};
%img_input = 1;
end
if nargin > 3
r = varargin{4};
end
if nargin > 4
or = varargin{5};
fl = or(end);
or = or(1:3);
end
end
% create file name
orpth = cd;
if numel(V) == 1
[pth, nam, ~] = fileparts(V);
else
[pth, nam, ~] = fileparts(V(1,:));
end
if ~nonewname
nameGIF = ['LST_', nam, '_', ps_create_timestamp];
end
namePNG = nameGIF;
nameGIF = [nameGIF, '.gif'];
folder = namePNG;
cd(pth)
if ~nonewname
mkdir(folder);
end
overlay = ~isempty(Vover);
V = spm_vol(V);
Vover = spm_vol(Vover);
% Remove ugly border around the image
%iptsetpref('ImshowBorder','tight');
fid = figure('visible', 'off');
if overlay
% How many overlays?
n = numel(Vover);
m = numel(V);
if m == 1
% Do all images have the same dimension?
dims = zeros(n+1, 3);
dims(end,:) = V.dim;
lcl = zeros(n, 1);
for i = 1:n
dims(i,:) = Vover(i).dim;
lcl(i) = ~isempty(regexp(Vover(i).fname, 'LCL', 'once'));
end
if any(sum(dims) ~= (dims(1,:) .* (n+1)))
fprintf('Images must all have the same dimensions!\n')
close(fid)
return;
end
% Load images
over = zeros(dims(1,1), dims(1,2), dims(1,3), n);
for i = 1:n
tmp = spm_read_vols(Vover(i));
% Flip image
if any(or ~= [1 2 3])
tmp = permute(tmp, or);
if i == 1
over = zeros(size(tmp,1), size(tmp,2), size(tmp,3), n);
end
end
if fl
if exist('flip', 'builtin')
tmp = flip(tmp, 2);
else
tmp = flipdim(tmp, 2);
end
end
over(:,:,:,i) = tmp;
end
img = spm_read_vols(V);
%if any(or ~= [1 3 2])
if any(or ~= [1 2 3])
img = permute(img, or);
end
if fl
if exist('flip', 'builtin')
img = flip(img, 2);
else
img = flipdim(img, 2);
end
end
img(isnan(img)) = 0;
img(:) = (img(:) ./ ps_quantile(img(:), .9999));
img(img >= 1) = 0.9999;
% Where to start?
if numel(r) == 1
c_tmp = indx2coord(find(img > 0), size(img, 1), size(img, 2));
r = [min(c_tmp(:,3)), max(c_tmp(:,3))];
end
counter = 0;
for z = r(1):r(2)
z_tmp = r(2) - counter;
counter = counter + 1;
if exist('flip', 'builtin')
img_tmp = flip(img(:,:,z_tmp)');
over_tmp = 0 .* flip(over(:,:,z_tmp,1)');
else
img_tmp = flipdim(img(:,:,z_tmp)', 1);
over_tmp = 0 .* flipdim(over(:,:,z_tmp,1)', 1);
end
for i = 1:n
if exist('flip', 'builtin')
over_tmp = [over_tmp, flip(over(:,:,z_tmp,i)')];
img_tmp = [img_tmp, flip(img(:,:,z_tmp)')];
else
over_tmp = [over_tmp, flipdim(over(:,:,z_tmp,i)', 1)];
img_tmp = [img_tmp, flipdim(img(:,:,z_tmp)', 1)];
end
end
over_tmp_rgb = zeros(size(over_tmp, 1), size(over_tmp, 2), 3);
if any(lcl > 0)
over_tmp2 = over_tmp .* 0;
over_tmp2(over_tmp == 1) = 0;
over_tmp2(over_tmp == 2) = 144;
over_tmp2(over_tmp == 3) = 208;
over_tmp_rgb(:,:,1) = over_tmp2;
over_tmp2 = over_tmp .* 0;
over_tmp2(over_tmp == 1) = 255;
over_tmp2(over_tmp == 2) = 144;
over_tmp2(over_tmp == 3) = 0;
over_tmp_rgb(:,:,2) = over_tmp2;
over_tmp2 = over_tmp .* 0;
over_tmp2(over_tmp == 1) = 102;
over_tmp2(over_tmp == 2) = 144;
over_tmp2(over_tmp == 3) = 0;
over_tmp_rgb(:,:,3) = over_tmp2;
col = zeros(3,3);
col(:,1) = [42, 253, 110];
col(:,2) = [252, 189, 65];
col(:,3) = [192, 7, 19];
over_tmp_rgb = ind2rgb(over_tmp, col);
else
% define jet-colors
over_tmp(over_tmp > .9) = .9;
%[X, ~] = gray2ind(over_tmp, 256);
X = round(over_tmp .* 255);
over_tmp_rgb = ind2rgb(X, hot(256));
end
% bring figure to front (pretend that figure is displayed in
% other figures that may have been clicked by the user)
figure(fid);
% make figure invisible
set(fid, 'visible', 'off')
%imshow(over_tmp_rgb);
imagesc(over_tmp_rgb);
axis off
over_tmp(over_tmp > 0) = 1;
over_tmp = over_tmp == 0;
img_tmp_gr = img_tmp;
hold on
%h = imshow(img_tmp_gr);
h = imagesc(img_tmp_gr); colormap(gray);
hold off
set(h, 'AlphaData', over_tmp);
res = 50; % pixels per inch
set(gcf, 'PaperUnits', 'inches', 'PaperPosition', [0 0 size(img_tmp_gr, 2) size(img_tmp_gr, 1)]/res);
%saveas(fid, fullfile(namePNG, ['overlay_', num2str(z_tmp), '.png']), 'png') % CHANGE!
saveas(gcf, fullfile(namePNG, ['overlay_', num2str(z_tmp), '.png']), 'png')
end
else
% Do all images have the same dimension?
dims = zeros(3, 3);
dims(end,:) = Vover.dim;
lcl = zeros(n, 1);
for i = 1:2
dims(i,:) = V(i).dim;
end
% Load images
over = spm_read_vols(Vover);
if any(or ~= [1 2 3]) && fl
over = permute(over, or);
dims = [size(over); size(over)];
end
if fl
if exist('flip', 'builtin')
over = flip(over, 2);
else
over = flipdim(over, 2);
end
end
img = zeros(dims(1,1), dims(1,2), dims(1,3), m);
for i = 1:m
tmp = spm_read_vols(V(i));
if any(or ~= [1 2 3])
tmp = permute(tmp, or);
end
if fl
if exist('flip', 'builtin')
tmp = flip(tmp, 2);
else
tmp = flipdim(tmp, 2);
end
end
tmp(tmp < 0) = 0;
tmp(isnan(tmp)) = 0;
tmp(:) = (tmp(:) ./ ps_quantile(tmp(:), .9999));
img(:,:,:,i) = tmp;
end
img(img > 1) = 1;
% Where to start?
if numel(r) == 1
c_tmp = indx2coord(find(img(:,:,:,1) > 0), size(img, 1), size(img, 2));
r = [min(c_tmp(:,3)), max(c_tmp(:,3))];
end
counter = 0;
for z = r(1):r(2)
z_tmp = r(2) - counter;
counter = counter + 1;
if exist('flip', 'builtin')
img_tmp = [flip(img(:,:,z_tmp,1)'), flip(img(:,:,z_tmp,2)'), flip(img(:,:,z_tmp,2)')];
over_tmp = [0 .* flip(over(:,:,z_tmp)'), 0 .* flip(over(:,:,z_tmp)'), 1 .* flip(over(:,:,z_tmp)')];
else
img_tmp = [flipdim(img(:,:,z_tmp,1)', 1), flipdim(img(:,:,z_tmp,2)', 1), flipdim(img(:,:,z_tmp,2)', 1)];
over_tmp = [0 .* flipdim(over(:,:,z_tmp)', 1), 0 .* flipdim(over(:,:,z_tmp)', 1), 1 .* flipdim(over(:,:,z_tmp)', 1)];
end
col = zeros(4,3);
col(1,:) = [0, 0, 0];
col(2,:) = [42, 253, 110];
col(3,:) = [252, 189, 65];
col(4,:) = [192, 7, 19];
%over_tmp_rgb = ind2rgb(gray2ind(over_tmp ./ 3, 4), col./255);
over_tmp_rgb = ind2rgb(uint8(over_tmp), col./255);
% bring figure to front (pretend that figure is displayed in
% other figures that may have been clicked by the user)
figure(fid);
% make figure invisible
set(fid, 'visible', 'off')
% plot image
%imshow(over_tmp_rgb);
imagesc(over_tmp_rgb);
axis off
over_tmp(over_tmp > 0) = 1;
over_tmp = over_tmp == 0;
% Add overlay
img_tmp_gr = img_tmp;
hold on
%h = imshow(img_tmp_gr);
h = imagesc(img_tmp_gr); colormap(gray);
hold off
% set transparency
set(h, 'AlphaData', over_tmp);
res = 50; % pixels per inch
set(gcf, 'PaperUnits', 'inches', 'PaperPosition', [0 0 size(img_tmp_gr, 2) size(img_tmp_gr, 1)]/res);
if nonewname
%saveas(h, [namePNG, '_', num2str(z_tmp), '.png'], 'png') % CHANGE!
saveas(gcf, [namePNG, '_', num2str(z_tmp), '.png'], 'png')
else
%saveas(h, ['overlay_', num2str(z_tmp), '.png'], 'png') % CHANGE!
saveas(gcf, ['overlay_', num2str(z_tmp), '.png'], 'png')
end
end
end
else
% Load images
img = spm_read_vols(V);
img(isnan(img)) = 0;
img(:) = (img(:) ./ ps_quantile(img(:), .9999));
img(img > 1) = 1;
if any(or ~= [1 2 3])
img = permute(img, [or, 4]);
end
if fl
if exist('flip', 'builtin')
img = flip(img, 2);
else
img = flipdim(img, 2);
end
end
% More than one image?
n = numel(V);
% Where to start?
if numel(r) == 1
if n > 1
c_tmp = indx2coord(find(img(:,:,:,1) > 0), size(img(:,:,:,1), 1), size(img(:,:,:,1), 2));
r = [min(c_tmp(:,3)), max(c_tmp(:,3))];
else
c_tmp = indx2coord(find(img > 0), size(img, 1), size(img, 2));
r = [min(c_tmp(:,3)), max(c_tmp(:,3))];
end
end
counter = 0;
for z = r(1):r(2)
z_tmp = r(2) - counter;
counter = counter + 1;
if n > 1
if exist('flip', 'builtin')
img_tmp = flip(img(:,:,z_tmp,1)');
for j = 2:n
img_tmp = [img_tmp, flip(img(:,:,z_tmp,j)')];
end
else
img_tmp = flipdim(img(:,:,z_tmp,1)', 1);
for j = 2:n
img_tmp = [img_tmp, flipdim(img(:,:,z_tmp,j)', 1)];
end
end
else
if exist('flip', 'builtin')
img_tmp = flip(img(:,:,z_tmp)');
else
img_tmp = flipdim(img(:,:,z_tmp)', 1);
end
end
img_tmp_gr = img_tmp;
% bring figure to front (pretend that figure is displayed in
% other figures that may have been clicked by the user)
figure(fid);
% make figure invisible
set(fid, 'visible', 'off')
%h = imshow(img_tmp_gr);
%h = imagesc(img_tmp_gr); colormap(gray); % CHANGE!
imagesc(img_tmp_gr); colormap(gray);
axis off
res = 50; % pixels per inch
set(gcf, 'PaperUnits', 'inches', 'PaperPosition', [0 0 size(img_tmp_gr, 2) size(img_tmp_gr, 1)]/res);
%saveas(h, fullfile(namePNG, ['overlay_', num2str(z_tmp), '.png']), 'png') % CHANGE!
saveas(gcf, fullfile(namePNG, ['overlay_', num2str(z_tmp), '.png']), 'png')
end
end
close(fid)
cd(orpth)
nameGIF = nameGIF((regexp(nameGIF, '/')+1):end);
end