Guest User

Untitled

a guest
Mar 24th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. # Important: the input volume must have isotropic spacing.
  2. # If surface is thick, use Extract skeleton module with Skeleton type = 2D, Do not prune branches = enabled.
  3. # Increase radius parameter to fill more holes.
  4. # Increase dimension to preserve more details.
  5.  
  6. inputLabelmap = getNode('Input labelmap')
  7.  
  8. ici = vtk.vtkImageChangeInformation()
  9. ici.SetInputData(inputLabelmap.GetImageData())
  10. ici.SetOutputSpacing(inputLabelmap.GetSpacing())
  11. ici.SetOrigin(inputLabelmap.GetOrigin())
  12.  
  13. imageToPoints=vtk.vtkImageDataToPointSet()
  14. imageToPoints.SetInputConnection(ici.GetOutputPort())
  15.  
  16. geom = vtk.vtkStructuredGridGeometryFilter()
  17. geom.SetInputConnection(imageToPoints.GetOutputPort())
  18.  
  19. threshold = vtk.vtkThreshold()
  20. threshold.ThresholdByUpper(1)
  21. threshold.SetInputConnection(geom.GetOutputPort())
  22.  
  23. geom2 = vtk.vtkGeometryFilter()
  24. geom2.SetInputConnection(threshold.GetOutputPort())
  25. geom2.Update()
  26. polyData=geom2.GetOutput()
  27.  
  28. # Estimate normals
  29. normals = vtk.vtkPCANormalEstimation()
  30. normals.SetInputData(polyData)
  31. sampleSize = polyData.GetNumberOfPoints() * .00005
  32. if sampleSize < 10:
  33. sampleSize = 50;
  34. normals.SetSampleSize(int(sampleSize))
  35. normals.SetNormalOrientationToGraphTraversal()
  36. normals.FlipNormalsOn()
  37.  
  38. distance = vtk.vtkSignedDistance()
  39. distance.SetInputConnection(normals.GetOutputPort())
  40.  
  41. dimension = 256
  42. bounds = [0,-1,0,-1,0,-1]
  43. polyData.GetBounds(bounds);
  44. range = [bounds[1]-bounds[0], bounds[3]-bounds[2], bounds[5]-bounds[4]]
  45. radius = max(range) / float(dimension) * 4 # approximately 4 voxels
  46.  
  47. distance.SetRadius(radius)
  48. distance.SetDimensions(dimension, dimension, dimension)
  49. distance.SetBounds(
  50. bounds[0] - range[0] * .1,
  51. bounds[1] + range[0] * .1,
  52. bounds[2] - range[1] * .1,
  53. bounds[3] + range[1] * .1,
  54. bounds[4] - range[2] * .1,
  55. bounds[5] + range[2] * .1)
  56.  
  57. surface = vtk.vtkExtractSurface()
  58. surface.SetInputConnection(distance.GetOutputPort())
  59. surface.SetRadius(radius * .99)
  60. surface.Update()
  61.  
  62. resultSurface = slicer.modules.models.logic().AddModel(surface.GetOutput())
  63. resultSurface.GetDisplayNode().BackfaceCullingOff()
Add Comment
Please, Sign In to add comment