Coregistrtaion of Freesurfer output and diffusion image

Hi all,

I tried to register my atlas-based volumetric parcellation and segmentation to diffusion space to create a structural connectivity map. Nevertheless, after applying the inverse transform matrix which I obtained from linear registrating diffuison to originial T1 space to the Freesurfer output file, the output images were not perfectly matched. I checked the freesurfer output image and found it matched the original T1, so I’m not sure what is wrong. Do you have any suggestion on improving the registration step?

flirt -in T1.nii.gz -ref DWI.nii.gz -interp nearestneighbour -dof 6 -omat %s/B0DWI2T1.mat
transformconvert %s/B0DWI2T1.mat -in %s %s flirt_import %s/DWI2T1_mrtrix.txt
mrtransform %s/T1_Parcels.mif -fslgrad %s %s -linear %s/DWI2T1_mrtrix.txt -inverse %s/%s_Parcel2DWI.mif -force


Hi @Sisimo, and welcome to the mrtrix community!

You can try this, adapted from QSIPrep’s workflow, after defining the variables PREPROC_T1 (preprocessed t1 image aligned with DWI), T1_BRAIN_MASK (brain mask of T1 image), FS_BRAIN (FreeSurfer brain file, converted to .nii), TMPDIR (just a place to put outputs), subject (subject ID):

antsRegistration --collapse-output-transforms 1 \
    --dimensionality 3 --float 0 \
    --initial-moving-transform [ ${PREPROC_T1}, ${FS_BRAIN}, 1 ] \
    --initialize-transforms-per-stage 0 --interpolation BSpline \
    --output [ ${TMPDIR}/${subject}/transform, ${TMPDIR}/${subject}/transform_Warped.nii.gz ] \
    --transform Rigid[ 0.1 ] \
    --metric Mattes[ ${PREPROC_T1}, ${FS_BRAIN} 1, 32, Random, 0.25 ] \
    --convergence [ 1000x500x250x100, 1e-06, 10 ] \
    --smoothing-sigmas 3.0x2.0x1.0x0.0mm --shrink-factors 8x4x2x1 \
    --use-histogram-matching 0 \
    --masks [ ${T1_BRAIN_MASK}, NULL ] \
    --winsorize-image-intensities [ 0.002, 0.998 ] \
    --write-composite-transform 0


# Convert ANTs .mat transform to .txt, and rename it
ConvertTransformFile 3 \
    ${TMPDIR}/${subject}/transform0GenericAffine.mat \

# Convert ANTs transform to MRTrix compatible transform 
transformconvert \
    ${TMPDIR}/${subject}/${subject}_from-FS_to-T1wACPC_mode-image_desc-ITK_xfm.txt \
    itk_import \

That transformation can be used to register the freesurfer brain to DWI outputs.


Hi Sisimo,

If you intend to bring T1 into DWI space (according to your first sentence), the way you’ve done it (flirt -in T1 -ref DWI) and then applying the inverse, actually brings DWI into T1 space, so you have to change your reference and input scan order in the flirt command.

Nonetheless, if you want to do it with a linear transformation such as flirt, in my experience, epi_reg has been way superior to flirt to register DWI to T1 and later use the inverse matrix to bring the T1 into DWI space (with transform convert). I’ve almost used it on elderly populations with variable degrees of atrophy and pathology.


1 Like

Hi Sisimo,

I use epi_reg for this purpose… I’ve found that using the white-matter-mask option helps :slight_smile: It also runs relatively quickly.

I don’t know how it compares to the ANTs method (@smeisler thanks for sharing your code - I haven’t tried this, yet, but it is always good to have options going forward :smiley: )

Here is some code that might be useful. Note that in this example, the MeanB0 is registered to the T1, and the inverse transformation is applied to transform the T1 to DWI space.

If you (or anyone else) does end up testing both epi_reg and the ANTs, I’d be interested in comparing the results of the two transformations (and maybe even adding in other registration techniques?)

# Register MeanB0 to T1 to obtain transformation matrix
epi_reg \
	--epi=${meanB0} \
	--t1=${T1_nifti} \
	--t1brain=${T1brain_nifti} \
	--wmseg=${WMseg_nifti} \

# convert transformation matrix to mrtrix
transformconvert ${reg_dir}MeanB0_warp.mat ${meanB0} ${T1brain_nifti} flirt_import ${reg_dir}transform_mrtrix.txt -force

# # Transform anatomical images #
mrtransform ${T1brain_nifti} ${RegisteredAnat} \
	-linear ${reg_dir}transform_mrtrix.txt \
	-inverse \
	-strides $meanB0 \

Hope this helps!


Hi Arkiev,

I’ve tried epi_reg and ANTs before on a few Parkinson’s disease samples maybe two years ago. Visually comparing the two results the final results were almost the same; however, in my opinion, ANTs can also be erroneous considering that distortion correction may not be optimal in brainstem regions. Thus, since the two brains belong to the same person and epi_reg is computationally much less intensive than ANTs, I always use epi_reg.


All previous answers are great. I’ll just add my $0.02.

You can use the new tools in freesurfer that provide ML segmentations from any contrast, applying them on both origin and target images. Next, use whatever registration tool you choose between the two, and apply the deformation to the original images.

An implementation of this using ANTs can be found here, inspired from micapipe.