-
Notifications
You must be signed in to change notification settings - Fork 6
/
nii_2_mesh_conversion.py
48 lines (40 loc) · 1.6 KB
/
nii_2_mesh_conversion.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
import vtk
def nii_2_mesh (filename_nii, filename_stl, label):
"""
Read a nifti file including a binary map of a segmented organ with label id = label.
Convert it to a smoothed mesh of type stl.
filename_nii : Input nifti binary map
filename_stl : Output mesh name in stl format
label : segmented label id
"""
# read the file
reader = vtk.vtkNIFTIImageReader()
reader.SetFileName(filename_nii)
reader.Update()
# apply marching cube surface generation
surf = vtk.vtkDiscreteMarchingCubes()
surf.SetInputConnection(reader.GetOutputPort())
surf.SetValue(0, label) # use surf.GenerateValues function if more than one contour is available in the file
surf.Update()
#smoothing the mesh
smoother= vtk.vtkWindowedSincPolyDataFilter()
if vtk.VTK_MAJOR_VERSION <= 5:
smoother.SetInput(surf.GetOutput())
else:
smoother.SetInputConnection(surf.GetOutputPort())
smoother.SetNumberOfIterations(30)
smoother.NonManifoldSmoothingOn()
smoother.NormalizeCoordinatesOn() #The positions can be translated and scaled such that they fit within a range of [-1, 1] prior to the smoothing computation
smoother.GenerateErrorScalarsOn()
smoother.Update()
# save the output
writer = vtk.vtkSTLWriter()
writer.SetInputConnection(smoother.GetOutputPort())
writer.SetFileTypeToASCII()
writer.SetFileName(filename_stl)
writer.Write()
if __name__ == '__main__':
filename_nii = '01.nii.gz'
filename_stl = '01.stl'
label = 1
nii_2_mesh (filename_nii, filename_stl, label)