我想使用VTK覆盖两个文件

时间:2019-06-27 23:48:55

标签: python vtk

我当前正在创建一个程序,将nrrd覆盖两个文件。两个文件的间距和大小等信息是相同的。一个文件具有3D医学图像信息,另一个文件存储标签的索引信息。我想提取此索引信息,并使每个索引的颜色映射不同。两个文件的输出都成功,但是两个文件没有重叠。我看过代码,但看不到问题所在。

程序结果我认为这是由于渲染两个文件的映射器中的差异,所以我使用相同的映射器来渲染它。色彩映射未成功,但打印覆盖成功。

那么,您要怎么做才能一遍又一遍地打印两个对象?我不知道如何看代码。非常感谢您的回答。

import vtk
import nrrd
import numpy as np
from vtk.util import numpy_support as VN


def main():
    renWin = vtk.vtkRenderWindow()
    renderer = vtk.vtkRenderer()
    renWin.AddRenderer(renderer)
    iren = vtk.vtkRenderWindowInteractor()
    iren.SetRenderWindow(renWin)


reader = vtk.vtkNrrdReader()
reader.SetFileName('sample_nrrd.nrrd')
reader.Update()

vol = ImageDataToVolume(reader.GetOutput())
renderer.AddVolume(vol)

data = nrrd.read('Segmentation-label_2.nrrd')

for x in range(0, 500):
    for y in range(100, 500):
        for z in range(220, 300):
            if data[0][x][y][z] != 0:
                src = vtk.vtkSphereSource()
                src.SetCenter(x, y, z)
                src.SetRadius(0.6)

                src.Update()

                mapper1 = vtk.vtkPolyDataMapper()
                actor1 = vtk.vtkActor()
                mapper1.SetInputConnection(src.GetOutputPort())
                actor1.SetMapper(mapper1)
                if data[0][x][y][z] == 1:
                    actor1.GetProperty().SetColor(255, 0, 0)
                elif data[0][x][y][z] == 2:
                    actor1.GetProperty().SetColor(0, 255, 0)
                elif data[0][x][y][z] == 3:
                    actor1.GetProperty().SetColor(0, 0, 255)
                elif data[0][x][y][z] == 4:
                    actor1.GetProperty().SetColor(255, 255, 0)
                renderer.AddActor(actor1)
iren.Initialize()
renWin.Render()
iren.Start() 


def ImageDataToVolume(imagedata):
    mapper = vtk.vtkSmartVolumeMapper()
    mapper.SetInputData(imagedata)

    vol = vtk.vtkVolume()
    vol.SetMapper(mapper)

    ApplyAutoVolumeProperty(imagedata, mapper, vol)

    return vol 


def GetScalarData(ptData):
    vtkDataArray = ptData.GetScalars()
    if vtkDataArray == None:
        return np.array([])
    sData = VN.vtk_to_numpy(vtkDataArray)
    return sData 


def ApplyAutoVolumeProperty(pData, pMapper, pVolume):
    scalar = GetScalarData(pData.GetPointData())

    min_value = -1000
    max_value = 3095

    fCenter = (max_value - min_value) / 2.0
    fWidth = max_value - min_value

    fLow = fCenter - fWidth / 2.0
    fHigh = fCenter + fWidth / 2.0

    fTerm = fHigh - fLow;

    print("min, max : " + str(min_value) + ", " + str(max_value))

    pMapper.SetBlendModeToComposite()

    volProperty = vtk.vtkVolumeProperty()
    volProperty.ShadeOn()
    volProperty.SetInterpolationTypeToLinear()
    volProperty.SetDiffuse(0.7)
    volProperty.SetAmbient(0.2)
    volProperty.SetSpecular(0.3)
    volProperty.SetSpecularPower(30.0)

    otf = vtk.vtkPiecewiseFunction()
    otf.AddPoint(fLow, 0.0)  # start
    otf.AddPoint(fHigh, 1.0)  # end
    volProperty.SetScalarOpacity(0, otf)

    ctf = vtk.vtkColorTransferFunction()
    ctf.AddRGBPoint(fLow + fTerm * 0 * 0.2, 0.0, 0.0, 0.0)
    ctf.AddRGBPoint(fLow + fTerm * 1 * 0.2, 0.7, 0.4, 0.1)
    ctf.AddRGBPoint(fLow + fTerm * 2 * 0.2, 0.6, 0.5, 0.3)
    ctf.AddRGBPoint(fHigh, 1.0, 1.0, 1.0)
    volProperty.SetColor(0, ctf)

    pVolume.SetProperty(volProperty)
    pass 


if __name__ == '__main__':
    main()

0 个答案:

没有答案