0

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.

Objective: I want a 1 step solution to convert .mnc files to .mha files

Code:

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)

Output:

(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".
Ayucathia
  • 5
  • 2

2 Answers2

0

It looks like you might have a permissions or path problem. SimpleITK can't seem to find the file. Try checking the permissions and put in a full path name.

Here's a little test program I wrote to check the MNC IO:

import SimpleITK as sitk
img = sitk.GaussianSource(sitk.sitkFloat32, [64,64,64])
sitk.WriteImage(img, "test.mnc")
img2 = sitk.ReadImage("test.mnc")
print(img2)
sitk.Show(img2)

It worked OK for me.

Dave Chen
  • 1,905
  • 1
  • 12
  • 18
  • I've added the full path, and still get errors. `RuntimeError: Exception thrown in SimpleITK ImageFileReader_Execute: /tmp/SimpleITK-build/ITK/Modules/IO/MINC/src/itkMINCImageIO.cxx:322: itk::ERROR: MINCImageIO(0x2e9e600): Could not open file "/home/russ/Documents/Testing_Space/ITK/subject04_wm_v.mnc".` With your code: `return _SimpleITK.Show(*args, **kwargs) RuntimeError: Exception thrown in SimpleITK Show: /tmp/SimpleITK/Code/IO/src/sitkShow.cxx:495: sitk::ERROR: No appropriate executable found.` – Ayucathia Oct 16 '19 at 02:04
  • When you ran my script, it looks like it failed on the last line, the call to sitk.Show. When Show is called, it tries to launch Fiji/ImageJ to display the image, which you might not have installed. But at least it means everything before the Show line executed properly. – Dave Chen Oct 16 '19 at 13:14
  • Yes that is correct. I don't actually need sitk.Show to work. I only need to convert the .mnc files to the .mha format. SITK can't read the file even when I've provided the full path. – Ayucathia Oct 17 '19 at 03:42
0

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.

Dave Chen
  • 1,905
  • 1
  • 12
  • 18