I want to use VTK to overlay two files

Viewed 166

I am currently creating a program that overlays nrrd two files. The information such as the spacing and size of the two files is the same. One file has 3D medical image information, and the other file stores the indexing information of labeling. I want to extract this indexing information and make color mapping different for each indexing. The output of both files is successful, but the two files are not superimposed. I've looked at the code, but I have not been able to see what the problem is.

Program Result I think this is due to the difference in the mapper that renders the two files, so I rendered it using the same mapper. Color mapping did not succeed, but printing overlaid succeeded.

So what do you have to do to print the two objects over and over? I do not know how to look at the code.

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 Answers
Related