/
warp_structures.m
196 lines (169 loc) · 7.65 KB
/
warp_structures.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
function planC = warp_structures(deformS,strCreationScanNum,movStructNumsV,movPlanC,planC, tmpDirPath, inverseFlag)
% function planC = warp_structures(deformS,strCreationScanNum,movStructNumsV,movPlanC,planC)
%
% APA, 07/20/2012
if ~exist('inverseFlag','var')
inverseFlag = 0;
end
if ~exist(tmpDirPath,'var') || isempty(tmpDirPath)
tmpDirPath = fullfile(getCERRPath,'ImageRegistration','tmpFiles');
end
indexMovS = movPlanC{end};
optS = getCERROptions();
antsFlag = 0; plmFlag = 0; elxFlag = 0;
if isstruct(deformS)
if ~inverseFlag
baseScanUID = deformS.baseScanUID;
movScanUID = deformS.movScanUID;
else
baseScanUID = deformS.movScanUID;
movScanUID = deformS.baseScanUID;
end
algorithm = deformS.algorithm;
registration_tool = deformS.registrationTool;
switch upper(registration_tool)
case 'PLASTIMATCH'
plmFlag = 1;
disp('Plastimatch selected');
bspFileName = fullfile(getCERRPath,'ImageRegistration',...
'tmpFiles',['bsp_coeffs_',baseScanUID,'_',movScanUID,'.txt']);
success = write_plm_transform_file(algorithm,bspFileName,...
deformS.algorithmParamsS);
%success = write_bspline_coeff_file(bspFileName,deformS.algorithmParamsS);
case 'ELASTIX'
elxFlag = 1;
disp('Elastix selected');
case 'ANTS'
antsFlag = 1;
disp('ANTs selected');
end
else
% Create b-spline coefficients file
plmFlag = 1;
bspFileName = deformS;
indexS = planC{end};
indexMovS = movPlanC{end};
movScanNum = getStructureAssociatedScan(movStructNumsV(1),movPlanC);
movScanUID = movPlanC{indexMovS.scan}(movScanNum).scanUID;
baseScanUID = planC{indexS.scan}(strCreationScanNum).scanUID;
end
%% Convert basePlanC scan & mask(s) to .mha file
if iscell(planC)
refScanNum = findScanByUID(planC,baseScanUID);
else
refScanNum = 1;
end
[refScanUniqName, ~] = genScanUniqName(planC,refScanNum);
if ischar(planC)
refScanFileName = fullfile(tmpDirPath, ['baseScan_' refScanUniqName '.' baseext basegz]);
copyfile(basePlanC, refScanFileName);
else
refScanFileName = fullfile(tmpDirPath,['baseScan_',refScanUniqName,'.mha']);
success = createMhaScansFromCERR(refScanNum, refScanFileName, planC);
end
% Generate name for the output .mha file
warpedMhaFileName = fullfile(tmpDirPath,['warped_struct_',baseScanUID,'_',movScanUID,'.mha']);
if antsFlag
if ~exist(optS.antspath_dir,'dir')
error(['ANTSPATH ' optS.antspath_dir ' not found on filesystem. Please review CERROptions.']);
end
antspath = fullfile(optS.antspath_dir,'bin');
setenv('ANTSPATH', antspath);
antsScriptPath = fullfile(optS.antspath_dir, 'Scripts');
antsCERRScriptPath = fullfile(getCERRPath,'CERR_core','ImageRegistration','antsScripts');
if isunix
setenv('PATH',[antspath ':' antsScriptPath ':' antsCERRScriptPath ':' getenv('PATH')]);
else
setenv('PATH',[antspath ';' antsScriptPath ';' antsCERRScriptPath ';' getenv('PATH')]);
end
transformParams = buildAntsTransform(deformS.algorithmParamsS.antsWarpProducts,inverseFlag);
interpParams = ' -n NearestNeighbor';
for structNum = movStructNumsV
mask3M = getUniformStr(structNum,movPlanC);
mask3M = permute(mask3M, [2 1 3]);
mask3M = flipdim(mask3M,3);
movStrUID = movPlanC{indexMovS.structures}(structNum).strUID;
randPart = floor(rand*1000);
movStrUniqName = [movStrUID,num2str(randPart)];
movStrFileName = fullfile(tmpDirPath, ['movStr_',movStrUniqName,'.mha']);
scanNum = getStructureAssociatedScan(structNum,movPlanC);
[xVals, yVals, zVals] = getUniformScanXYZVals(movPlanC{indexMovS.scan}(scanNum));
resolution = [abs(xVals(2)-xVals(1)), abs(yVals(2)-yVals(1)), abs(zVals(2)-zVals(1))] * 10;
offset = [xVals(1) -yVals(1) -zVals(end)] * 10;
% Write .mha file for this structure
writemetaimagefile(movStrFileName, mask3M, resolution, offset)
antsCommand = [' antsApplyTransforms -d 3 -r ' refScanFileName ' -i ' movStrFileName ' -o ' warpedMhaFileName ' ' transformParams ' ' interpParams];
disp(antsCommand);
system(antsCommand);
disp('Warp complete, importing warped scan to planC...');
% [data3M,infoS] = readmha(warpedMhaFileName);
infoS = mha_read_header(warpedMhaFileName);
data3M = mha_read_volume(infoS);
data3M = flipdim(permute(data3M,[2,1,3]),3);
isUniform = 1;
strName = ['Warped_',movPlanC{indexMovS.structures}(structNum).structureName];
planC = maskToCERRStructure(data3M,isUniform,strCreationScanNum,strName,planC);
% Cleanup
try
delete(movStrFileName)
delete(warpedMhaFileName)
end
end
end
if plmFlag
% Switch to plastimatch directory if it exists
prevDir = pwd;
plmCommand = 'plastimatch warp ';
optName = fullfile(getCERRPath,'CERROptions.json');
optS = opts4Exe(optName);
if exist(optS.plastimatch_build_dir,'dir') && isunix
cd(optS.plastimatch_build_dir)
plmCommand = ['./',plmCommand];
end
for structNum = movStructNumsV
% Convert structure mask to .mha
mask3M = getUniformStr(structNum,movPlanC);
mask3M = permute(mask3M, [2 1 3]);
mask3M = flipdim(mask3M,3);
movStrUID = movPlanC{indexMovS.structures}(structNum).strUID;
randPart = floor(rand*1000);
movStrUniqName = [movStrUID,num2str(randPart)];
movStrFileName = fullfile(getCERRPath,'ImageRegistration','tmpFiles',['movStr_',movStrUniqName,'.mha']);
scanNum = getStructureAssociatedScan(structNum,movPlanC);
[xVals, yVals, zVals] = getUniformScanXYZVals(movPlanC{indexMovS.scan}(scanNum));
resolution = [abs(xVals(2)-xVals(1)), abs(yVals(2)-yVals(1)), abs(zVals(2)-zVals(1))] * 10;
offset = [xVals(1) -yVals(1) -zVals(end)] * 10;
% Write .mha file for this structure
writemetaimagefile(movStrFileName, mask3M, resolution, offset)
% Issue plastimatch warp command with nearest neighbor interpolation
fail = system([plmCommand, '--input ', movStrFileName, ...
' --output-img ', warpedMhaFileName, ' --xf ', bspFileName,...
' --interpolation nn', ' --fixed ', refScanFileName]);
if fail % try escaping slashes
system([plmCommand, '--input ', escapeSlashes(movStrFileName),...
' --output-img ', escapeSlashes(warpedMhaFileName),...
' --xf ', escapeSlashes(bspFileName), ' --interpolation nn',...
' --fixed ', escapeSlashes(refScanFileName)])
end
% Read the warped output .mha file within CERR
%infoS = mha_read_header(warpedMhaFileName);
%data3M = mha_read_volume(infoS);
[data3M,infoS] = readmha(warpedMhaFileName);
data3M = flipdim(permute(data3M,[2,1,3]),3);
isUniform = 1;
strName = ['Warped_',movPlanC{indexMovS.structures}(structNum).structureName];
planC = maskToCERRStructure(data3M,isUniform,strCreationScanNum,strName,planC);
% Cleanup
try
delete(movStrFileName)
delete(warpedMhaFileName)
end
end
try
if isstruct(deformS)
delete(bspFileName)
end
end
% Switch back to the previous directory
cd(prevDir)
end