def img_to_stl(img): # Pad black to the boundaries of the image pad = [5, 5, 5] img = sitk.ConstantPad(img, pad, pad) vtkimg = sitk2vtk.sitk2vtk(img) isovalue = 0 mesh = vtkutils.extractSurface(vtkimg, isovalue) connectivityFilter = False mesh = vtkutils.cleanMesh(mesh, connectivityFilter) smoothIterations = 25 mesh = vtkutils.smoothMesh(mesh, smoothIterations) quad = .90 mesh = vtkutils.reduceMesh(mesh, quad) vtkutils.writeMesh(mesh, "result.stl")
#sitk.WriteImage( img, vtkname ) import sitk2vtk vtkimg = sitk2vtk.sitk2vtk(img) img = None gc.collect() import traceback, vtk if debug: print "\nVTK version: ", vtk.vtkVersion.GetVTKVersion() print "VTK: ", vtk, "\n" import vtkutils mesh = vtkutils.extractSurface(vtkimg, isovalue) vtkimg = None gc.collect() mesh2 = vtkutils.cleanMesh(mesh, connectivityFilter) mesh = None gc.collect() mesh3 = vtkutils.smoothMesh(mesh2, smoothIterations) mesh2 = None gc.collect() mesh4 = vtkutils.reduceMesh(mesh3, quad) mesh3 = None gc.collect() if rotFlag: mesh5 = vtkutils.rotateMesh(mesh4, rotAxis, rotAngle) else:
img = None gc.collect() import traceback, vtk if debug: print "\nVTK version: ", vtk.vtkVersion.GetVTKVersion() print "VTK: ", vtk, "\n" import vtkutils if debug: print "Extracting surface" mesh = vtkutils.extractSurface(vtkimg, isovalue) vtkimg = None gc.collect() if debug: print "Cleaning mesh" mesh2 = vtkutils.cleanMesh(mesh, connectivityFilter) mesh = None gc.collect() if debug: print "Smoothing mesh" mesh3 = vtkutils.smoothMesh(mesh2, smoothIterations) mesh2 = None gc.collect() if debug: print "Simplifying mesh" mesh4 = vtkutils.reduceMesh(mesh3, quad)