-
Notifications
You must be signed in to change notification settings - Fork 0
/
Structure.py
443 lines (374 loc) · 16.6 KB
/
Structure.py
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# -*- coding: utf-8 -*-
import logging
import vtk
from vtk.util import numpy_support
import numpy as np
import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
# own modules
from dcmreader import Contour
from geom import Voxel
class Structure:
def __init__(self, dcm, structure):
""" readin in structure and create polydata according to
https://github.com/malaterre/GDCM/blob/master/Utilities/VTK/vtkGDCMPolyDataReader.cxx
"""
self.logger = logging.getLogger("Structure")
self.index = structure["index"]
self.name = structure["name"]
self.logger.info("Load structure " + self.name)
self.debug = []
cnts_slices, cnts = self.readContourData(dcm)
if self.debug == []:
self.slices = cnts_slices[:]
self.slices = [round(x, 2) for x in self.slices]
pts_subcontours = []
voxels = []
for iSlice in range(len(self.slices)):
for iSubContour in range(1, len(cnts[iSlice])): # first entry is the slice position
pts_subcontours.append([iSlice, iSubContour, len(cnts[iSlice][iSubContour][0])])
for iPoint in range(len(cnts[iSlice][iSubContour][0])):
voxels.append([round(cnts[iSlice][iSubContour][0][iPoint], 2),
round(cnts[iSlice][iSubContour][1][iPoint], 2),
self.slices[iSlice]])
# unreasonable reasons but some times there are CTs with different resolution
uniques = list(set(abs(np.round(np.ediff1d(np.array(self.slices)), 3))))
uniques.sort()
if len(uniques) > 1:
# check for missing slices using modulo:
mod_result = [round(uniques[x] % uniques[0], 1) for x in range(1, len(uniques))]
if np.array(mod_result).any():
self.logger.info("Unique Slices \t\t%s", uniques)
self.debug.append(["Contour slices at different resolution ", uniques])
self.voxels = voxels
self.pts_subcontours = pts_subcontours
self.createPolyData()
self.getExtent()
self.centerOfMass = self.calculateCenterofMass()
def createPolyPoints(self):
""" creates a poly points object
Arguments:
None
Returns:
None"""
appendFilter = vtk.vtkAppendPolyData()
beforeSlice = -1
countPoints = 0
for iSlice, iSubContour, nPoints in self.pts_subcontours:
vertexIndices = np.where(np.array(self.voxels)[:,2] == self.slices[iSlice])[0]
if iSlice != beforeSlice:
countPoints = 0
beforeSlice = iSlice
subContour = Contour()
subContour.createPolyPoints(vertexIndices[countPoints:countPoints + nPoints], self.voxels)
appendFilter.AddInputData(subContour.polyData)
countPoints += nPoints
appendFilter.Update()
contours = appendFilter.GetOutput()
poly = vtk.vtkPolyData()
points = vtk.vtkPoints()
contourPoints = contours.GetPoints()
numPoints = contourPoints.GetNumberOfPoints()
points.SetNumberOfPoints(numPoints);
for i in range(numPoints):
pt = list(contourPoints.GetPoint(i))
points.SetPoint(i, pt);
poly.SetPolys(contours.GetPolys())
poly.SetPoints(points)
self.polyData = poly # self.convertToLines(poly)
self.modifiedPolyData = self.polyData
self.savePolyData(r"K:\RAO_Physik\Research\1_FUNCTIONAL IMAGING\7_SAKK_Lung_Study\P4_spatial_distribution\results")
self.calculateCenterofMass()
self.logger.info("Center of Mass %s", self.centerOfMass.getRounds())
def savePolyData(self, outPath):
""" save poly data as vtp file
Arguments:
outPath path of the output
Returns:
None
"""
polyDataWriter = vtk.vtkXMLPolyDataWriter()
polyDataWriter.SetInputData(self.modifiedPolyData)
polyDataWriter.SetFileName(outPath + "\\" + self.name + ".vtp")
polyDataWriter.SetCompressorTypeToNone()
polyDataWriter.SetDataModeToAscii()
polyDataWriter.Write()
def createMask(self, res, origin, dim):
""" creates a binary mask array from structure using vtk module
Arguments:
res image resolution
origin image origin
dim image dimensions
Returns:
None
"""
self.logger.info("Init empty mask")
dim = dim.getArray()
mask = vtk.vtkImageData()
mask.SetSpacing(res.getArray())
mask.SetOrigin(origin.getArray())
mask.SetDimensions(dim)
mask.SetExtent(0, dim[0]-1, 0, dim[1]-1, 0, dim[2]-1)
mask.AllocateScalars(vtk.VTK_UNSIGNED_CHAR,1);
count = mask.GetNumberOfPoints()
for i in range(count):
mask.GetPointData().GetScalars().SetTuple1(i, 1)
self.logger.info("extruding")
extruder = vtk.vtkLinearExtrusionFilter()
extruder.SetInputData(self.modifiedPolyData)
extruder.SetExtrusionTypeToNormalExtrusion()
extruder.CappingOn(); # capp on top
extruder.SetVector(res.getArray());
extruder.Update();
self.logger.info("stencil mask")
# Just get the outer extent
dataToStencil = vtk.vtkPolyDataToImageStencil()
dataToStencil.SetInputConnection(extruder.GetOutputPort()) # stripper
dataToStencil.SetTolerance(1e-3)
dataToStencil.SetInformationInput(mask)
dataToStencil.SetOutputSpacing(res.getArray())
dataToStencil.SetOutputOrigin(origin.getArray())
dataToStencil.Update()
stencil = vtk.vtkImageStencil()
stencil.SetInputData(mask)
stencil.SetStencilConnection(dataToStencil.GetOutputPort())
stencil.ReverseStencilOff()
stencil.SetBackgroundValue(0)
stencil.Update()
# flip the image in Y and Z directions
#flip = vtk.vtkImageReslice()
#flip.SetInputConnection(stencil.GetOutputPort())
#flip.SetResliceAxesDirectionCosines(1,0,0, 0,-1,0, 0,0,-1);
#flip.Update();
# convert mask image data to numpy array with correct dimensions
# vtk_data = flip.GetOutput().GetPointData().GetScalars()
vtk_data = stencil.GetOutput().GetPointData().GetScalars()
#temp_data = np.zeros((dim[0], dim[1], dim[2]), dtype = np.int16)
temp_data = numpy_support.vtk_to_numpy(vtk_data).reshape(dim[2], dim[1], dim[0]).astype(np.int16)
# write to image
# scalars = stencil.GetOutput().GetPointData().GetScalars()
# np_scalars = vtk_to_numpy(scalars)
# np_scalars = np_scalars.reshape(dim[2], dim[1], dim[0])
# np_scalars = np_scalars.transpose(2,1,0)
#temp_data = temp_data.transpose(2,1,0)
#temp_data.transpose(1,2,0)
#temp_data = np.flip(temp_data, 2)
self.mask = temp_data.transpose(2,1,0)
def calculateVoxVolume(self, res, origin, dim):
""" calculates the volume (number of voxels) of structure mask
Arguments: :
res image resolution
origin image origin
dim image dimensions
Returns:
None
"""
if not hasattr(self, "mask"):
self.createMask(res, origin, dim)
if len(np.unique(self.mask)) < 2:
print ("Values ", np.unique(self.mask))
print ("No correct mask stenciled. Exit")
return
self.voxVolume = len(np.where(self.mask == 1)[0])
def getVoxVolume(self, res, origin, dim):
self.voxVolume = 0.
self.calculateVoxVolume(res, origin, dim)
return self.voxVolume
def createPolyData(self, scale_factor=Voxel(1.0, 1.0, 1.0)):
""" To create a polydata in vtk of the contour points we need two things:
1. a geometry: Describes single entities, such as points
2. a topology: Describes the connections of single entities, i.e.
how are the points connected?
Arguments:
scale_factor factor (x,y,z) to scale the polydata
Returns:
None
"""
append = vtk.vtkAppendPolyData()
beforeSlice = -1
for iSlice, iSubContour, nPoints in self.pts_subcontours:
if iSlice != beforeSlice:
countPoints = 0
beforeSlice = iSlice
vertexIndices = np.where(np.array(self.voxels)[:,2] == self.slices[iSlice])[0]
linePoints = vertexIndices[countPoints:countPoints + nPoints]
linePoints = np.append(linePoints, linePoints[0])
# create a contour for each subcontour
subContour = Contour()
subContour.createPolyLine(linePoints, self.voxels)
subContour.scale(scale_factor)
append.AddInputData(subContour.modifiedPolyData)
countPoints += nPoints
append.Update()
polyData = append.GetOutput()
if set(scale_factor.getArray()) == set([1.0, 1.0, 1.0]):
self.polyData = polyData
self.modifiedPolyData = polyData
self.calculateCenterofMass()
self.scaleZ(scale_factor)
self.logger.info("Center of Mass \t\t%s", self.centerOfMass.getRounds())
def createSurface(self):
""" create surface object from polydata using ruled Surface filter
Arguments:
None
Returns:
ruledSurfaceFilter filter object containing the surface
"""
ruledSurfaceFilter = vtk.vtkRuledSurfaceFilter()
ruledSurfaceFilter.SetInputData(self.modifiedPolyData)
ruledSurfaceFilter.SetResolution(50, 50)
ruledSurfaceFilter.SetRuledModeToResample()
ruledSurfaceFilter.Update()
self.getExtent()
return ruledSurfaceFilter
def readContourData(self, dcm):
""" process dcm file to extract contour points of structure set
Arguments:
dcm pydicom object of RTST
Returns:
slices list of slice position of the structure
contourdataPoints physical word coordinates of the contour points
"""
contourData = []
if "ContourSequence" in dcm.ROIContourSequence[self.index]:
for iSubContour in range(len(dcm.ROIContourSequence[self.index].ContourSequence)):
contourData.append([float(dcm.ROIContourSequence[self.index].ContourSequence[iSubContour].ContourData[2]),
dcm.ROIContourSequence[self.index].ContourSequence[iSubContour].ContourData[::3],
dcm.ROIContourSequence[self.index].ContourSequence[iSubContour].ContourData[1::3]])
slices = [contourData[iSlice][0] for iSlice in range(len(contourData))]
if len(slices) > 1:
if slices[1] - slices[0] < 0:
contourData.sort()
contourdataPoints = self.multiContour(contourData)
slices = [contourdataPoints[iSlice][0] for iSlice in range(len(contourdataPoints))]
return slices[:], contourdataPoints[:]
else:
self.logger.info("Only one Slice found")
self.debug.append("Contour contains one single slice")
return slices[:], []
else:
self.logger.info("No Contour sequence found")
self.debug.append(["Structure " + self.name + " does not contain a contour sequence"])
return [], []
def multiContour(self, contourData):
""" account for mutlicontours in one slice,
checks z positions in each sublist of the list and if the have the same z then creats a new sublist
for example input l = [[z1, [x1, x2],[y1,y2]], [z1, [x3, x4, x5],[y3, y4, y5]], [z2, [x1, x2],[y1,y2]]] - 3 contours on 2 slices
output l = [[z1, [[x1, x2],[y1,y2]], [[x3, x4, x5],[y3, y4, y5]]], [z2, [[x1, x2],[y1,y2]]]]
Arguments:
contourData point coordinates of contours in old format
Returns:
kontur points coordinates in new format
"""
listap=[]
lista_nr=[]
for contourpoint in contourData:
listap.append(contourpoint[0]) # skice
if contourpoint[0] not in lista_nr:
lista_nr.append(contourpoint[0]) # insert slice point into list
counts = []
for i in lista_nr:
counts.append(listap.count(i)) #how many times a ceratin z position occurs on the list
listka=[]
nr=0
kontur = []
for i in range(len(contourData)):
if contourData[i][0] not in listka:
m=[contourData[i][0]]
for j in range(counts[nr]):
m.append([np.array(contourData[i+j][1], dtype=np.float), np.array(contourData[i+j][2], dtype=np.float)])
listka.append(contourData[i][0])
kontur.append(m)
nr+=1
return kontur
def calculateCenterofMass(self):
""" Compute center of mass using polyData
Arguments:
None
Return:
center of mass
"""
centerOfMassFilter = vtk.vtkCenterOfMass()
centerOfMassFilter.SetInputData(self.modifiedPolyData)
centerOfMassFilter.SetUseScalarsAsWeights(False)
centerOfMassFilter.Update()
com = np.ones([3])
com = centerOfMassFilter.GetCenter()
self.centerOfMass = Voxel(com[0], com[1], com[2])
return self.centerOfMass
def processPolyData(self, shift, factor):
""" processes Polydata using shift and factor to scale and translate
Arguments:
shift Voxel object shift in x, y, z
factor Voxel object scale factor in x, y, z
Returns:
None
"""
self.logger.info(self.name + ": Process PolyData")
self.scalePolyData(factor)
self.translate(shift)
self.calculateCenterofMass()
def translate(self, shift):
""" translate polydata to new coordinate and update modifiedPolyData
Arguments:
shift Voxel object shift in x, y, z
Returns:
None
"""
translation = vtk.vtkTransform()
translation.Translate(shift.x, shift.y, shift.z)
transformFilter = vtk.vtkTransformPolyDataFilter()
transformFilter.SetInputData(self.modifiedPolyData)
transformFilter.SetTransform(translation)
transformFilter.Update()
self.modifiedPolyData = transformFilter.GetOutput()
self.centerOfMass = self.calculateCenterofMass()
def scalePolyData(self, factor):
""" scale polydata by factor in 2 dimensions
Arguments:
factor Voxel object scale in x, y
Returns:
None
"""
self.calculateCenterofMass()
self.createPolyData(factor)
def scaleZ(self, factor):
""" scale polydata by factor in z dimensions
Arguments:
factor Voxel object stretch in z
Returns:
None
"""
# since the scaling is performed in terms of the origin (0,0,0), the structure is
# first translated to origin by subtracting center of mass, then scaled
# and finally translate back by add the center of mass
# see https://stackoverflow.com/questions/55813955/how-to-scale-a-polydata-in-vtk-without-translating-it
com = self.centerOfMass
self.translate(com.negate())
scale = vtk.vtkTransform()
scale.Scale(1.0, 1.0, factor.z)
scale_TF = vtk.vtkTransformPolyDataFilter()
scale_TF.SetInputData(self.modifiedPolyData);
scale_TF.SetTransform(scale);
scale_TF.Update()
self.modifiedPolyData = scale_TF.GetOutput()
self.translate(com)
self.centerOfMass = self.calculateCenterofMass()
def calculateProperties(self):
""" calculates properties of the polygon
Arguments:
None
Return:
None
"""
self.logger.info(" " + self.name + ": Calculate shape properties" )
self.centerOfMass = self.calculateCenterofMass()
self.volume, self.surface = self.calculateMassProperties()
def getExtent(self):
""" get extent of polydata
Arguments:
None
Return:
None
"""
self.bounds = self.modifiedPolyData.GetBounds()