I have a 4D numpy array and would like to write it as a 4D non-vector MHA image. Unfortunately, GetImageFromArray apparently cannot do that. I can edit the file header manually after writing to disk; it achieves the result I’m looking for, but I’m wondering if there is a cleaner way to do that, using SimpleITK ideally, but itk python bindings would be OK too.
SimpleITK in Python has a function GetImageFromArray that theoretically ought to convert a 4d numpy array to a SimpleITK image. That ought to solve your problem, but I tried it, and doesn’t work correctly for 4d image. This is a problem that needs to be rectified, obviously.
Until we can do so, here is an example that takes a 4d numpy array, extracts each 3d slice as a SimpleITK image, and then joins the slices into one 4D image.
#! /usr/bin/env python
import numpy as np
import SimpleITK as sitk
np_array = np.zeros( (10,10,10,10) )
tdim = np_array.shape[3]
slices = []
for t in range(tdim):
slices.append( sitk.GetImageFromArray( np_array[t], False ) )
img = sitk.JoinSeries(slices)
sitk.WriteImage(img, "test.mha")