surface_process.py
4.76 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
import multiprocessing
import tempfile
import time
import numpy
import vtk
import i18n
import data.converters
from scipy import ndimage
class SurfaceProcess(multiprocessing.Process):
def __init__(self, pipe, filename, shape, dtype, spacing, mode, min_value, max_value,
decimate_reduction, smooth_relaxation_factor,
smooth_iterations, language, fill_holes, keep_largest,
flip_image, q_in, q_out):
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.fill_holes = fill_holes
self.keep_largest = keep_largest
self.flip_image = flip_image
self.q_in = q_in
self.q_out = q_out
self.dtype = dtype
self.shape = shape
def run(self):
self.mask = numpy.memmap(self.filename, mode='r', dtype=self.dtype,
shape=self.shape)
while 1:
roi = self.q_in.get()
print roi
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):
smoothed = numpy.array(self.mask[roi])
image = converters.to_vtk(smoothed, self.spacing, roi.start,
"AXIAL")
flip = vtk.vtkImageFlip()
flip.SetInput(image)
flip.SetFilteredAxis(1)
flip.FlipAboutOriginOn()
flip.Update()
# Create vtkPolyData from vtkImageData
#print "Generating Polydata"
#if self.mode == "CONTOUR":
#print "Contour"
#contour = vtk.vtkContourFilter()
#contour.SetInput(image)
#contour.SetValue(0, self.min_value) # initial threshold
#contour.SetValue(1, self.max_value) # final threshold
#contour.ComputeScalarsOn()
#contour.ComputeGradientsOn()
#contour.ComputeNormalsOn()
#polydata = contour.GetOutput()
#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()
polydata = mcubes.GetOutput()
triangle = vtk.vtkTriangleFilter()
triangle.SetInput(polydata)
triangle.Update()
polydata = triangle.GetOutput()
bounds = polydata.GetBounds()
origin = ((bounds[1] + bounds[0]) / 2.0, (bounds[3] + bounds[2])/2.0,
(bounds[5] + bounds[4]) / 2.0)
print "Bounds is", bounds
print "origin is", origin
#print "Decimating"
decimation = vtk.vtkDecimatePro()
decimation.SetInput(polydata)
decimation.SetTargetReduction(0.3)
#decimation.PreserveTopologyOn()
decimation.SplittingOff()
decimation.BoundaryVertexDeletionOff()
polydata = decimation.GetOutput()
#decimation = vtk.vtkQuadricClustering()
#decimation.SetInput(polydata)
#decimation.AutoAdjustNumberOfDivisionsOff()
#decimation.SetDivisionOrigin(0, 0, 0)
#decimation.SetDivisionSpacing(self.spacing)
#decimation.SetFeaturePointsAngle(80)
#decimation.UseFeaturePointsOn()
#decimation.UseFeatureEdgesOn()
#ecimation.CopyCellDataOn()
#print "Division", decimation.GetNumberOfDivisions()
#polydata = decimation.GetOutput()
#if self.smooth_iterations and self.smooth_relaxation_factor:
#print "Smoothing"
#smoother = vtk.vtkWindowedSincPolyDataFilter()
#smoother.SetInput(polydata)
#smoother.SetNumberOfIterations(self.smooth_iterations)
#smoother.SetFeatureAngle(120)
#smoother.SetNumberOfIterations(30)
#smoother.BoundarySmoothingOn()
#smoother.SetPassBand(0.01)
#smoother.FeatureEdgeSmoothingOn()
#smoother.NonManifoldSmoothingOn()
#smoother.NormalizeCoordinatesOn()
#smoother.Update()
#polydata = smoother.GetOutput()
print "Saving"
filename = tempfile.mktemp(suffix='_%s.vtp' % (self.pid))
writer = vtk.vtkXMLPolyDataWriter()
writer.SetInput(polydata)
writer.SetFileName(filename)
writer.Write()
self.q_out.put(filename)