MRconvert nifti to mif with Gradient scheme, transformation matrix and the other header things

Hi MRTrixler,

I have to convert my *.mif files to nifti to make some Interpolation resolution changes on my data. Now I would like to convert them back with all that I need. The question is: what do I need! The gradient scheme is not the problem, with -grad.
Question 1: do i need for all the other MRTrix Function something else from the ex-mif-header?

Question 2: What is with the transformation Matrix? It looks different, if i convert all back to mif. (I also have to make some 3D data to 4D during the back-to-mif converting). I I interpolate to a higher resolution, the transformation matrix should be the same like bevor the whole mif to nifti.

Can I somehow rewrite the transformation matrix or would that be wrong? Is there a need or an explanation or som ehints?

Thanks for the help,

Ralf

Can I ask what you need to do and why this can’t be done using mrgrid? You can choose the interpolation method using the -interp option.

Depends entirely on what you did to the data before then, and what you plan to do with the data afterwards. More concretely, whether there was anything in the mif header that got lost in the conversion to NIfTI and back that you would need in downstream applications. That might include information about the acquisition, the PE direction table, the command history, etc. But we can’t know what you might need without a very detailed description of what you plan to do with the data.

You can probably avoid any losses by exporting and re-importing all the required information using a JSON file (use the relevant options to mrconvert).

Any change in resolution will typically entail at least a change in the origin (the 4th column of the transform). This is because it corresponds to the centre of voxel (0,0,0), so if you for example you halve the voxel size, but keep the FoV strictly the same, the centre of the voxel at (0,0,0) will be closer to the corner of the FoV than the original. Hope that makes sense…

You may also see other differences depending on whether whatever processing you’ve performed has changed the strides. Stride handling is a pretty complex topic, and even more complex when you throw NIfTI into the mix. So I’ll refer you to the relevant pages in the docs. But that doesn’t mean it’s necessarily incorrect…

Reassuring that what I typed up pretty much follows Donald’s answer but he’s a faster typer… in case it’s useful, a bit more detail:

You can export all data from a header in a few ways:

  • mrinfo -json_all creates a json file that included all header information, also that related to spatial information so is tied to the image resolution.
  • mrinfo -json_keyval exports only the header key/value entries
  • mrconvert -json_export and -json_import also handle key/value entries
  • the config file option NIfTIAutoSaveJSON creates json files with the information otherwise lost by converting from .mif to nifti.

The transformation matrix relates the voxel grid to real (scanner) space and is not constant when manipulating the image grid:


For instance, if you change the resolution or crop or pad the image, the location in scanner space of a given voxel (i,j,k) might change due to a change in its indexing, so you’d need to adjust the translation component of the transformation.

You can display above quantities using mrinfo. For example, reducing the image resolution by 80%:

mrinfo i.mif -size -spacing -transform
99 99 64 1
1.51515 1.51515 1.5 1
                1                -0                 0 -82.1688461303711
               -0                 1                 0 -54.4631576538086
               -0                -0                 1  -28.134614944458
                0                 0                 0                 1

mrgrid i.mif regrid -scale 0.8 - | mrinfo - -size -spacing -transform
mrgrid: [100%] reslicing "i.mif"
80 80 52 1
1.875 1.875 1.84615 1
                1                -0                 0 -81.9889220678711
               -0                 1                 0 -54.2832335913086
               -0                -0                 1 -27.9615380213811
                0                 0                 0                 1

1 Like

Thanks you both for your answer,

I will defnitly switch to json-file. It will help a lot. Didnt know that MRTrix is capable to do it with json.

Resolution:
I didnt know about mrgrid. I gues I should … from time to time…go trough the Command list of MRTrix.
I use the 7th order B-Spline from SPM to change the resolution. Do you have something similar or also 7th order B spline? I will defnitly test yours too.

Transformation:
Thanks for the clarification with the transformation. I understand completly. I learned a lot about MRTrix today.

:slight_smile:

Yours,

Ralf

We don’t have a 7th order spline interpolation filter but the supported interpolation methods include a sinc filter (with window size 7) which I guess comes closest to that.

Thank you… I try this right in this moment. Would be nice to be able to stay inside MRTrix and FSL!

Ralf

Now I would like to convert them back with all that I need.
Question 1: do i need for all the other MRTrix Function something else from the ex-mif-header?

If I interpret your intended question here as that you wish to get the header contents from the originating .mif into the one that was produced from an intermediate NIfTI, with 3.0.0 there’s a new command-line option in mrconvert: -copy_properties will bring all key-value entries from the header of one image into the header of another.