python - Adding Alpha Channel to Volume in VTK
问题描述
Hello everyone interested,
i am having a bit trouble to add an alpha channel to a volume (made from rgb tiffstack) in VTK. In the Slicer3d App, i just add the following skript in the python shell to get this done:
colorVolume = slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLVectorVolumeNode")
# Convert RGB image to RGBA
luminance = vtk.vtkImageLuminance()
luminance.SetInputConnection(colorVolume.GetImageDataConnection())
append=vtk.vtkImageAppendComponents()
append.AddInputConnection(colorVolume.GetImageDataConnection())
append.AddInputConnection(luminance.GetOutputPort())
append.Update()
colorVolume.SetAndObserveImageData(append.GetOutput())
# Enable volume rendering
volRenLogic = slicer.modules.volumerendering.logic()
displayNode = volRenLogic.CreateDefaultVolumeRenderingNodes(colorVolume)
displayNode.SetVisibility(True)
# Enable direct RGBA color mapping
displayNode.GetVolumePropertyNode().GetVolumeProperty().SetIndependentComponents(0)
Now i am trying this in my own Script in python with vtk. But i am missing the right translation from Slicer to Python VTK.
For a better Understanding, my Input Data is a Tifftack/MultiImageTiff in RBG and i read it in with the following code:
filename = "D:/SomeFilePath/Some_Tiff_Stack.tif"
colors = vtk.vtkNamedColors()
# renderer, render window
# and interactor.
ren1 = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren1)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
# Create the reader for the Tiff-Data (Multi-Image-Tiff)
reader = vtk.vtkTIFFReader()
reader.SetFileName(filename)
reader.SetDataSpacing(1,1,1)
# Create transfer mapping scalar value to opacity (configured in Slicer app).
opacityTransferFunction = vtk.vtkPiecewiseFunction()
opacityTransferFunction.AddPoint(0, 0)
opacityTransferFunction.AddPoint(20, 0)
opacityTransferFunction.AddPoint(40, 0.45)
opacityTransferFunction.AddPoint(98.26, 0.78)
opacityTransferFunction.AddPoint(255, 0)
# Create transfer mapping scalar value to color.
colorTransferFunction = vtk.vtkColorTransferFunction()
colorTransferFunction.AddRGBPoint(0.0, 1, 1, 1)
colorTransferFunction.AddRGBPoint(64.0, 1.0, 1, 1)
colorTransferFunction.AddRGBPoint(128.0, 1.0, 1, 1.0)
colorTransferFunction.AddRGBPoint(192.0, 1.0, 1.0, 1.0)
colorTransferFunction.AddRGBPoint(255.0, 1, 1, 1)
# The property describes how the data will look.
volumeProperty = vtk.vtkVolumeProperty()
volumeProperty.SetColor(colorTransferFunction)
volumeProperty.SetScalarOpacity(opacityTransferFunction)
volumeProperty.SetInterpolationTypeToLinear()
'''
#This is what i need to translate to python/vtk
luminance = vtk.vtkImageLuminance()
luminance.SetInputConnection(colorVolume.GetImageDataConnection())
append=vtk.vtkImageAppendComponents()
append.AddInputConnection(colorVolume.GetImageDataConnection())
append.AddInputConnection(luminance.GetOutputPort())
append.Update()
colorVolume.SetAndObserveImageData(append.GetOutput())
volumeProperty.SetIndependentComponents(0)
'''
# The mapper / ray cast function know how to render the data.
#volumeMapper = vtk.vtkFixedPointVolumeRayCastMapper()
volumeMapper = vtk.vtkGPUVolumeRayCastMapper()
volumeMapper.SetInputConnection(reader.GetOutputPort())
volume = vtk.vtkVolume()
volume.SetMapper(volumeMapper)
volume.SetProperty(volumeProperty)
#volumeProperty.SetIndependentComponents(0)
ren1.AddVolume(volume)
ren1.ResetCameraClippingRange()
ren1.ResetCamera()
renWin.SetSize(600, 600)
renWin.Render()
iren.Start()
If someone has any idea how to get this done, i realy appreciate any Help. Until now, i "only" get a black Box surrounding my Image/Volume. With help of the alpha chanel i will be able to set the blackbox to transparent.
Best Regards Bastian
解决方案
作为修复/翻译,我建议如下:
# create a default luminance map with the same size as the input image
luminance = vtk.vtkImageLuminance()
luminance.SetInputConnection(reader.GetOutputPort())
# merge the original image and the luminace map into one RGBA image
append = vtk.vtkImageAppendComponents()
append.AddInputConnection(reader.GetOutputPort())
append.AddInputConnection(luminance.GetOutputPort())
volumeProperty.SetIndependentComponents(False)
# use the created RGBA image from the append as input for the mapper
volumeMapper = vtk.vtkGPUVolumeRayCastMapper()
volumeMapper.SetInputConnection(append.GetOutputPort())
这将创建一个亮度图并将其与图像合并为 RGBA 图像。
colorVolume.SetAndObserveImageData(append.GetOutput())
此行用作将创建的图像反馈到切片器中,在这种情况下您不需要它,因为您执行自己的渲染。
推荐阅读
- ruby-on-rails - 主动模型序列化器 each_serializer 与序列化器
- berkeley-db - berkeley-db-je 的最新版本是什么?
- php - PHP mysql数据未从最后一个ID检索
- javascript - 如何为一般元素设置背景颜色,然后通过一个事件(onclick)设置特定元素(相同类型)的背景颜色?
- python - 打印从另一个类继承的另一个类的参数
- python - 向MySql添加元组的Python函数不起作用
- html - 在 React 中提交表单时选择标签并物化数据选择器消失
- sql - 触发器在 SQL Server 中对表进行排序和更新
- spring - 在以下任何来源中都找不到插件 [id: 'io.spring.dependency-management', version: '1.0.5.RELEASE', apply: false]:
- sql-server - Dynamic Management View 给出的 Index Tuning 建议有多准确