Transform gradients


is it possible to transform the gradients file when using mrrtransform to get the diffusion image in to another space?



mrtransform does this by default. From the docs:

If a DW scheme is contained in the header (or specified separately), and the number of directions matches the number of volumes in the images, any transformation applied using the -linear option will be also be applied to the directions.

If by ‘gradients file’, you’re referring to separate bvecs/bvals files, then that is also possible, you’d just need to specify the input files using the -fslgrad option (which inserts the directions into the image’s header internally), and extract them again somehow – ideally this would be done using the -export_grad_fsl option, but I note this isn’t currently implemented in mrtransform (it’s a trivial fix, we’ll make sure it’s there for the next release). To get around that, you can use a construct like:

mrtransform dwi.nii -fslgard bvecs bvals -linear transformation.txt - | mrconvert - dwi_transformed.nii -export_grad_fsl bvecs_transformed bvals_transformed

Incidentally, the need to constantly supply the correct bvecs & bvals files all the time, with all the scope for human error this introduces, is one of the reasons why we recommend the use of the MRtrix .mif format – this information is then an integral part of the image header, and lives in the same file.

Thanks alot for the reply!
so jes i mean bvecs and bvals, but they are in the mrtrix format, and embedded in the header info. though they are not changing after i use mrtransform, despite the fact that the image does correctly transform.

however, if i run the command you just suggested

mrtransform dwi.nii -fslgard bvecs bvals -linear transformation.txt - | mrconvert - dwi_transformed.nii -export_grad_fsl bvecs_transformed bvals_transformed

with my version being

mrtransform data_mas.mif -grad all_data_grads.txt -interp nearest -linear mni_2_data_all_flirt_7_inv.mat -template mni_2_data_all_flirt_7_inv.nii.gz - | mrconvert - data_mas_mrtrans_mni_grads.mif -export_grad_mrtrix grads_transformed -datatype float32 -force -strides 1,2,3,4 

then the gradients DO change, but the transformed image is empty - though the header information matches the correctly transformed version - which can be obtained with this command…

mrtransform data_mas.nii.gz data_mas_mrtrans_mni.nii.gz -template mni_2_data_all_flirt_7_inv.nii.gz -grad all_data_grads.txt -datatype float32 -force -strides 1,2,3,4 -interp nearest

I imagine the transformation matrix itself is incompatible? it was obtained using a rigid transform in flirt with 7dofs instead of 6…

many thanks for your help so far



That’s the most likely problem. The transform information provided by flirt maps voxel space of one onto the voxel space of the other. mrtransfom expects a mapping from real/scanner space to real/scanner space. Use transformconvert to convert between the two.

Worth noting that the above command only regrids the image – it does not alter the position of the anatomy with respect to real space. Since the anatomy is still oriented the same, the DW gradients (which in MRtrix3 are defined in real space) are also unaffected. That’s why you’re seeing no effect on the gradient table with that command.

You’ll probably find that the corresponding bvecs will have changed though, if you were to produce them with mrconvert -export_fsl_grad, since they’re defined relative to the image axes – which will have been modified by the regridding operation.

If you do intend to actually move the anatomy, not just remap it onto a different voxel grid, then you need to supply an option that does this, e.g. -linear. But you’ll need to fix up your transformation first…