-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextract_axial_slices.m
executable file
·183 lines (139 loc) · 5.77 KB
/
extract_axial_slices.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
close all
clear all
clc
study = 'Cambridge';
dirDataRoot = '/home/andek/Research_projects/fMRI_GAN/data/fcon1000/T1_fMRI/';
% Output folders
dirT1 = fullfile(dirDataRoot, 'T2F_T1_41');
dirfMRI = fullfile(dirDataRoot, 'T2F_fMRI_41');
if (~exist(dirT1, 'dir'))
mkdir(dirT1);
end
if (~exist(dirfMRI, 'dir'))
mkdir(dirfMRI);
end
% Slices to extract around the middle slice
selectedSlicesAroundMid = 1 * (-20:20); % 41 slices across the middle
% File lists
listT1 = dir(fullfile(dirDataRoot, [study '*T1*.nii.gz'])); % All subjects
listfMRI = dir(fullfile(dirDataRoot, [study '*fMRI*.nii.gz'])); % All subjects
maxVal = zeros(length(listT1),2);
medianStep = zeros(length(listT1),2);
quant095 = zeros(length(listT1),2);
quant0995 = zeros(length(listT1),2);
T1sizes = zeros(length(listT1),3);
% Read all volumes once, to get max sizes
for i = 1:length(listT1)
fileT1 = fullfile(dirDataRoot, listT1(i).name);
filefMRI = fullfile(dirDataRoot, listfMRI(i).name);
vol = double(niftiread(fileT1));
[sy sx sz] = size(vol);
T1sizes(i,:) = [sy sx sz];
end
maxSx = max(T1sizes(:,1));
maxSy = max(T1sizes(:,2));
% Set targetdim once
% (to avoid different sizes over subjects)
targetDim = ceil([maxSy maxSx]/4)*4;
% Now read volumes again
for i = 1:length(listT1)
fprintf('i = %d \n', i)
% T1 and fMRI
fileT1 = fullfile(dirDataRoot, listT1(i).name);
filefMRI = fullfile(dirDataRoot, listfMRI(i).name);
%% T1
% Load T1 data
vol = double(niftiread(fileT1));
maxVal(i,1) = max(vol(:));
quant095(i,1) = quantile(vol(vol~=0), 0.95);
quant0995(i,1) = quantile(vol(vol~=0), 0.995);
% Calculate center of mass
[sy sx sz] = size(vol);
T1sizes(i,:) = size(vol);
totalMass = sum(vol(:));
rx = 0; ry = 0; rz = 0;
for x = 1:sx
for y = 1:sy
for z = 1:sz
rx = rx + vol(y,x,z) * x;
ry = ry + vol(y,x,z) * y;
rz = rz + vol(y,x,z) * z;
end
end
end
rx = rx / totalMass;
ry = ry / totalMass;
rz = rz / totalMass;
midSliceZ = round(rz);
% Extract axial slices around the middle slice
slices = midSliceZ + selectedSlicesAroundMid;
for j = 1:length(slices)
im = squeeze(vol(:,:,slices(j)));
% Convert and rearrange dimensions
%im = uint8((im ./ maxVal(i,1)) .* 255);
im = uint8((im ./ quant0995(i,1)) .* 255);
im = rot90(im',2);
% Make sure that image size is divisible by 4
imOut = uint8(zeros(targetDim));
imOut(1:size(im,1), 1:size(im,2)) = im;
% Save image
imageIndex = (i-1)*length(slices) + j;
fileOut = fullfile(dirT1, ['im', num2str(imageIndex), '.png']);
imwrite(imOut, fileOut)
end
% Load fMRI data
vol = double(niftiread(filefMRI));
% Calculate center of mass
[sy sx sz] = size(vol);
fMRIsizes(i,:) = size(vol);
totalMass = sum(vol(:));
rx = 0; ry = 0; rz = 0;
for x = 1:sx
for y = 1:sy
for z = 1:sz
rx = rx + vol(y,x,z) * x;
ry = ry + vol(y,x,z) * y;
rz = rz + vol(y,x,z) * z;
end
end
end
rx = rx / totalMass;
ry = ry / totalMass;
rz = rz / totalMass;
midSliceZ = round(rz);
maxVal(i,2) = max(vol(:));
quant095(i,2) = quantile(vol(vol~=0), 0.95);
quant0995(i,2) = quantile(vol(vol~=0), 0.995);
% Extract axial slices around the middle slice
slices = midSliceZ + selectedSlicesAroundMid;
for j = 1:length(slices)
im = squeeze(vol(:,:,slices(j)));
% Convert and rearrange dimensions
% im = uint8((im ./ maxVal(i,2)) .* 255);
im = uint8((im ./ quant0995(i,2)) .* 255);
im = rot90(im',2);
% Make sure that image size is divisible by 4
imOut = uint8(zeros(targetDim));
imOut(1:size(im,1), 1:size(im,2)) = im;
% Save image
imageIndex = (i-1)*length(slices) + j;
fileOut = fullfile(dirfMRI, ['im', num2str(imageIndex), '.png']);
imwrite(imOut, fileOut)
end
end
system(sprintf('cp %s',[fullfile(dirDataRoot, 'T2F_T1_41/') 'im1* data/' study '-T2F-41/trainA' ] ))
system(sprintf('cp %s',[fullfile(dirDataRoot, 'T2F_T1_41/') 'im2* data/' study '-T2F-41/trainA' ] ))
system(sprintf('cp %s',[fullfile(dirDataRoot, 'T2F_T1_41/') 'im3* data/' study '-T2F-41/trainA' ] ))
system(sprintf('cp %s',[fullfile(dirDataRoot, 'T2F_T1_41/') 'im4* data/' study '-T2F-41/trainA' ] ))
system(sprintf('cp %s',[fullfile(dirDataRoot, 'T2F_T1_41/') 'im5* data/' study '-T2F-41/trainA' ] ))
system(sprintf('cp %s',[fullfile(dirDataRoot, 'T2F_T1_41/') 'im6* data/' study '-T2F-41/trainA' ] ))
system(sprintf('cp %s',[fullfile(dirDataRoot, 'T2F_T1_41/') 'im7* data/' study '-T2F-41/testA' ] ))
system(sprintf('cp %s',[fullfile(dirDataRoot, 'T2F_T1_41/') 'im8* data/' study '-T2F-41/testA' ] ))
system(sprintf('cp %s',[fullfile(dirDataRoot, 'T2F_fMRI_41/') 'im1* data/' study '-T2F-41/trainB' ] ))
system(sprintf('cp %s',[fullfile(dirDataRoot, 'T2F_fMRI_41/') 'im2* data/' study '-T2F-41/trainB' ] ))
system(sprintf('cp %s',[fullfile(dirDataRoot, 'T2F_fMRI_41/') 'im3* data/' study '-T2F-41/trainB' ] ))
system(sprintf('cp %s',[fullfile(dirDataRoot, 'T2F_fMRI_41/') 'im4* data/' study '-T2F-41/trainB' ] ))
system(sprintf('cp %s',[fullfile(dirDataRoot, 'T2F_fMRI_41/') 'im5* data/' study '-T2F-41/trainB' ] ))
system(sprintf('cp %s',[fullfile(dirDataRoot, 'T2F_fMRI_41/') 'im6* data/' study '-T2F-41/trainB' ] ))
system(sprintf('cp %s',[fullfile(dirDataRoot, 'T2F_fMRI_41/') 'im7* data/' study '-T2F-41/testB' ] ))
system(sprintf('cp %s',[fullfile(dirDataRoot, 'T2F_fMRI_41/') 'im8* data/' study '-T2F-41/testB' ] ))