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

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?

You can get the image and segmentation as a numpy array using slicer.util.arrayFromVolume and slicer.util.arrayFromSegmentBinaryLabelmap functions (see examples). 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.

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.

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.

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?

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

@pieper The ResampleScalarVectorDWIVolume documentation is in the readthedocs:

https://slicer.readthedocs.io/en/latest/user_guide/modules/resamplescalarvectordwivolume.html

1 Like

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