0

Is there a way in Python or any other language to convert .raw/mhd image data to Nifti/nii?

I can load the .raw/mhd file in python via SimpleITK as in this post: Reading *.mhd/*.raw format in python

import skimage.io as io
img = io.imread('file.mhd', plugin='simpleitk')

I am having a hard time exporting as nii with proper dimensions... would ideally use the header information in the original mhd file...

Thanks

user844597
  • 35
  • 2
  • 7

1 Answers1

3

You should be able to just do in with SimpleITK. You would do something like this:

import SimpleITK as sitk

img = sitk.ReadImage("input.mhd")
sitk.WriteImage(img, "output.nii")

If you don't have SimpleITK in python, installing it as follows:

pip install SimpleITK

SimpleITK does its best effort of preserving all header information, although it's not perfect. Hopefully the voxel dimensions will be preserved.

Dan
  • 15
  • 6
Dave Chen
  • 1,905
  • 1
  • 12
  • 18
  • Is it possible to write a dicom series instead of a single dicom image ? – Tee Jul 10 '20 at 17:31
  • 1
    Yes, here is an example of how to write out a dicom series. Most of the work is in getting the meta-data tags right. https://simpleitk.readthedocs.io/en/master/link_DicomSeriesFromArray_docs.html – Dave Chen Jul 10 '20 at 19:18