Created
August 17, 2018 09:26
-
-
Save kmader/dc22fdf1d3d2244c6752925133e64ef5 to your computer and use it in GitHub Desktop.
convert volumes to labelmaps in Slicer3D
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | |
slicer.mrmlScene.Clear(0) | |
[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