-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathsitkWrapper.m
278 lines (221 loc) · 10.1 KB
/
sitkWrapper.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
function filteredOutS = sitkWrapper(sitkLibPath, scanM, filterType, paramS)
%function filteredOutS = sitkWrapper(sitkLibPath, scanM, filterType, paramS, planC)
%Calculate image filters using the Simple ITK Python Library
%sitkLibPath - location of sitk python wrappscan to be filtered
%description - Short description string if desired.
%filterType - name of sitk filter
%paramS: parameters required to calculate the filter
%planC: to convert scan back to cerr
%
% example usage:
% filterType = 'GradientImageFilter';
% paramS.useImageSpacing = false;
% paramS.useImageDirection = true;
% sitkLibPath = 'C:\Python34\Lib\site-packages\SimpleITK\';
% planC = loadPlanC(cerrFileName,tempdir);
% planC = updatePlanFields(planC);
% % Quality assure
% planC = quality_assure_planC(cerrFileName,planC);
% indexS = planC{end};
% % Get Scan
% scanNum =1;
% scanM = double(planC{indexS.scan}(scanNum).scanArray) ...
% - planC{indexS.scan}(scanNum).scanInfo(1).CTOffset;%
%
% sitkWrapper(sitkLibPath, scanM, filterType, paramS)
%
%
% Rutu Pandya, Dec, 16 2019.
%
% Copyright 2010, Joseph O. Deasy, on behalf of the CERR development team.
%
% This file is part of The Computational Environment for Radiotherapy Research (CERR).
%
% CERR development has been led by: Aditya Apte, Divya Khullar, James Alaly, and Joseph O. Deasy.
%
% CERR has been financially supported by the US National Institutes of Health under multiple grants.
%
% CERR is distributed under the terms of the Lesser GNU Public License.
%
% This version of CERR is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% CERR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
% without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
% See the GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with CERR.
% import python module SimpleITK
sitkModule = 'SimpleITK';
numpyModule = 'numpy';
%p = pyenv;
P = py.sys.path;
currentPath = pwd;
cd(sitkLibPath);
sitkFileName = fullfile(sitkLibPath,sitkModule);
numpyFileName = fullfile(sitkLibPath,numpyModule);
try
if count(P,numpyFileName) == 0
insert(P,int32(0),numpyFileName);
end
py.importlib.import_module(numpyModule)
if count(P,sitkFileName) == 0
insert(P,int32(0),sitkFileName);
end
py.importlib.import_module(sitkModule)
catch
disp('SimpleITK module could not be imported, check the path');
end
cd(currentPath);
%origScanSize = size(scanM);
% visualize original scan
%slc = 50;
%figure, imagesc(scanM(:,:,50)), title('orig Image')
% % convert scan to numpy array and integer
% scanPy = py.numpy.array(scanM(:).');
% scanPy = scanPy.astype(py.numpy.int64);
%
% % get original shape of the scan
% origShape = py.numpy.array(size(scanM));
% origShape = origShape.astype(py.numpy.int64);
switch filterType
case 'GradientImageFilter'
% paramS inputs needed:
% useImageSpacing (bool), true by default
% useImageDirection (bool), true by default
% convert scan to numpy array and integer
scanPy = py.numpy.array(scanM);
scanPy = scanPy.astype(py.numpy.float32);
% get original shape of the scan
%origShape = py.numpy.array(size(scanM));
%origShape = origShape.astype(py.numpy.int64);
% reshape numpy array to original shape
%scanPy = reshape(scanPy,origShape);
% Get image from the array
itkimg = py.extra.GetImageFromArray(scanPy);
% calculate gradient
gradient = py.SimpleITK.GradientImageFilter();
if(paramS.useImageSpacing == false)
gradient.SetUseImageSpacing(paramS.useImageSpacing);
end
if(paramS.useImageDirection == false)
gradient.SetUseImageDirection(paramS.useImageDirection);
end
gradImg = gradient.Execute(itkimg);
% extract numpy array from resulting image
npGradImg = py.extra.GetArrayFromImage(gradImg);
% convert resulting numpy array to matlab array in required shape
%dblGradResultM = double(py.array.array('d',py.numpy.nditer(npGradImg)));
%gradMatM = reshape(dblGradResultM,[3,origScanSize]);
gradMatM = double(npGradImg);
gradMatM = permute(gradMatM,[2,3,4,1]);
% %visualize
% size(gradMatM)
% figure, imagesc(gradMatM(:,:,50,1))
% figure, imagesc(gradMatM(:,:,50,2))
% figure, imagesc(gradMatM(:,:,50,3))
filteredOutS.xGradient = gradMatM(:,:,:,1);
filteredOutS.yGradient = gradMatM(:,:,:,2);
filteredOutS.zGradient = gradMatM(:,:,:,3);
case 'LaplacianRecursiveGaussianImageFilter'
% convert scan to numpy array and integer
% scanPy = py.numpy.array(scanM(:).');
scanPy = py.numpy.array(scanM);
scanPy = scanPy.astype(py.numpy.float32);
% get original shape of the scan
%origShape = py.numpy.array(size(scanM));
%origShape = origShape.astype(py.numpy.int64);
% reshape numpy array to original shape
%scanPy = reshape(scanPy,origShape);
% Get image from the array
itkimg = py.extra.GetImageFromArray(scanPy);
% Set Image spacing
zSpacing = paramS.VoxelSize_mm.val(3);
xSpacing = paramS.VoxelSize_mm.val(1);
ySpacing = paramS.VoxelSize_mm.val(2);
itkimg.SetSpacing([zSpacing, ySpacing, xSpacing]);
% calculate gradient
logRecursiveFilt = py.SimpleITK.LaplacianRecursiveGaussianImageFilter();
logRecursiveFilt.SetNormalizeAcrossScale(true)
sigmaVal = paramS.Sigma_mm.val;
logRecursiveFilt.SetSigma(sigmaVal);
% Execute the filter
logImg = logRecursiveFilt.Execute(itkimg);
% extract numpy array from resulting image
npLogImg = py.extra.GetArrayFromImage(logImg);
% convert resulting numpy array to matlab array in required shape
%dblLogResultM = double(py.array.array('d',py.numpy.nditer(npLogImg)));
%logMatM = reshape(dblLogResultM,origScanSize);
logMatM = double(npLogImg);
filteredOutS.logImg3M = logMatM;
case 'HistogramMatchingImageFilter'
% paramS inputs needed:
% numHistLevel (int), paramS.numMatchPts (int),
% ThresholdAtMeanIntensityOn (bool),
% refImgPath (char vector)
% convert scan to numpy array and integer
scanPy = py.numpy.array(scanM);
scanPy = scanPy.astype(py.numpy.float32);
% get original shape of the scan
%origShape = py.numpy.array(size(scanM(:)'));
%origShape = origShape.astype(py.numpy.int64);
% reshape numpy array to original shape
%scanPy = reshape(scanPy,origShape);
% Get image from the array
srcItkImg = py.extra.GetImageFromArray(scanPy);
% srcItkImg = py.SimpleITK.ReadImage('E:\data\TumorAware_MR\nrrdScanFormat.nrrd');
% get ref image
refImgPath = fullfile(getCERRPath,'ModelImplementationLibrary/SegmentationModels/referenceImages');
if isfield(paramS,'refImg') && ~isempty(paramS.refImg)
refImgPath = fullfile(refImgPath,paramS.refImg);
refItkImg = py.extra.ReadImage(refImgPath);
refItkImg = py.extra.Cast(refItkImg,py.SimpleITK.sitkFloat32);
%Adjust to RTOG-compliant orientation
refScanPy = py.extra.GetArrayFromImage(refItkImg);
refScan3M = single(refScanPy);
refScan3M = permute(refScan3M,[2,3,1]);
refScan3M = flip(flip(refScan3M,1),2);
refScan3M = flip(refScan3M,3);
refScanPy = py.numpy.array(refScan3M);
refItkImg = py.extra.GetImageFromArray(refScanPy);
elseif isfield(paramS,'refImgMat') && ~isempty(paramS.refImgMat)
refScanPy = py.numpy.array(paramS.refImgMat);
refScanPy = refScanPy.astype(py.numpy.float32);
% Get image from the array
refItkImg = py.extra.GetImageFromArray(refScanPy);
else
error('Reference image not specified for histogram matching')
end
%refNumElems = int64(py.numpy.prod(refScanPy.shape));
%refShape = py.numpy.array([1,refNumElems]);
%refScanPy = refScanPy.astype(py.numpy.int64);
% reshape numpy array to original shape
%refScanPy = reshape(refScanPy,refShape);
%refItkImg = py.SimpleITK.reshape(refItkImg, scanPy);
% execute Histogram Matching
matcher = py.SimpleITK.HistogramMatchingImageFilter();
matcher.SetNumberOfHistogramLevels(uint32(paramS.numHistLevel));
matcher.SetNumberOfMatchPoints(uint32(paramS.numMatchPts));
if(paramS.thresholdAtMeanIntensityOn)
matcher.ThresholdAtMeanIntensityOn();
end
matchedImg = matcher.Execute(srcItkImg,refItkImg);
% extract numpy array from resulting image
npHistImg = py.extra.GetArrayFromImage(matchedImg);
% convert resulting numpy array to matlab array in required shape
%dblHistResultM = double(py.array.array('d',py.numpy.nditer(npHistImg)));
%histMatM = reshape(dblHistResultM,[origScanSize(1),origScanSize(2),origScanSize(3)]);
histMatM = double(npHistImg);
filteredOutS.histMatchedImage = histMatM;
% %visualize
% size(histMatM)
% figure, imagesc(histMatM(:,:,slc,1))
% figure, imagesc(histMatM(:,:,slc,2))
% figure, imagesc(histMatM(:,:,slc,3))
otherwise
msgStr = [filterType,' not defined. Add it to sitkWrapper.m'];
error(msgStr)
end