Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
convert volumes to labelmaps in Slicer3D
from glob import glob
import os
cur_glob = '/Volumes/NO NAME/*seg.nii'
files_to_process = glob(cur_glob)
print('Processing', len(files_to_process), 'files')
def process_file(in_path):
[success, loadedVolumeNode] = slicer.util.loadVolume(in_path, returnNode=True)
outputLabelMap = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode')
vol_logic = slicer.modules.volumes.logic()
# convert the volume
vol_logic.CreateLabelVolumeFromVolume(slicer.mrmlScene, outputLabelMap, loadedVolumeNode)
path_name, file_name = os.path.split(in_path)
file_prefix, file_suffix = os.path.splitext(file_name)
return slicer.util.saveNode(outputLabelMap, os.path.join(path_name, '{}_converted.nii'.format(file_prefix)))
for c_file in files_to_process: process_file(c_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.