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

**URL:** <https://discourse.slicer.org/t/how-can-i-reshape-a-nrrd-segmentation-file-to-a-reference-volume-shape/34276>\
**Category:** Support\
**Created:** [February 13, 2024, 3:05am UTC](https://discourse.slicer.org/t/how-can-i-reshape-a-nrrd-segmentation-file-to-a-reference-volume-shape/34276 "2024-02-13T03:05:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![binigoromillo](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/binigoromillo/32/69325_2.png) [@binigoromillo](https://discourse.slicer.org/u/binigoromillo)\
**Post date:** [February 13, 2024, 3:05am UTC](https://discourse.slicer.org/t/how-can-i-reshape-a-nrrd-segmentation-file-to-a-reference-volume-shape/34276/1 "2024-02-13T03:05:12Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [February 13, 2024, 4:51am UTC](https://discourse.slicer.org/t/how-can-i-reshape-a-nrrd-segmentation-file-to-a-reference-volume-shape/34276/2 "2024-02-13T04:51:40Z")

</div>

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

[https://slicer.readthedocs.io/en/latest/developer\_guide/script\_repository.html#segmentations](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#segmentations)

---

<div class="post-metadata">

**Author:** ![binigoromillo](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/binigoromillo/32/69325_2.png) [@binigoromillo](https://discourse.slicer.org/u/binigoromillo)\
**Post date:** [February 14, 2024, 7:50pm UTC](https://discourse.slicer.org/t/how-can-i-reshape-a-nrrd-segmentation-file-to-a-reference-volume-shape/34276/3 "2024-02-14T19:50:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [February 14, 2024, 7:59pm UTC](https://discourse.slicer.org/t/how-can-i-reshape-a-nrrd-segmentation-file-to-a-reference-volume-shape/34276/4 "2024-02-14T19:59:19Z")

</div>

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?

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/4/84332d89da3d6d43ef4f17ade17ff2b39c9c4fe6.png)  
f

---

<div class="post-metadata">

**Author:** ![binigoromillo](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/binigoromillo/32/69325_2.png) [@binigoromillo](https://discourse.slicer.org/u/binigoromillo)\
**Post date:** [February 14, 2024, 8:06pm UTC](https://discourse.slicer.org/t/how-can-i-reshape-a-nrrd-segmentation-file-to-a-reference-volume-shape/34276/5 "2024-02-14T20:06:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![binigoromillo](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/binigoromillo/32/69325_2.png) [@binigoromillo](https://discourse.slicer.org/u/binigoromillo)\
**Post date:** [February 14, 2024, 8:08pm UTC](https://discourse.slicer.org/t/how-can-i-reshape-a-nrrd-segmentation-file-to-a-reference-volume-shape/34276/6 "2024-02-14T20:08:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [February 14, 2024, 8:16pm UTC](https://discourse.slicer.org/t/how-can-i-reshape-a-nrrd-segmentation-file-to-a-reference-volume-shape/34276/7 "2024-02-14T20:16:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![binigoromillo](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/binigoromillo/32/69325_2.png) [@binigoromillo](https://discourse.slicer.org/u/binigoromillo)\
**Post date:** [February 14, 2024, 8:29pm UTC](https://discourse.slicer.org/t/how-can-i-reshape-a-nrrd-segmentation-file-to-a-reference-volume-shape/34276/8 "2024-02-14T20:29:22Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [February 14, 2024, 8:34pm UTC](https://discourse.slicer.org/t/how-can-i-reshape-a-nrrd-segmentation-file-to-a-reference-volume-shape/34276/9 "2024-02-14T20:34:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![binigoromillo](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/binigoromillo/32/69325_2.png) [@binigoromillo](https://discourse.slicer.org/u/binigoromillo)\
**Post date:** [February 14, 2024, 8:38pm UTC](https://discourse.slicer.org/t/how-can-i-reshape-a-nrrd-segmentation-file-to-a-reference-volume-shape/34276/10 "2024-02-14T20:38:25Z")

</div>

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