# Converting Segmentation to Volume automatically w/ Python or Matlab

**URL:** <https://discourse.slicer.org/t/converting-segmentation-to-volume-automatically-w-python-or-matlab/16829>\
**Category:** Development\
**Tags:** registration, segmentation, python, nifti\
**Created:** [March 29, 2021, 7:54pm UTC](https://discourse.slicer.org/t/converting-segmentation-to-volume-automatically-w-python-or-matlab/16829 "2021-03-29T19:54:35Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [April 3, 2021, 2:50am UTC](https://discourse.slicer.org/t/converting-segmentation-to-volume-automatically-w-python-or-matlab/16829/2 "2021-04-03T02:50:04Z")

</div>

You can load the segmentation and export to volume file. If you want the segmentation to have a particular geometry (origin, spacing, axis directions, and extents) then specify the CT volume as reference volume when you export to volume file (when you call `ExportSegmentsBinaryLabelmapRepresentationToFiles`).

See complete example here:

> [@Overlapping Segmentions Export to NIFTI](https://discourse.slicer.org/t/overlapping-segmentions-export-to-nifti/15773/13):
>
> Great idea, working perfectly now. Thanks so much for your extensive help. In case anyone else wants to do something similar, here is my final code: # \*\*\*\*\* THE FINAL SCRIPT \*\*\*\*\* # Required to have 3 segments - "Mask", "Single", "Multi" # Save Image data as Image Data.nii.gz node = slicer.mrmlScene.GetFirstNodeByClass('vtkMRMLScalarVolumeNode') file\_path = "/Users/pete/OneDrive/ImageData.nii.gz" properties = {'useCompression': 1}; #use compression slicer.util.saveNode(node, file\_path, prope…

---

_[View the full topic](https://discourse.slicer.org/t/converting-segmentation-to-volume-automatically-w-python-or-matlab/16829)._
