Mrconvert and mrview: Error parsing auto align header entry for MGH format

Hello MRtrix experts,
I’ve run into a problem viewing or running mrconvert on a few .mgz files output by Freesurfer’s recon-all. I get the above message error, and then: “invalid number of entries on line 0 (5 should be 4).”

If I use freesurfer’s tool to view the auto align header (mri_info nu.mgz --autoalign) I get a 4x4 matrix. Any idea what the issue is? It seems to only affect my .mgz files that have anything other than an identity matrix as the auto align header.

Any help would be greatly appreciated!


Hi John,

It’s probably a small glitch remaining in the MGH handling code arising from the fact that I’ve never seen a .mgz file with anything other than an identity auto-align matrix :neutral_face:

As you can see here, a fair bit of effort went into proper handling of the additional “header” (“footer”?) fields in this file format, despite them not actually comprehensively documented anywhere. So I’m hoping that it will be a relatively simple fix if you can get me access to an example image.


Thanks, Rob. I’ve messaged you with a link to one of the offending files.

Sorry for the hassle! I really appreciate the help.



Thanks for the data; I’ll see what I can do.
In the meantime, there appears to be a workaround that you can use straight away:

cp nu.mgz nu.mgh.gz
gunzip nu.mgh.gz
mrconvert nu.mgh <output>

Just don’t ask me why this works but loading the .mgz directly doesn’t :man_shrugging:

Thanks! This worked perfectly.

Sorry to push you back into header hell – it looks like you guys already put a lot of work into this. For what its worth, I really appreciate all the effort you all put into making MRtrix so “human usable.” It is by far my favorite neuroimaging package to use for that reason.

Actually, upon further inspection, either mrconvert or mrtransform does not appear to work correctly after using this unzipping technique.

For context, my pipeline is as follows (ignoring some details for brevity):
mrconvert nu.mgh nu.nii
epi_reg --epi=b0_preproc_bias.nii --t1=nu.nii […] --out epi2struct
transformconvert epi2struct.mat b0_preproc_bias.nii nu.nii flirt_import epi2struct_mrtrix.mat
mrtransform nu.nii -linear nu_dMRIreg.nii -inverse

This process works very well with the images I didn’t have to use the mgh unzip workaround on, but comes out misaligned when I use the workaround.

I think the problem may arise at the mrconvert stage. I noticed that when I run “mrview nu.mgh”, it shows up in the (normal) axial view. However, when I run “mrview nu.nii”, it shows up in a sagittal view. However, when I choose a particular view (e.g., axial, sagittal, coronal) from the view menu, it goes to the correct view.

Any help is appreciated!

OK, that’s a bit weird… But in any case, @rsmith has already gone and pushed a fix for this, which I approved earlier. So you should be able to update with the usual git pull && ./build, and see if that fixes things…?

Thank you @rsmith and @jdtournier! This fix did the trick!

1 Like