stl file stores the annotation results of CT data, and I want to convert it to nii format for deep learning.

Hello sir, I don’t know if you have time, but I would like to ask you a question.

My situation is the same as yours, the stl file stores the annotation results of CT data, and I want to convert it to nii format for deep learning. I reproduced the code from your post into the Python interactor in slicer, but I keep getting

referenceVolumeNode = slicer.util.loadVolume(reference_volume_path), which keeps giving me an error about not being able to load noad from file.

I’m very confused and I’m looking forward to your help when you have time.

Below is my code

import os
import slicer
stl_path = r"D:\change\stl"
image_path = r"D:\change\dicom"
out_path = r"D:\change\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)

1 Like