FLIRT, FNIRT, and SyN (ANTs) Image Registration

Image registration when working with multimodal neuroimaging or even different sequences within subjects is one of the most important parts of image processing.

I’ve written about registering brain ROIs that were created in T1 space into diffusion space using FreeSurfer’s mri_convert (from version 4.5). FreeSurfer provides other tools for image registration but mri_convert is the simplest. However, I’ve had more accurate results using FSL’s FLIRT (FMRIB’s linear image registration tool) and FNIRT (FMRIB’s nonlinear image registration tool). For subcortical structures such as the caudate nucleus (and other periventricle structures), FNIRT usually is more accurate than FLIRT is.

To run FLIRT you type something like this into the command line (this is all one line/command):

flirt -in $DWIDIR/${blind}/${blind}_rawavg_brain.nii.gz -ref $DWIDIR/${blind}/${blind}_nodif_brain.nii.gz -out $DWIDIR/${blind}/${blind}_rawavg_brain_nodif_trilinear.nii.gz -omat $DWIDIR/${blind}/${blind}_rawavg_brain_nodif_trilinear.mat -bins 256 -cost corratio -dof 12

The flirt -help command provides more usage information (and there is good information online on the FSL course website) so I will not spend time on the command here other than that and to mention that you need skull stripped brains for FLIRT to work well (i.e., run BET first). In this example I used a 12 degree of freedom registration, the maximum that FLIRT provides. Check your data because a 6 degree or 9 degree registration might be more appropriate.

FLIRT works well for intra-individual images of the same or different modalities as well as many inter-individual images. FNIRT has the potential to work much better but also the potential for significant errors. Any nonlinear registration method (FNIRT included) can have problems caused by greater distortions on specific scans (e.g., diffusion sinus-caused warping). In general, for scans acquired at the same scan session of the same participant/patient, linear registration is sufficient. Many argue that rigid body (6 degree of freedom) is most appropriate for registering a T1 with a diffusion scan, for example; I generally follow that convention. However, I often try multiple registration techniques in order to find the ones that work best. FNIRT might be overkill or maybe even inappropriate for intra-individual registrations but it has the potential for higher quality registrations. Run FNIRT by typing in a command similar to this (again that is all one line and the dashes are all double dashes in case they are not displayed properly):

fnirt --ref=$DWIDIR/${blind}/${blind}_nodif --in=$DWIDIR/${blind}/${blind}_rawavg.nii.gz --refmask=$DWIDIR/${blind}/${blind}_nodif_brain_mask.nii.gz --iout=$DWIDIR/${blind}/${blind}_rawavg_brain_nodif_fnirt.nii.gz --aff=$DWIDIR/${blind}/${blind}_rawavg_brain_nodif_trilinear.mat --cout=$DWIDIR/${blind}/${blind}_rawavg_brain_nodif_fnirt_cout

This command inputs the .mat file created from a previous FLIRT run so you need to run FLIRT first. I’ll copy an example from the FSL website so you can see a couple different ways of running FNIRT.

fnirt --in=old_subj --aff=old_subj_to_MNI152.mat --config=T1_2_MNI152_2mm.cnf
--cout=coef_old_subj_to_MNI152 --iout=fnirted_old_subj --jout=jac_old_subj_to_MNI152 --jacrange=0.1,10

The FSL website has a lot more information about how to run FNIRT; it is a complicated (powerful) registration tool. I won’t get in to other things you can do with FNIRT in this post because I want to focus on another registration tool.

Because registration is so important, I did a literature search to try and find an article about the best registration tool. Arno Klein and colleagues compared 14 different non-linear image registration techniques (see Evaluation of 14 nonlinear deformation algorithms applied to human brain MRI registration; also check out the lead author’s website). One tool that provided great results was the SyN (Symmetric Normalization) method, which is part of the Advanced Normalization Tools (ANTs) package.

Installing this package is not straightforward for computer novices, although installation has improved recently. There are precompiled packages for Linux and OS X available here: but building/compiling from source is recommended.

UPDATE: Old instructions for installing ANTs are in the –Legacy instructions– section. In the years since I originally wrote them, methods have changed. Please refer to the following websites for updated installation methods.

This is where you can download ANTs files: There are compile (installation) instructions here: There is some ANTs documentation here:

On Linux/OS X if you are compiling the source code you need the source files, CMake, and a C++ compiler.

There is a helpful discussion site as well: that is a better place to go for specific instructions or questions than here.

————————-Legacy instructions——————————————–

First, I’d recommend downloading all the software you will need:

  1. ANTs binaries
  2. ITK
  3. CMake

You need to compile ANTs using CMake and ITK (someone with more programming experience might be able to use a different compiler but that is outside my realm of expertise). This means you need to install CMake then ITK then ANTs.

CMake is the easy part. You download the software and install it. When you open up the gui you see something like this:

Building ITK with CMake is not difficult but if you haven’t done it before, it can be tricky; however, there is a guide that walks you through it (in Windows and Linux but it’s not terribly different in OS X). Basically you select the source files you downloaded from online from this site and then select a target directory.

Then you configure then generate the build files. Sometimes you receive errors after configuring but most of them can be ignored, at least in OS X. You might need to configure a few times to clear the errors. After you generate the files you can exit CMake.

Alternatively, you can use the command line to call cmake. There are instructions on the ANTs website that you can apply to the ITK build.

After you generate the ITK files, jump into Terminal and cd into the ITK directory where you generated the files. Type “make” (without quotation marks) to build ITK. It might take a while to build but everything should go smoothly.

If you navigate to that directory you should see something like this:

Now you are ready to compile and build ANTs. Again, use CMake (either the GUI or the command line). Specify the source and destination directories then compile. It will mostly likely give you an error. You will need to specify where the ITK directory is as well as the CMAKE_BUILD_TYPE (set as RELEASE) and CMAKE_OSX_ARCHITCTURES (set as i386) if you are using OS X. Now configure again (1-3 times) and everything should be fine. After the files have been generated then navigate (using Terminal) to the ANTs directory and type “make” (without quotation marks). The software should build.

After this process finishes (it can take a number of minutes) type “make test” or just ctest to test the build. Now you are ready to use ANTs for registration!

In my .bash_profile (read this post for setting one up) I set ANTSPATH=/Applications/neuroimaging/ANTS

Also, you may want to alias ANTs in your bash profile (e.g., alias ANTS=/Applications/neuroimaging/ANTS/ANTS)

————————-End of legacy instructions——————————————–

Now you are ready to perform a mapping (registration). You should use skull-stripped brains, so make sure you have created those first.

Now we want to register a T1 into diffusion space. Run it using a form like this (this is certainly not the best way for this case but it will serve as an example):

ANTS 3 -m PR[fixedimage.nii,movingimage.nii,1,2] -i 50x20x10 -o OutputName -t SyN[0.3] -r Gauss[3,0]

So for example I could run this: ANTS 3 -m PR[/images/subject/b0diffusionimage.nii.gz,/images/subject/T1image.nii.gz,1,2] -i 50x20x10 -o T1_to_diffusion_synants.nii.gz -t SyN[0.3] -r Gauss[3,0]

The “3” following ANTs specifies that this is a 3D image. The first image is fixed (a template or an image you want to register another on to), the second image is the one you are manipulating, -o specifies your output and all the rest can be modified as needed. Refer to the ANTs website for more information (or type “ANTS -h”).

Running this command takes a while. Here is what your Terminal window might look like while ANTs runs:

Absolutely thrilling. Once it is done processing you will want to apply the warpings to your image.

Run something like this:

WarpImageMultiTransform 3 T1.nii.gz T1_to_diffusion_synants.nii.gz -R b0_diffusion.nii.gz T1_to_diffusion_synantsWarp.nii.gz T1_to_diffusion_synantsAffine.txt

See screenshot below for more options (those are not all the options, some are cut off in the screenshot):

One option you will use if you are applying the warp to a mask file (such as FreeSurfer’s aseg) is the –use-NN option (note: that’s a double dash before use but not after use).

From the help file: Important Notes:  Prefixname “abcd” without any extension will use “.nii.gz” by default The abcdWarp and abcdInverseWarp do not exist. They are formed on the basis of abcd(Inverse)Warpxvec/yvec/zvec.nii.gz when calling ./WarpImageMultiTransform, yet you have to use them as if they exist.
Here is the final product:
You can see the frontal lobe distortion of the diffusion scan. Also, the masks are not identical in size. I skull stripped the brains quickly without making sure I had great results. However, the two scans are registered very well. I’ll update more about SyN (ANTs) as I learn the tool.
Disclaimer: The settings I gave as examples are almost certainly not optimal. I just started using this tool so I am not aware of the different options and settings. I wrote this guide simply to introduce the tool and serve as a reference for myself.

About Jared Tanner

I have a PhD in Clinical and Health Psychology with an emphasis in neuropsychology at the University of Florida. I previously studied at Brigham Young University. I am currently a Research Assistant Professor at the University of Florida. I spend the bulk of my research time dealing with structural magnetic resonance images of the brain. My specialty is with traditional structural MR images, such as T1-weighted and T2-weighted images, as well as diffusion weighted images. I also look at the cognitive and behavioral functioning of individuals with PD and older adults undergoing orthopedic surgery. Funding for the images came from NINDS K23NS060660 (awarded to Catherine Price, University of Florida). Brain images may not be used without my written permission (grant and software requirements).