赞
踩
这段时间需要做一个脑部纤维聚类的工作,在一篇论文中找到了72个纤维束模板,但由于数据是.trk格式的,所以第一步要把.trk转成.vtk,据说trackvis可以加载.trk并且直接输出.vtk,缺点是不能批量操作,我有7000多个需要转换的数据。
所以我上网找了找有没有相关的python代码或matlab代码,就算是只能找到单个文件的转换方法也行,如果找到批量转换的就更好了,最终我找到了这个TRK文件转换为VTK文件的方法,发现tractconverter模块可以实现单个文件转换,然后我去了模块作者的GitHub,知道了如何安装这个模块,然而在安装时提示了一个错误,各种搜索还是解决不了,然后去了GitHub的issues模块,果不其然有很多人也有同样的错误,同时发现作者说这个模块因为一些原因,不能实现转换的功能,然后给推荐了一段能够实现转换功能的python代码:
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())
我再这个代码基础上实现了trk文件到vtk文件的批量转换,代码如下:
import vtk from dipy.tracking.streamline import Streamlines from dipy.io.streamline import load_trk import os 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()) path0 = 'tractsegFiber' path_list0 = os.listdir(path0) ######删除隐藏文件夹###### for i in range(0, len(path_list0)): if path_list0[i][0] == '.': path_list0[i] = '' while '' in path_list0: path_list0.remove('') ######################### print("path_list0:") print(path_list0) print("len0:") print(len(path_list0)) path1 = 'tractsegFiber/' + path_list0[0] + '/tracts' path_list1 = os.listdir(path1) ######删除隐藏文件夹###### for i in range(0, len(path_list1)): if path_list1[i][0] == '.': path_list1[i] = '' while '' in path_list1: path_list1.remove('') ######################### print("path_list1:") print(path_list1) print("len1:") print(len(path_list1)) path_list_vtk = [None]*len(path_list1) for k in range(0, len(path_list_vtk)): path_list_vtk[k] = path_list1[k][:-4] print("path_list_vtk:") print(path_list_vtk) print("len_vtk:") print(len(path_list_vtk)) for h in range(0, len(path_list0)): for j in range(0, len(path_list1)): streams, hdr= load_trk('tractsegFiber/' + path_list0[h] + '/tracts/' + path_list1[j], 'same') streamlines = Streamlines(streams) saveStreamlinesVTK(streamlines, 'tractsegFiber/' + path_list0[h] + '/tracts/' + path_list_vtk[j] + '.vtk')
其中,进入load_trk(),然后找到load_generator()并进入
def f_gen(filename, reference, to_space=Space.RASMM,
to_origin=Origin.NIFTI, bbox_valid_check=False,
trk_header_check=True):
将上面代码中的Origin.NIFTI 改为Origin.TRACKVIS,这样最后得到的vtk和用trackvis转换得到的vtk相同。
由于本人水平有限,无法实现较高的通用性,有需要的可以根据自己的trk数据所在的路径修改代码,并欢迎在评论区讨论,下面是我的trk数据所在的路径:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。