1. 问题描述
我的stl文件存的是CT数据的标注结果,不转的话一些常用的可视化软件(比如ITKSnap)不支持,而且后面想做深度学习写数据接口的话会比较麻烦。而且我的数据有些多,一百多个,排除用软件一个个另存为的方式(这种方式可以用Mimics)
2. 解决办法:
用3D Sclier的Python interactor编程界面循环转换:下载安装3D Sclier之后,再输入下面的代码
import os
import slicer
stl_path = r"path\annotations"
image_path = r"path\images"
out_path = r"path\annotations_nii"
patients = os.listdir(stl_path)
for patient in patients:
patient_path = os.path.join(stl_path, patient)
stl_files = os.listdir(patient_path)
output_file_path = os.path.join(out_path, patient)
os.makedirs(output_file_path, exist_ok=True)
reference_volume_path = os.path.join(image_path, patient+".nii.gz")
referenceVolumeNode = slicer.util.loadVolume(reference_volume_path)
for stl_file in stl_files:
stl_file_name = os.path.join(patient_path, stl_file)
output_file_name = os.path.join(output_file_path, stl_file[0:-4] + ".nii.gz")
segmentationNode = slicer.util.loadSegmentation(stl_file_name)
outputLabelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode')
slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(segmentationNode, outputLabelmapVolumeNode,
referenceVolumeNode)
slicer.util.saveNode(outputLabelmapVolumeNode, output_file_name)
slicer.mrmlScene.Clear(0)
每转成功一个就会输出一个True
3. 其他说明
(1)最后一行的代码是清除导入到3D Slicer的数据的,不加这一行多次循环后会out of memory,因为你重新加载分割结果的时候上一次加载的不会在3D Slicer显示窗口被清除覆盖
slicer.mrmlScene.Clear(0)
(2)主要参考资料: