surface_process.py
7.39 KB
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
import multiprocessing
import tempfile
import time
import numpy
import vtk
import invesalius.i18n as i18n
import invesalius.data.converters as converters
# import invesalius.data.imagedata_utils as iu
from scipy import ndimage
# TODO: Code duplicated from file {imagedata_utils.py}.
def ResampleImage3D(imagedata, value):
"""
Resample vtkImageData matrix.
"""
spacing = imagedata.GetSpacing()
extent = imagedata.GetExtent()
size = imagedata.GetDimensions()
width = float(size[0])
height = float(size[1]/value)
resolution = (height/(extent[1]-extent[0])+1)*spacing[1]
resample = vtk.vtkImageResample()
resample.SetInputData(imagedata)
resample.SetAxisMagnificationFactor(0, resolution)
resample.SetAxisMagnificationFactor(1, resolution)
return resample.GetOutput()
class SurfaceProcess(multiprocessing.Process):
def __init__(self, pipe, filename, shape, dtype, mask_filename,
mask_shape, mask_dtype, spacing, mode, min_value, max_value,
decimate_reduction, smooth_relaxation_factor,
smooth_iterations, language, flip_image, q_in, q_out,
from_binary, algorithm, imagedata_resolution):
multiprocessing.Process.__init__(self)
self.pipe = pipe
self.spacing = spacing
self.filename = filename
self.mode = mode
self.min_value = min_value
self.max_value = max_value
self.decimate_reduction = decimate_reduction
self.smooth_relaxation_factor = smooth_relaxation_factor
self.smooth_iterations = smooth_iterations
self.language = language
self.flip_image = flip_image
self.q_in = q_in
self.q_out = q_out
self.dtype = dtype
self.shape = shape
self.from_binary = from_binary
self.algorithm = algorithm
self.imagedata_resolution = imagedata_resolution
self.mask_filename = mask_filename
self.mask_shape = mask_shape
self.mask_dtype = mask_dtype
def run(self):
if self.from_binary:
self.mask = numpy.memmap(self.mask_filename, mode='r',
dtype=self.mask_dtype,
shape=self.mask_shape)
else:
self.image = numpy.memmap(self.filename, mode='r', dtype=self.dtype,
shape=self.shape)
self.mask = numpy.memmap(self.mask_filename, mode='r',
dtype=self.mask_dtype,
shape=self.mask_shape)
while 1:
roi = self.q_in.get()
if roi is None:
break
self.CreateSurface(roi)
def SendProgress(self, obj, msg):
prog = obj.GetProgress()
self.pipe.send([prog, msg])
def CreateSurface(self, roi):
if self.from_binary:
a_mask = numpy.array(self.mask[roi.start + 1: roi.stop + 1,
1:, 1:])
image = converters.to_vtk(a_mask, self.spacing, roi.start,
"AXIAL")
del a_mask
else:
a_image = numpy.array(self.image[roi])
if self.algorithm == u'InVesalius 3.b2':
a_mask = numpy.array(self.mask[roi.start + 1: roi.stop + 1,
1:, 1:])
a_image[a_mask == 1] = a_image.min() - 1
a_image[a_mask == 254] = (self.min_value + self.max_value) / 2.0
image = converters.to_vtk(a_image, self.spacing, roi.start,
"AXIAL")
gauss = vtk.vtkImageGaussianSmooth()
gauss.SetInputData(image)
gauss.SetRadiusFactor(0.3)
gauss.ReleaseDataFlagOn()
gauss.Update()
del image
image = gauss.GetOutput()
del gauss
del a_mask
else:
image = converters.to_vtk(a_image, self.spacing, roi.start,
"AXIAL")
del a_image
if self.imagedata_resolution:
# image = iu.ResampleImage3D(image, self.imagedata_resolution)
image = ResampleImage3D(image, self.imagedata_resolution)
flip = vtk.vtkImageFlip()
flip.SetInputData(image)
flip.SetFilteredAxis(1)
flip.FlipAboutOriginOn()
flip.ReleaseDataFlagOn()
flip.Update()
del image
image = flip.GetOutput()
del flip
#filename = tempfile.mktemp(suffix='_%s.vti' % (self.pid))
#writer = vtk.vtkXMLImageDataWriter()
#writer.SetInput(mask_vtk)
#writer.SetFileName(filename)
#writer.Write()
#print "Writing piece", roi, "to", filename
# Create vtkPolyData from vtkImageData
#print "Generating Polydata"
#if self.mode == "CONTOUR":
#print "Contour"
contour = vtk.vtkContourFilter()
contour.SetInputData(image)
#contour.SetInput(flip.GetOutput())
if self.from_binary:
contour.SetValue(0, 127) # initial threshold
else:
contour.SetValue(0, self.min_value) # initial threshold
contour.SetValue(1, self.max_value) # final threshold
contour.ComputeScalarsOn()
contour.ComputeGradientsOn()
contour.ComputeNormalsOn()
contour.ReleaseDataFlagOn()
contour.Update()
#contour.AddObserver("ProgressEvent", lambda obj,evt:
# self.SendProgress(obj, _("Generating 3D surface...")))
polydata = contour.GetOutput()
del image
del contour
#else: #mode == "GRAYSCALE":
#mcubes = vtk.vtkMarchingCubes()
#mcubes.SetInput(flip.GetOutput())
#mcubes.SetValue(0, self.min_value)
#mcubes.SetValue(1, self.max_value)
#mcubes.ComputeScalarsOff()
#mcubes.ComputeGradientsOff()
#mcubes.ComputeNormalsOff()
#mcubes.AddObserver("ProgressEvent", lambda obj,evt:
#self.SendProgress(obj, _("Generating 3D surface...")))
#polydata = mcubes.GetOutput()
#triangle = vtk.vtkTriangleFilter()
#triangle.SetInput(polydata)
#triangle.AddObserver("ProgressEvent", lambda obj,evt:
#self.SendProgress(obj, _("Generating 3D surface...")))
#triangle.Update()
#polydata = triangle.GetOutput()
#if self.decimate_reduction:
##print "Decimating"
#decimation = vtk.vtkDecimatePro()
#decimation.SetInput(polydata)
#decimation.SetTargetReduction(0.3)
#decimation.AddObserver("ProgressEvent", lambda obj,evt:
#self.SendProgress(obj, _("Generating 3D surface...")))
##decimation.PreserveTopologyOn()
#decimation.SplittingOff()
#decimation.BoundaryVertexDeletionOff()
#polydata = decimation.GetOutput()
self.pipe.send(None)
filename = tempfile.mktemp(suffix='_%s.vtp' % (self.pid))
writer = vtk.vtkXMLPolyDataWriter()
writer.SetInputData(polydata)
writer.SetFileName(filename)
writer.Write()
print "Writing piece", roi, "to", filename
del polydata
del writer
self.q_out.put(filename)