How can I reshape a nrrd segmentation file to a reference volume shape?

Hi everyone! I have a big dataset that includes nifti files and their corresponding nrrd segmentation files. Most of the segmentations were exported with an older version of Slicer and without selecting a reference volume. Therefore, there’s a shape mismatch between the segmentations and the nifti volumes. I don’t want to export every case again one by one in Slicer. Is there a way that I can do this with python code?

check the relevant sections of the script repository. I believe everythign you need to build a python based workflow is explained there

Thank you! I’m now using the SlicerJupyter extension to run slicer code.
However, I’m not able to load the segmentation.

seg = slicer.util.loadSegmentation(“Segmentation.seg.nrrd”)

RuntimeError: Failed to load node from file: Segmentation.seg.nrrd
Error: Loading Segmentation.seg.nrrd - load failed.

I’m able lo load it as a Volume though. But then, I can’t export the case with:
slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(seg, labelmapVolumeNode, ct)

because it’s expecting a vtkMRMLSegmentationNode.

Thank you.

It works for me:

Are you sure the filepath is correct, or indeed this is a segmentation file (as opposed to a labelmap?).

What happens when you drag and drop the same file in the Slicer? Does it load?


yes. When I drag and drop the segmentation I get this log:

“Segmentation” Reader has successfully read the file “/home/blanca/Documents/3D-SAM-Slicer/Slicer-5.6.1-linux-amd64/Segmentation.seg.nrrd” “[0.07s]”

and my segmentation gets loaded.

however, if I use the command to load the segmentation, then I get this log:

vtkMRMLSegmentationStorageNode::ReadDataInternal: Segmentation file ‘/home/blanca/Documents/Segmentation.seg.nrrd’ is not found while trying to read node (vtkMRMLSegmentationStorageNode1).

vtkMRMLStorageNode::ReadData: Failed to read node Segmentation (vtkMRMLSegmentationNode1) from filename=‘Segmentation.seg.nrrd’

LoadSegmentationFromFile: Error reading Segmentation.seg.nrrd

Path in your command line is not the same as what you have loaded when you drag and drop. Error is clear, it is not finding the file.

That’s right. The current directory in my jupyter notebook with the slicer kernel is already ‘/home/blanca/Documents/3D-SAM-Slicer/Slicer-5.6.1-linux-amd64’ so I only have to give the file name (it works if I load it as a volume so it shouldn’t be a path issue).

I am not sure how the paths are intepreted through Jupyter Notebook, perhaps it doesn’t translate. I would suggest using the full path of the file and see it works.

Thank you! I would’ve never said it wasn’t finding the path since I used the relative ct volume path and that worked…