我正在发现 VTK 并想用它来绘制一个 3D numpy 数组。到目前为止,我已经设法将一个 numpy 数组转换为 avtk.Volume
并显示它,但是从那里我很难得到漂亮的东西。
我得到一个像这样的块状渲染:
我想要一个平滑的渲染,所以我猜这个体积是平滑的,或者从这个体积中提取的表面是平滑的。
我已经为此卷测试了一堆 vtk 映射器,例如SmartVolumeMapper
,并使用了着色器和插值,但没有得到很好的结果。
这是我的代码(在 Python 中):
import vtk
import numpy as np
npa= #some 3D numpy array
[h,w,z]=npa.shape
#importing the numpy array (comes from http://www.vtk.org/Wiki/VTK/Examples/Python/vtkWithNumpy)
dataImporter = vtk.vtkImageImport()
data_string = npa.tostring()
dataImporter.CopyImportVoidPointer(data_string, len(data_string))
dataImporter.SetDataScalarTypeToUnsignedChar()
dataImporter.SetNumberOfScalarComponents(1)
dataImporter.SetDataExtent(0,z-1, 0, w-1, 0,h-1)
dataImporter.SetWholeExtent(0,z-1, 0,w-1, 0,h-1)
#Defining a transparency function
alphaChannelFunc = vtk.vtkPiecewiseFunction()
alphaChannelFunc.AddPoint(0, 0.0)
alphaChannelFunc.AddPoint(255, 1)
# Defining a color function
colorFunc = vtk.vtkColorTransferFunction()
colorFunc.AddRGBPoint(255, 1.0, 1.0, 1.0)
colorFunc.AddRGBPoint(128, 0.0, 0, 1.0)
#Creating the volume
volumeProperty = vtk.vtkVolumeProperty()
volumeProperty.SetColor(colorFunc)
volumeProperty.SetScalarOpacity(alphaChannelFunc)
volumeProperty.ShadeOn()
volumeProperty.SetInterpolationTypeToLinear()
#Creating the mapper
compositeFunction = vtk.vtkVolumeRayCastCompositeFunction()
volumeMapper = vtk.vtkVolumeRayCastMapper()
volumeMapper.SetVolumeRayCastFunction(compositeFunction)
volumeMapper.SetInputConnection(dataImporter.GetOutputPort())
#Creating the volume actor
volume = vtk.vtkVolume()
volume.SetMapper(volumeMapper)
volume.SetProperty(volumeProperty)
#Creating the renderer
renderer = vtk.vtkRenderer()
renderWin = vtk.vtkRenderWindow()
renderWin.AddRenderer(renderer)
renderInteractor = vtk.vtkRenderWindowInteractor()
renderInteractor.SetRenderWindow(renderWin)
#Adding the actor
renderer.AddVolume(volume)
renderer.SetBackground(0, 0, 0)
renderWin.SetSize(400, 400)
#Launching the renderer
renderInteractor.Initialize()
renderWin.Render()
renderInteractor.Start()
我的印象是Volume
演员不是获得漂亮东西的方法,也许我应该去一个PolyData
或什么?我浏览了Marching Cubes 示例(在 C++ 中),它似乎需要一个体积并从中提取一个表面,但我暂时无法让它工作(没有错误,但输出是一个完全白色的错误窗口,它赢得了'不关闭)。
我可以更深入地研究它以尝试让它工作,但首先我想从你们那里得到意见,因为我是 VTK 的初学者,也许我处理这一切都是错误的。
我在 Ubuntu 14 上使用 Python 2.7.12 和 vtk 5.10.1。