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).

FLIRT works well for brains that are similar (e.g., a T1 to a T1) but registering a T1 to a diffusion image is trickier. FNIRT works much better in that instance, at least it might work better. However, there can be problems that arise due to greater distortions on the diffusion scan. I usually try multiple registration techniques in order to find the ones that work best. FNIRT might be overkill for intraindividual registrations but visually, it usually looks better than FLIRT. 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 FLIRTing 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 am at most only a novice user of it but I’ve had success with the tool. I won’t get in to other things you can do with FNIRT in this post because I want to focus on a registration tool I just started using – just yesterday, in fact.

Because registration is so important (and will be key for much of my research, including my dissertation), I did a literature search to try and find an article about the best registration tool. Arno Klein and colleagues in a recent article 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.

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)

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 hopefully whet your appetite for it.

Related posts:

  1. Converting FreeSufer aseg Labels to Individual Volume Masks
  2. Bash Scripting for Neuroimaging
  3. Converting DICOM Images to NIFTI Images

About Jared Tanner

I am a PhD candidate in neuropsychology at the University of Florida. I previously studied at Brigham Young University. 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. Funding for the images and much of my research comes from NINDS K23NS060660 (awarded to Catherine Price, University of Florida). Brain images may not be used without my written permission (grant and software requirements).

3 comments on “FLIRT, FNIRT, and SyN (ANTs) Image Registration

  1. Pingback: Using FreeSurfer’s bbregister for Image Registration

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CommentLuv badge