Calculating the mean acquisitions

Dear all,

We have acquired the diffusion MRI data 4 times per subject (for almost all the subjects). These data are acquired in a first direction of phase encoding (x2), then in a second (x2).

Is it possible to calculate the mean of these 4 acquisitions to have one volume for each subject. If yes, could you help me with the corresponding command line?

Your help is well appreciated.


1 Like

Hi Abir

The command mrmath can calculate the mean of any number of input images for you:

mrmath dwi_1.mif dwi_2.mif dwi_rpe_1.mif dwi_rpe_2.mif mean output.mif

You’ll of course want to be sure that the images are properly aligned (coregistered) and on the same image grid first. :slight_smile:


Dear Fiona,

Thank you very much for your response, of course all the images are aligned and on the same grid.


Dear all,

After calculating the mean of these four acquisitions, I tried to generate a brain mask with the dwi2mask command line, but I have obtained this error message: [ERROR] DWI volumes could not be classified into b-value shells; gradient encoding may not represent a HARDI sequence.

Could you please help me to resolve it?

Thank you very much.

Mrmath may not preserve b value and bvecs in mif header. These are required by dwi2mask

From one dwi volume export bvalue and bvecs then explicitly use these in dwi2mask using either-fslgrad or -grad

Also I would distortion correct these using topup before averaging

Hi @TR-Abir,

Before going any further into this, it might be worth explaining what you’re trying to achieve? We generally recommend not averaging your data, but rather concatenating it into a single, larger dataset, which avoids all manner of issues due to e.g. motion or (as @Brad_Moffat mentions) different distortions. Can you provide a bit more context so we can give you the most relevant advice?


Dear @jdtournier and @Brad_Moffat,

Thank you very much for your responses,

In fact, we have acquired the diffusion data 4 times for each subject, we have made all the corrections (movement, noise, deformation) now I want to have a single volume of the 4 acquisitions, then we want to generate the brain mask to generating whole brain tractography.

Note that each acquisition has its own grad file, i.e. different bvecs, and the same b values.

How can I move forward?

Thanks for advance,

Hi Abir,

As I suggested above, the simplest way to deal with this is not to average (it looks like it wouldn’t be sensible anyway if these acquisitions don’t use the same gradient table), but to concatenate them to form a longer dataset, which can then be fed directly into e.g. dwi2mask, e.g.:

dwicat acq1.mif acq2.mif acq3.mif acq4.mif - | dwi2mask - mask.mif

However, can I check how you did your processing? If you processed each of these 4 acquisitions separately, you will probably find some residual misalignment between them, which means you won’t be able to average them immediately – you might need to perform some additional registration to align them, which isn’t ideal as that will introduce additional interpolation errors. To avoid this, I think it would have been better to concatenate the 4 datasets right at the start of the processing chain (using dwicat), and treat the whole thing as a single dataset.

Note that this assumes these 4 datasets were acquired in a single session – i.e. we’re not talking about 4 time points in a longitudinal study or something, in which case you’d obviously want to keep them separate.

However, if you want to go ahead with generating a mask from a single average, one approach would be to concatenate all 4 acquisitions, then average along the volume dimension to get a single overall mean over the entire dataset, and apply a simple thresholding on that:

mrcat acq1.mif acq2.mif acq3.mif acq4.mif -axis 3 - | mrthreshold - mask.mif

This might do a good enough job straight away, or you may need to tweak the threshold value to clean things up using the -abs option to mrthreshold. Or you could also feed that into FSL’s bet tool, which also produces good results most of the time.

In any case, I hope this provides some useful pointers…
All the best,


1 Like

Dear @jdtournier,

Thank you very much for this helpful explaination, you are right, the best way is to concatenate these 4 qcquistions because I treated theme separately, and finally I have generated the mask by concatenating the acquisitions, averaging along the volume dimension, and applying a simple thresholding as you suggested.

Thanks again,


1 Like