# Is it possible to export image + segment as .npz image?

**URL:** <https://discourse.slicer.org/t/is-it-possible-to-export-image-segment-as-npz-image/21794>\
**Category:** Support\
**Created:** [February 4, 2022, 2:32am UTC](https://discourse.slicer.org/t/is-it-possible-to-export-image-segment-as-npz-image/21794 "2022-02-04T02:32:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![codeenthusiast](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/codeenthusiast/32/14136_2.png) [@codeenthusiast](https://discourse.slicer.org/u/codeenthusiast)\
**Post date:** [February 4, 2022, 2:32am UTC](https://discourse.slicer.org/t/is-it-possible-to-export-image-segment-as-npz-image/21794/1 "2022-02-04T02:32:15Z")

</div>

I am working on an image registration platform on VoxelMorph and it can take .npz as input. Is it possible to save the volume and segmentation of an image in 3D slicer as .npz format?

---

<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:** [February 4, 2022, 4:22am UTC](https://discourse.slicer.org/t/is-it-possible-to-export-image-segment-as-npz-image/21794/2 "2022-02-04T04:22:19Z")

</div>

You can get the image and segmentation as a numpy array using `slicer.util.arrayFromVolume` and `slicer.util.arrayFromSegmentBinaryLabelmap` functions (see [examples](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#read-and-write-a-segment-as-a-numpy-array)). You can write a numpy array to a npz file using `np.savez`.

I would not recommend to use npz format, though, as I don’t think there is a standard way of storing image geometry in it (image origin, spacing, axis directions). Instead, you can save the volume and segmentation as nrrd or nifti files, which contain all essential metadata. VoxelMorph can read nifti files according to its [tutorials](https://github.com/voxelmorph/voxelmorph#registration).

Let us know how it all works. Common registration problems are solved quite nicely by ElastiX and ANTs toolkits, typically under 20 seconds for rigid transform and under 1 minute for deformable transform, so there is not a huge room for practically relevant improvements (maybe for large 4D volumes?). But if VoxelMorph works much faster and it is more robust and accurate than these classic toolkits then we might look into making it available directly in Slicer.

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [February 4, 2022, 1:51pm UTC](https://discourse.slicer.org/t/is-it-possible-to-export-image-segment-as-npz-image/21794/3 "2022-02-04T13:51:40Z")

</div>

If VoxelMorph works well please consider making a Slicer extension for it so that others can easily try it on their data. I haven’t tried VoxelMorph, but I’ve been told it is very efficient and robust.

---

<div class="post-metadata">

**Author:** ![codeenthusiast](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/codeenthusiast/32/14136_2.png) [@codeenthusiast](https://discourse.slicer.org/u/codeenthusiast)\
**Post date:** [February 7, 2022, 1:11am UTC](https://discourse.slicer.org/t/is-it-possible-to-export-image-segment-as-npz-image/21794/4 "2022-02-07T01:11:09Z")

</div>

Thanks very much for your reply. This is very helpful.

I have a follow-up question for this. I have to make sure that the dimensions for all my training images is equal. Is there a way to modify the dimensions in 3D slicer so that all images are the same from a dimension perspective?

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [February 7, 2022, 5:47pm UTC](https://discourse.slicer.org/t/is-it-possible-to-export-image-segment-as-npz-image/21794/5 "2022-02-07T17:47:04Z")

</div>

Yes, you can use `ResampleScalarVectorDWIVolume` and set a reference volume to be the same for all you data (assuming they are in the same space).

For some reason the documentation didn’t get into readthedocs yet, but here’s the 3.6 version of the documentation:  
[https://www.slicer.org/wiki/Modules:ResampleScalarVectorDWIVolume-Documentation-3.6](https://www.slicer.org/wiki/Modules:ResampleScalarVectorDWIVolume-Documentation-3.6)

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [February 7, 2022, 5:50pm UTC](https://discourse.slicer.org/t/is-it-possible-to-export-image-segment-as-npz-image/21794/6 "2022-02-07T17:50:14Z")

</div>

@pieper The `ResampleScalarVectorDWIVolume` documentation is in the readthedocs:

[https://slicer.readthedocs.io/en/latest/user\_guide/modules/resamplescalarvectordwivolume.html](https://slicer.readthedocs.io/en/latest/user_guide/modules/resamplescalarvectordwivolume.html)

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [February 7, 2022, 5:54pm UTC](https://discourse.slicer.org/t/is-it-possible-to-export-image-segment-as-npz-image/21794/7 "2022-02-07T17:54:14Z")

</div>

Ah, good, thanks for finding it James. I had searched for the module name which didn’t work.
