vtk内两种PolyData转voxel的方法

一是采用vtkVoxelModeller

voxelModel = vtk.vtkVoxelModeller()
voxelModel.SetInputConnection(connect.GetOutputPort())
voxelModel.SetSampleDimensions(128, 128, 128)
voxelModel.SetScalarTypeToBit()
voxelModel.SetForegroundValue(1)
voxelModel.SetBackgroundValue(0)
voxelSurface = vtk.vtkContourFilter()
voxelSurface.SetInputConnection(voxelModel.GetOutputPort())
voxelSurface.SetValue(0, .999)

这种方法实验下来发现特别的慢。
另一种是采用vtkImagePolyDataToImageStencil

stl = reader.GetOutput()
bounds = stl.GetBounds()
spacing = [0.1, 0.1, 0.1]
whiteImage = vtk.vtkImageData()
whiteImage.SetSpacing(spacing)
# Set dim
dim = [math.ceil((bounds[i*2+1]-bounds[i*2])/spacing[i]) for i in range(3)]
print(dim)
whiteImage.SetDimensions(dim)
whiteImage.SetExtent(0, dim[0]-1, 0, dim[1]-1, 0, dim[2]-1)
# Set origin
origin = [bounds[i*2]+spacing[i]/2 for i in range(3)]
whiteImage.SetOrigin(origin)
whiteImage.AllocateScalars(vtk.VTK_UNSIGNED_CHAR, 1)
# Fill the image with foreground voxels
inval = 1
outval = 0
whiteImage.GetPointData().GetScalars().Fill(inval)
# Polygonal data --> image stencil
pol2stenc = vtk.vtkPolyDataToImageStencil()
pol2stenc.SetInputData(stl)
pol2stenc.SetOutputOrigin(origin)
pol2stenc.SetOutputSpacing(spacing)
pol2stenc.SetOutputWholeExtent(whiteImage.GetExtent())
pol2stenc.Update()
# Cut the corresponding white image and set the background
imgstenc = vtk.vtkImageStencil()
imgstenc.SetInputData(whiteImage)
imgstenc.SetStencilConnection(pol2stenc.GetOutputPort())
imgstenc.ReverseStencilOff()
imgstenc.SetBackgroundValue(outval)
imgstenc.Update()

这种方法效率还不错,就是要控制一下它的边界,不然容易产生空洞。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 每天我们都在耗尽着清晨每一缕阳光,望着渐渐徐徐升起时,四季的变化蹉跎在分寸之间。人生的时间的确少的奢侈可怜,还没有...
    帝颛顼阅读 178评论 0 1
  • 微信小程序中如何使用特殊字体 先百度下载要使用的字体 打开网站 https://transfonter.org/红...
    hsqin阅读 10,624评论 0 10