[Neuroimaging] convert Trk file to VTK file
Nico Hoffmann
nico.hoffmann at tu-dresden.de
Wed Jul 3 13:39:30 EDT 2019
Dear Bert,
the attached python snippet allows you to convert trk to vtk based on vtk and dipy.
You can add scalar information to the vtk polydata array by polydata.GetPointData().AddArray(scalars). “scalars” is a vtk.vtkFloatArray() containing a scalar per vtkPoint. That value can be used to color your streamlines (and even every point on the streamlines) in 3DSlicer (at least).
Best,
Nico
---
import vtk
from dipy.tracking.streamline import Streamlines
from dipy.io.streamline import load_trk
streams, hdr = load_trk(fname)
streamlines = Streamlines(streams)
saveStreamlinesVTK(streamlines,”sl.vtk”)
def saveStreamlinesVTK(streamlines, pStreamlines):
polydata = vtk.vtkPolyData()
lines = vtk.vtkCellArray()
points = vtk.vtkPoints()
ptCtr = 0
for i in range(0,len(streamlines)):
if((i % 10000) == 0):
print(str(i) + "/" + str(len(streamlines)))
line = vtk.vtkLine()
line.GetPointIds().SetNumberOfIds(len(streamlines[i]))
for j in range(0,len(streamlines[i])):
points.InsertNextPoint(streamlines[i][j])
linePts = line.GetPointIds()
linePts.SetId(j,ptCtr)
ptCtr += 1
lines.InsertNextCell(line)
polydata.SetLines(lines)
polydata.SetPoints(points)
writer = vtk.vtkPolyDataWriter()
writer.SetFileName(pStreamlines)
writer.SetInputData(polydata)
writer.Write()
print("Wrote streamlines to " + writer.GetFileName())
--
Dr. rer. nat. Nico Hoffmann
Computational Radiation Physics
Helmholtz-Zentrum Dresden-Rossendorf (HZDR)
Bautzner Landstraße 400
01328 Dresden
T +49 351 260 3668
More information about the Neuroimaging
mailing list