-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGenerateDRRsUnetMultiVol.m
309 lines (220 loc) · 10.1 KB
/
GenerateDRRsUnetMultiVol.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
function GenerateDRRsUnetMultiVol(VolDir,OutputDir)
%% GenerateDRRsUnetMultiVol(BaseFile)
% ------------------------------------------
% FILE : GenerateDRRsUnetMultiVol.m
% AUTHOR : Mark Gardner, The University of Sydney
% DATE : 2022-09-12 Created.
% ------------------------------------------
% PURPOSE
% Convert mulitple mha volumes into images that can be used as
% training/testing data for deep-learning networks.
% ------------------------------------------
% INPUT
% VolumeDir: Directory where the generated volumes are located.
% OutputDir: Directory where the generated images and supporting data
% files will be written to.
%% Check inputs
if nargin < 1
VolDir = uigetdir(matlabroot,'Select the directory where the input .mha volumes are located');
end
if isempty(VolDir) || ~exist(VolDir,'dir')
error(['Directory does not exist: ',VolDir])
end
if nargin < 2
OutputDir = fullfile(VolDir,'TrainingData');
end
%% Define initial variables
close('all')
VolumeNum = 15; %Number of volumes created, 11 training plus 4 test volumes
angles = 0:0.1:359.9; %Projection angle of the DRRs created
imageSize = [550 550 1]; %Size of images being created
var_gauss = 0.00001; %Variance of the gaussian noise added.
ProjOffset = 0; %X-offset of detector. Change to simulate half-fan geometry
ReconDir = fullfile(OutputDir,'Recon');
if ~exist(ReconDir,'dir')
mkdir(ReconDir)
end
DRRDir = fullfile(OutputDir,'DRRs');
if ~exist(DRRDir,'dir')
mkdir(DRRDir)
end
%% Create geometry files
geoFile = fullfile(DRRDir,'GeometryGAN1.xml');
if ~exist(geoFile,'file')
geoFile1 = fullfile(DRRDir,'GeometryGAN1.xml');
system(['igtsimulatedgeometry -f 0 --arc 180 -n ',num2str(numel(angles)/2),' -o "',geoFile1,'"',...
' --proj_iso_x ',num2str(ProjOffset)])
geoFile2 = fullfile(DRRDir,'GeometryGAN2.xml');
system(['igtsimulatedgeometry -f 180 --arc 180 -n ',num2str(numel(angles)/2),' -o "',geoFile2,'"',...
' --proj_iso_x ',num2str(ProjOffset)])
end
%% Shift volumes so centre of tumour is centre pixel
CTFile = fullfile(VolDir,'CTVol01.mha');
CTShifted = fullfile(ReconDir,'CTShifted.mha');
if ~exist(CTShifted,'file')
MaskVolFile = fullfile(VolDir,'GTVVol01.mha');
[MaskVolHeader,MaskVol] = MhaRead(MaskVolFile);
[r,c,v] = ind2sub(size(MaskVol),find(MaskVol > 0));
DiffR = (median(r) - MaskVolHeader.Dimensions(1)/2);%*MaskVolHeader.PixelDimensions(1);
DiffC = (median(c) - MaskVolHeader.Dimensions(2)/2);%*MaskVolHeader.PixelDimensions(2);
DiffV = (median(v) - MaskVolHeader.Dimensions(3)/2);%*MaskVolHeader.PixelDimensions(3);
[CTHeader,CTVol] = MhaRead(CTFile);
CTVol2 = NonCircularShift(CTVol,DiffR,DiffC,DiffV);
MhaWrite(CTHeader,CTVol2,CTShifted)
end
MaskVolFile = fullfile(ReconDir,'GTVShifted.mha');
if ~exist(MaskVolFile,'file')
[GTVHeader,GTVVol] = MhaRead(fullfile(VolDir,'GTVVol01.mha'));
GTVVol2 = NonCircularShift(GTVVol,DiffR,DiffC,DiffV);
MhaWrite(GTVHeader,GTVVol2,MaskVolFile)
end
%% Create DRRs and convert DRRs to pictures for first volume
if ~exist(fullfile(DRRDir,'Vol01DRR1.mha'),'file')
DRRFile = fullfile(DRRDir,'Vol01DRR.mha');
DoForwardProjection(CTShifted,DRRFile,DRRDir)
Projs2Pics(DRRFile,fullfile(OutputDir,'DRRImg01'))
end
if ~exist(fullfile(DRRDir,'GTVVol01DRR1.mha'),'file')
TumourDRRFile = fullfile(DRRDir,'GTVVol01DRR.mha');
[CTHeader,CTVol] = MhaRead(CTShifted);
%MaskVolFile = fullfile(BaseFile,'ContourMasks','GTVShifted.mha');
MaskVolFile = fullfile(ReconDir,'GTVShifted.mha');
[~,MaskVol] = MhaRead(MaskVolFile);
CTVol2 = CTVol;
CTVol2(~MaskVol) = 0;
MhaWrite(CTHeader,CTVol2,fullfile(ReconDir,'GTVVol01.mha'));
DoForwardProjection(fullfile(ReconDir,'GTVVol01.mha'),TumourDRRFile,DRRDir)
Projs2Pics(TumourDRRFile,fullfile(OutputDir,'MaskImg01'))
end
%% Repeat for all other volumes
for j = 2:VolumeNum
CTFileLoop = fullfile(VolDir,num2str(j,'CTVol%02d.mha'));
GTVFileLoop = fullfile(VolDir,num2str(j,'GTVVol%02d.mha'));
CTShiftedLoop = fullfile(VolDir,num2str(j,'CTVol%02dShifted.mha'));
GTVShiftedLoop = fullfile(VolDir,num2str(j,'GTVVol%02dShifted.mha'));
if ~exist(CTShiftedLoop,'file')
[CTHeader,CTVol] = MhaRead(CTFileLoop);
CTVol2 = NonCircularShift(CTVol,DiffR,DiffC,DiffV);
if j >= VolumeNum-1
CTVol2 = imnoise(CTVol2,'gaussian',0,var_gauss);
end
MhaWrite(CTHeader,CTVol2,CTShiftedLoop)
end
if ~exist(GTVShiftedLoop,'file')
[CTHeader,CTVol] = MhaRead(GTVFileLoop);
CTVol2 = NonCircularShift(CTVol,DiffR,DiffC,DiffV);
MhaWrite(CTHeader,CTVol2,GTVShiftedLoop)
end
if ~exist(fullfile(DRRDir,num2str(j,'Vol%02dDRR1.mha')),'file')
DRRFile = fullfile(DRRDir,num2str(j,'Vol%02dDRR.mha'));
DoForwardProjection(CTShiftedLoop,DRRFile,DRRDir)
Projs2Pics(DRRFile,fullfile(OutputDir,num2str(j,'DRRImg%02d')))
end
if ~exist(fullfile(DRRDir,num2str(j,'GTVVol%02dDRR1.mha')),'file')
TumourDRRFile = fullfile(DRRDir,num2str(j,'GTVVol%02dDRR.mha'));
[CTHeader,CTVol] = MhaRead(CTShifted);
[~,MaskVol] = MhaRead(GTVShiftedLoop);
CTVol2 = CTVol;
CTVol2(~MaskVol) = 0;
MhaWrite(CTHeader,CTVol2,fullfile(ReconDir,num2str(j,'GTVVol%02d.mha')));
DoForwardProjection(fullfile(ReconDir,num2str(j,'GTVVol%02d.mha')),TumourDRRFile,DRRDir)
Projs2Pics(TumourDRRFile,fullfile(OutputDir,num2str(j,'MaskImg%02d')))
end
end
%% Convert jp2 ims to final im
FinalDir = fullfile(OutputDir,'DataSet','train');
if ~exist(FinalDir,'dir')
mkdir(FinalDir)
end
filter_N = 3;
a = 1;
b = ones(filter_N,1)./filter_N;
for j = 1:VolumeNum
imdsDirectory = fullfile(OutputDir,num2str(j,'DRRImg%02d'));
pxdsDirectory = fullfile(OutputDir,num2str(j,'MaskImg%02d'));
[Images, ~] = Read_jp2(imdsDirectory);
[Masks, ~] = Read_jp2(pxdsDirectory);
if ~HalfFan
Images = Images(768/2 - imageSize(1)/2 +1: 768/2 + imageSize(1)/2 ,...
1024/2 - imageSize(2)/2 +1: 1024/2 + imageSize(2)/2,:);
Masks = Masks(768/2 - imageSize(1)/2 +1 : 768/2 + imageSize(1)/2,...
1024/2 - imageSize(2)/2 +1 : 1024/2 + imageSize(2)/2 ,:);
else
Images = Images(1:imageSize(1) ,...
768/2 - imageSize(2)/2 +1: 768/2 + imageSize(2)/2,:);
Masks = Masks(1:imageSize(1),...
768/2 - imageSize(2)/2 +1 : 768/2 + imageSize(2)/2 ,:);
end
for i = 1:numel(Images(1,1,:))
Images(:,:,i) = filter(b, a, Images(:,:,i));
end
for im = 1:size(Images,3)
I = Images(:,:,im);
M = Masks(:,:,im);
I = double(I);
I = (I - min(I(:)))/(max(I(:)) - min(I(:)));
M = double(M);
M = (M - min(M(:)))/(max(M(:)) - min(M(:)));
I = im2uint16(I);
M = im2uint16(M);
joined = [I M];
fullFileName = fullfile(FinalDir,num2str([im,j],'CombineMaskDRR%04d_%02d.png'));
imwrite(joined, fullFileName, 'BitDepth', 16);
end
end
%Automatically assign the data to training and testing
AssignTrainingData(fullfile(OutputDir,'DataSet'))
end
function DoForwardProjection(InputVol,OutputFile,OutputDir)
% Setup the forward projections using rtkforwardprojections.exe.
% Split into two seperate functions to save memory on the computer.
geoFile1 = fullfile(OutputDir,'GeometryGAN1.xml');
geoFile2 = fullfile(OutputDir,'GeometryGAN2.xml');
[~,name,ext] = fileparts(OutputFile);
OutputTemp1 = fullfile(OutputDir,[name,'1',ext]);
OutputTemp2 = fullfile(OutputDir,[name,'2',ext]);
system(['rtkforwardprojections -g "',geoFile1,'" -i "',InputVol,'" -o "', OutputTemp1,'"',...
' -f CudaRayCast --spacing 0.388 --dimension 768,1024'])
system(['rtkforwardprojections -g "',geoFile2,'" -i "',InputVol,'" -o "', OutputTemp2,'"',...
' -f CudaRayCast --spacing 0.388 --dimension 768,1024'])
end
function[Vol2] = NonCircularShift(Vol,DiffC,DiffR,DiffV)
% Shifts the volumes
DiffCInt = round(DiffC);
DiffRInt = round(DiffR);
DiffVInt = round(DiffV);
Vol2 = zeros(size(Vol),'like',Vol);
if DiffRInt < 0
RInds2 = abs(DiffRInt)+1:size(Vol,1);
RInds = 1:size(Vol,2)-abs(DiffRInt);
elseif DiffRInt > 0
RInds2 = 1:size(Vol,2)-DiffRInt;
RInds = DiffRInt+1:size(Vol,2);
else
RInds2 = 1:size(Vol,2);
RInds = 1:size(Vol,2);
end
if DiffCInt < 0
CInds2 = abs(DiffCInt)+1:size(Vol,1);
CInds = 1:size(Vol,1)-abs(DiffCInt);
elseif DiffCInt > 0
CInds2 = 1:size(Vol,1)-DiffCInt;
CInds = DiffCInt+1:size(Vol,1);
else
CInds2 = 1:size(Vol,1);
CInds = 1:size(Vol,1);
end
if DiffVInt < 0
VInds2 = abs(DiffVInt)+1:size(Vol,3);
VInds = 1:size(Vol,3)-abs(DiffVInt);
elseif DiffVInt > 0
VInds2 = 1:size(Vol,3)-DiffVInt;
VInds = DiffVInt+1:size(Vol,3);
else
VInds2 = 1:size(Vol,1);
VInds = 1:size(Vol,1);
end
for i = 1:numel(RInds)
Vol2(CInds2,RInds2(i),VInds2) = Vol(CInds,RInds(i),VInds);
end
end