# How to export an individual .nrrd file for each segmented structure?

**URL:** <https://discourse.slicer.org/t/how-to-export-an-individual-nrrd-file-for-each-segmented-structure/39968>\
**Category:** Support\
**Tags:** segmentation\
**Created:** [November 1, 2024, 12:57am UTC](https://discourse.slicer.org/t/how-to-export-an-individual-nrrd-file-for-each-segmented-structure/39968 "2024-11-01T00:57:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mitchellwiebe](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@mitchellwiebe](https://discourse.slicer.org/u/mitchellwiebe)\
**Post date:** [November 1, 2024, 12:57am UTC](https://discourse.slicer.org/t/how-to-export-an-individual-nrrd-file-for-each-segmented-structure/39968/1 "2024-11-01T00:57:48Z")

</div>

Hi there,

I’m working on a project where I will extract features from \>200 individuals across several structures (and for CT/dose). As part of the workflow, I feel that it would be most convenient to export a .nrrd file (or .seg.nrrd) for each segmented structure, resulting in as many binary masks as I have structure segmentations. However, when I try to do so, I end up with less masks than total structures (non-overlapping structure are ‘forced’ into the same mask it seems).

I’m sure there’s a nice way to do so but this is giving me trouble! Any advice would be greatly appreciated.

---

<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:** [November 1, 2024, 2:51am UTC](https://discourse.slicer.org/t/how-to-export-an-individual-nrrd-file-for-each-segmented-structure/39968/2 "2024-11-01T02:51:19Z")

</div>

If you look into the script repository, there is an example of exporting a labelmap from a named segment:

[https://slicer.readthedocs.io/en/latest/developer\_guide/script\_repository.html#export-labelmap-node-from-segmentation-node](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#export-labelmap-node-from-segmentation-node)

You can loop over segments, and expand this example.

---

<div class="post-metadata">

**Author:** ![mitchellwiebe](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@mitchellwiebe](https://discourse.slicer.org/u/mitchellwiebe)\
**Post date:** [November 1, 2024, 9:07pm UTC](https://discourse.slicer.org/t/how-to-export-an-individual-nrrd-file-for-each-segmented-structure/39968/3 "2024-11-01T21:07:40Z")

</div>

Thank you for the response! This was right where I needed to be pointed to.

I put together the lines of code that were of use to me. The final working code is as follows. 🙂

```auto
# Loops through all of the existing segmentations and saves them to a file one-by-one (as .nrrd)

# Set output directory
outputPath = r"path\to\savefolder"

def exportEachSegmentAsLabelmap():
    # Get segmentation and reference volume nodes
    segmentationNode = slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLSegmentationNode")
    referenceVolumeNode = slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLScalarVolumeNode")
    segmentationNode.GetSegmentation().CreateRepresentation("BinaryLabelmap")
    
    if not segmentationNode or not referenceVolumeNode:
        print("Segmentation or reference volume node not found.")
        return
    
    # List of segment names you want to export
    segmentNames = list(segmentationNode.GetSegmentation().GetSegmentIDs())
    
    # Iterate over each segment name
    for segmentName in segmentNames:
        print(f"Exporting segment: {segmentName}")
        
        # Get segment ID by name
        segmentId = segmentationNode.GetSegmentation().GetSegmentIdBySegmentName(segmentName)
        
        if not segmentId:
            print(f"Segment {segmentName} not found.")
            continue
        
        # Create a vtkStringArray and add the current segment ID
        segmentIds = vtk.vtkStringArray()
        segmentIds.InsertNextValue(segmentId)
        
        # Create a new labelmap volume node for the export
        labelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLLabelMapVolumeNode")
        
        # Export the segment to the labelmap
        slicer.vtkSlicerSegmentationsModuleLogic.ExportSegmentsToLabelmapNode(
            segmentationNode, segmentIds, labelmapVolumeNode, referenceVolumeNode
        )
        
        # Define file path and save each labelmap with the segment name
        filepath = os.path.join(outputPath, f"{segmentName}-label.nrrd")
        slicer.util.saveNode(labelmapVolumeNode, filepath)
        
        # Clean up labelmap node from the scene
        slicer.mrmlScene.RemoveNode(labelmapVolumeNode)
        
        print(f"Segment {segmentName} saved to {filepath}")

# Run the export function directly
exportEachSegmentAsLabelmap()

```
