Last active
October 7, 2021 00:50
-
-
Save anibalsolon/040e598c5974290416f7160b26bfc4b8 to your computer and use it in GitHub Desktop.
NRRD segmentation to Nifti using Slicer Python API
This file contains 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
""" | |
Usage: | |
Slicer --python-script nrrd2nifti.py --no-splash --no-main-window -- Segmentation.seg.nrrd | |
""" | |
import os | |
import sys | |
if len(sys.argv) == 1: | |
print("Usage: nrrd2nifti.py <nrrd file (.nrrd)>") | |
sys.exit(1) | |
if len(sys.argv) == 2 and not sys.argv[1].endswith(".nrrd"): | |
print("Usage: nrrd2nifti.py <nrrd file (.nrrd)>") | |
sys.exit(1) | |
segmentation_filename = os.path.abspath(sys.argv[1]) | |
segmentationNode = slicer.util.loadSegmentation(segmentation_filename) | |
labelmapNode = slicer.vtkMRMLLabelMapVolumeNode() | |
slicer.mrmlScene.AddNode(labelmapNode) | |
slicer.modules.segmentations.logic().ExportAllSegmentsToLabelmapNode( | |
segmentationNode, labelmapNode, slicer.vtkSegmentation.EXTENT_REFERENCE_GEOMETRY | |
) | |
output_file = segmentation_filename.replace('.nrrd', '.nii.gz') | |
slicer.util.saveNode(labelmapNode, output_file) | |
print('Segmentation saved to', output_file) | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment