I want to convert .mnc files from BrainWeb (https://brainweb.bic.mni.mcgill.ca/brainweb/anatomic_normal_20.html) to .mha file format for use in TumorSim (https://www.nitrc.org/projects/tumorsim/).
I have tried converting the file from .mnc to .nii using nibabel and mnc2nii, and then converting the .nii file to the .mha format.
However, this process leads to the file size increasing dramatically (from 56.9 MB .mha to 56.9~227.5 MB .nii depending on output voxel format)
From there, converting the .nii file to the .mha format retains the same file size. The size of .mha files used in TumorSim are around 4.8 MB.
import SimpleITK as sitk
inputImageFileName = 'subject04_wm_v.mnc'
outputImageFileName = 'white_matter.mha'
reader = sitk.ImageFileReader()
reader.SetImageIO("MINCImageIO")
reader.SetFileName(inputImageFileName)
image = reader.Execute()
writer = sitk.ImageFileWriter()
writer.SetFileName(outputImageFileName)
writer.Execute(image)
(py3env) russ@russ-Latitude-E5450:~/Documents/Testing_Space/ITK$ python mncconverter.py
/tmp/SimpleITK-build/ITK/Modules/ThirdParty/MINC/src/libminc/libsrc2/volume.c:1399 (from MINC): Unable to open file 'subject04_wm_v.mnc'
Traceback (most recent call last):
File "mncconverter.py", line 9, in <module>
image = reader.Execute()
File "/home/russ/Documents/freesurfer/psacnn_brain_segmentation/py3env/lib/python3.6/site-packages/SimpleITK/SimpleITK.py", line 8654, in Execute
return _SimpleITK.ImageFileReader_Execute(self)
RuntimeError: Exception thrown in SimpleITK ImageFileReader_Execute: /tmp/SimpleITK-build/ITK/Modules/IO/MINC/src/itkMINCImageIO.cxx:322:
itk::ERROR: MINCImageIO(0x2de7600): Could not open file "subject04_wm_v.mnc".
After trying to load a BrainWeb image myself, I found this web page that describes the problem: https://www.slicer.org/wiki/How_to_read_.mnc_files_using_ITK
The issue is that BrainWeb images are stored in MINC 1 format, while ITK/SimpleITK read MINC 2.
There is a utility, mincconvert that converts from 1 to 2 that will allow ITK to read the images: http://bic-mni.github.io/man-pages/man/mincconvert.html
The other option is to download the images from BrainWeb in the raw format, and then create a MHA header with the proper dimensions. MHA's header is text, so that wouldn't be too hard, either.