You are on page 1of 8

Registration and Unwarping Practical

Practical Overview
In this practical you will learn to use the tool FEAT for registration (in the context of a fMRI data analysis) as well
as calling the specific tools FLIRT and FNIRT directly. The optional parts explore more ways of using the tools
FLIRT and FNIRT and are strongly recommended for anyone working on structural MRI image analysis or
working with pathological, elderly or very young populations, since these often need a little extra intervention to
get registrations to work well.
You will learn in this practical how to register (align) images that may be from different modalities, come from
different subjects, and/or contain different distortions. Being able to get good registrations is crucial for all
analyses: structural, functional and diffusion.

Two-stage Registration and Unwarping with FEAT
Register functional EPI images to the individual structural image and to standard space using the FEAT
GUI. This includes fieldmap-based unwarping and the preparation of these fieldmap scans.
Inter-subject Registration
Perform registration of a subject to standard space with affine and non-linear registration.
Applying and Inverting Transformations
Calculate and apply transforms and inverses for both FLIRT and FNIRT. This provides experience in
transforming ordinary images and masks between the different "image spaces" (functional, structural,

Optional extensions:
Viewing Deformation Fields
Gain a better understanding of the deformation fields and Jacobians resulting from the non-linear
registration in the Inter-subject Registration example.
Partial Field of View (FOV) Registration
Use the FLIRT schedule files to achieve limited DOF registrations for partial FOV images (limited number
of slices).
Getting to Know the Transformation Matrix
Decomposing the tranformation matrix using avscale.

Two-stage Registration and Unwarping with FEAT

cd ~/fsl_course_data/reg/feat_example

The objective of this practical is to become familiar with how to perform and evaluate the results of registration in
the FEAT GUI (the main fMRI analysis GUI). This involves two-stage registration and fieldmap-based unwarping,
and requires the images to be suitably prepared. Registration in other FSL GUIs works very similarly.
Preparing the structural image
We need to perform brain extraction on the structural image prior to using it for registration (as explained in the
lecture). To do this just run BET on the image struct.nii.gz, and save the result as struct_brain.nii.gz. Check
the results with fslview.
Fieldmap images
There are three images that the Siemens scanner saves from a fieldmap sequence. These are (in this case):


where the first two represent the standard magnitude parts of the images with the different echo times (that the
scanner acquired to generate the fieldmap), and the last one represents the difference of the phase parts of the
two images. Have a quick look at these with fslview. We will need to use both magnitude and phase images.
Processing the fieldmap magnitude image
We will start by brain extracting the first magnitude image (note that we will not use the second magnitude image
as it normally has less signal but contains the same information and so we do not need it). Run brain extraction
on the first magnitude image (images_006_grefieldmap521Hzpx3mm1001.nii.gz) and save the result as
Check what the results look like in fslview and also view the phase image
Note that it is important that the brain extracted image does not include non-brain tissue, especially in areas
where the phase difference image is noisy (as it is just outside the brain). If necessary, alter the brain extraction and in general in this case it is better to remove a little bit of brain tissue in order to avoid these noisy voxels, as
any missing brain voxels will get filled in by the process at a later stage with good, low-noise neighbouring values.
Processing the fieldmap phase difference image
Here we need to take this raw scanner output, which is scaled in a strange way (0 to 360 degrees are mapped to
0 to 4096), and convert it into a radians per second image (this is equivalent to an image in Hz multiplied by 2*pi).
For this we need the phase difference image, the brain extracted magnitude image and the difference in the echo
times of the fieldmap acquisitions. This latter value is 2.46ms and can be found in the text file README.txt, which
is conveniently given here but will not exist normally. Therefore it is important to record this echo time difference
when you scan (your scanner operator will be able to give you the value).
Armed with this information, all we need to do is run the script called fsl_prepare_fieldmap. Note that these
images come from a Siemens scanner, which is, at present, all this script can process. You can use the
command line call:
fsl_prepare_fieldmap SIEMENS images_007_grefieldmap521Hzpx3mm2001 \
fmap_mag_brain fmap_rads 2.46

View the output with fslview and check that most of the brain has small values (less than 100) while in the
inferior frontal and temporal areas the values are larger (either large and positive or large and negative).
Using the FEAT GUI
With the fieldmap processed and the structural image brain extracted we are now ready to use the FEAT GUI for
registration. Note that we will actually run a whole FMRI analysis but the statistical parts have already been set
up for you.
Start the FEAT GUI by typing Feat in the terminal. Select Load from the buttons at the bottom of the GUI, then
select the file premade_design.fsf and click OK. This will load up some settings for the statistical part of the fMRI
analysis, which you can ignore today. However, none of the B0 unwarping (fieldmap-based correction) or
registration has been set up so we will do this now.
On the main Data tab, we need to make sure the input data and output directory are set correctly. Click on the
Select 4D data button and select the input file: func_data.nii.gz from the fsl_course_data/reg/feat_example
directory. Then click on the file browser for the Output directory and make sure it is in the appropriate directory.
Once you are in the right directory, enter a new name for the output, e.g. func_data_flirt.feat, in the Selection
box at the bottom of the file browser and press OK.
Now go to the Pre-stats tab and click the B0 unwarping button. Note that all other parts of this tab (e.g. motion
correction) are set to their default values and we will leave them with these settings. Now go through and
populate the relevant parts: using fmap_rads for the "Fieldmap" (but select this using the file browser that the

folder icon opens); fmap_mag_brain for the "Fieldmap mag"; a value of 0.49 ms for "Effective EPI echo spacing"
(taken from the README.txt file here, but in general you need to take note of this value when scanning - ask your
operator - and if you are using parallel acceleration then it needs to be divided by the acceleration factor); 28ms
for "EPI TE"; "-y" for "Unwarp direction" (again your operator can tell you whether the unwarp direction, or phase
encode direction, is x, y or z, but whether it is + or - needs to be determined, currently, by trial and error, which
has already been done here); and leave the "% Signal loss threshold" at the default value of 10%.
Go to the Registration tab and click on the "Main structural image" button. Open the file browser and select
struct_brain. We will not select the "Nonlinear" button in the Standard space section for this part of the practical
in order to save time, but you normally would use this setting as it gives the best results.
That's everything we need to set (given that we loaded in a pre-made version to start with). So double-check that
the "Pre-stats" and "Registration" tabs look correct and when you are happy press the Go button at the bottom.
This should start up a web browser showing the progress of FEAT - although it may take a minute for this to
While you wait
The FEAT job will take about 10-15 minutes to finish. In the meantime there are two previously run jobs that you
can look at. They are very similar to the job you set up above, but in the first one the non-linear registration
(FNIRT) to standard space is turned on (the "non-linear" button), while in the other neither non-linear registration
nor fieldmap-based correction (B0 unwarping) were done. You can look at the outputs from these FEAT runs by
opening new tabs in the browser that FEAT started and opening the files:
firefox ~/fsl_course_data/reg/feat_example/func_data_fnirt.feat/report.html
firefox ~/fsl_course_data/reg/feat_example/func_data_flirt_nofmap.feat/report.html

(or use open instead of firefox on a Mac)

Look carefully at the results of the registrations in the web page report and in the "Unwarping" page for the former
case (where fieldmap-based correction was run). Which results seem better and which seem the same? You can
also use fslview to look in more detail, for example:
cd ~/fsl_course_data/reg/feat_example/func_data_fnirt.feat/reg/
fslview example_func2highres.nii.gz highres.nii.gz highres_wmedge.nii.gz -l Red

and turn the "highres" (structural) off to see the "example_func" (EPI/functional) image transformed into the
structural space (with the white matter borders still showing). Feel free to look at other images in this "reg"
subdirectory or in the "unwarp" subdirectory inside this.
When you are finished you can do:
cd -

to get back to the directory you were in before.

Once the FEAT job is finished
Look carefully at the results of the registrations created by the run you setup before (with fieldmap-based
correction but without non-linear registration). Compare the results to the other two above, either using the
webpage images or using fslview.

Inter-subject Registration
cd ~/fsl_course_data/reg

We will now look at registering an image of one subject to standard space with both affine (FLIRT) and non-linear
(FNIRT) methods. We will work with an older subject that has slightly enlarged ventricles compared to the

standard brain, and hence we expect it to be advantageous to use non-linear registration.

Apply BET before FLIRT
Apply BET to the image old_subj (saving the result as old_brain) with a fractional intensity threshold of 0.35,
using either the GUI or the command line version. Check the output in FSLView (run this by typing ' fslview
old_subj old_brain -l Red ' to directly load both images). Note that the brain extracted image does not need to
be "perfect" but should have removed the majority of the non-brain structures without removing much of the brain
material. Check the result with FSLView.
Registration using the FLIRT GUI
Start the FLIRT GUI (using "Flirt &" or "Flirt_gui &") and register old_brain (input image) to
MNI152_T1_2mm_brain (the default reference image which is already brain extracted i.e. we register our skullstripped brain to a skull-stripped template).
Choose 12 DOF (which should be the default) as we are doing a subject-template registration and select the
output name to be flirted_old_brain. Use the browse button to make sure the output is saved to a place where
you can find it later.
Visual Check - FSLView
When Flirt is finished (a couple of minutes) load the output and reference volumes into FSLView (the reference
can be loaded most easily using the "Add Standard ..." option from the "File" menu). Once these are loaded, try
to see the difference by flicking between them: that is, by turning the "top" one off and on (i.e. making them visible
or not in the image list - by changing the "eye" symbol).
Flicking between images is a very good way of detecting registration differences and changes. Normally this can
only be done for the reference and output images from FLIRT, as it is unusual that pre- and post- registered
images share a common resolution and FOV. That is why we cannot load the input image, only the output and
the reference together.
Note that affine registration to standard space is never "perfect", but should get major features such as the
brainstem and brain outline quite close. Variation in the population is expected and is reflected in the blurring of
structures within the standard (average) brain - although this standard brain was produced with non-linear
registration and is less blurred than the affine version MNI152lin_T1_2mm. However, in this case the image is of an
older subject and the enlarged ventricles will not fit perfectly with the standard brain.
Transformation Matrix
When running FLIRT, the GUI also saved a transformation matrix which was called flirted_old_brain.mat. We
can put this transformation matrix to use straight away by transforming the original (non-skull-stripped) image
with the same transform. Run the command-line version of flirt with the following (single) line:
flirt -in old_subj -ref $FSLDIR/data/standard/MNI152_T1_2mm_brain \
-out flirted_old_subj -applyxfm -init flirted_old_brain.mat

This means we will now have two images (flirted_old_brain and flirted_old_subj), both transformed into the
same space. Look at this in FSLView.
Registration command (FNIRT)
Now we will look at non-linear registration of old_subj to MNI152_T1_2mm. Note that FNIRT is more sensitive to
errors in brain extraction so we do not use the BET output nor do we register it to the skull stripped template, as
it deals with non-brain tissue via a cost-function weighting mask in the standard space.
Running FNIRT takes around 20 minutes, so this has already been done using the following command (do not
run this now):
fnirt --in=old_subj --aff=flirted_old_brain.mat \
--config=T1_2_MNI152_2mm.cnf --cout=coef_old_subj_to_MNI152 \

--iout=fnirted_old_subj --jout=jac_old_subj_to_MNI152 \

FNIRT actually takes a large number of parameters but here we have used the configuration file
T1_2_MNI152_2mm.cnf to specify most of these for us. The configuration file is simply a text file that contains many
of the parameters that FNIRT takes (and values for them). This enables us to come up with a set of parameters
that works well for matching a T1-weighted structural scan to the MNI152 T1-weighted standard brain and other
common uses. You may see the parameters that were used by typing
cat $FSLDIR/etc/flirtsch/T1_2_MNI152_2mm.cnf

The sight of that may convince you that configuration files are quite useful, and that unless you know exactly
what you are doing you should use existing configurations. FSL comes with configuration files for doing things
like matching T1 structural images to the MNI152 T1 standard brain (T1_2_MNI152_2mm.cnf) and for matching FAimages to the FMRIB58 FA standard brain (FA_2_FMRIB58_1mm.cnf). In the example above we have restricted the
Jacobian-determinant (see below) to be in a more narrow range than is normally the case (default 0.01,100). We
do this to facilitate calculating the inverse of the field (covered in the full FSL course practical).
that the FNIRT command requires that you specify the FLIRT matrix (flirted_old_brain.mat) that we
generated previously. If you forget that you will most likely get poor results and may spend a long time trying
to figure out why.
Also note
that a registration always implies registering one image (typically -i or --in in FSL applications) to another.
In the fnirt command above we can see that we want to register the image old_subj, but to what? The -ref image is in this case specified in the configuration file and if you performed the cat command above you
will have seen on the first row --ref=/usr/local/fsl/data/standard/MNI152_T1_2mm.
Visual Check (images)
Load flirted_old_subj, fnirted_old_subj and MNI152_T1_2mm into fslview (Note that the last image can be added
with "Add standard..."). Flip between the registered images and the template to see how well they match. It can
also be interesting to flip back and forth between the linearly (flirted_old_subj) and the non-linearly
(fnirted_old_subj) registered images. That may convince you that FNIRT has actually been quite gentle with
your images, and on the whole the warps are relatively small and well behaved. It will appear as if the 2 images
have a different smoothness, but ignore that for the now.

Applying and Inverting Transformations

cd ~/fsl_course_data/reg

The objective of this practical is to become familiar with applying transformations in FLIRT and FNIRT, as well as
using their inverses.
Applying a transform (FLIRT)
We are going to apply the same linear transform that we calculated previously, but use it to resample our data
(image) to a higher resolution. We can do that with the applywarp command:
applywarp -i old_subj -r $FSLDIR/data/standard/MNI152_T1_1mm -o highres_flirted_old_subj \

Note that the important thing here is that we have specified a different reference scan (with -r), one with a
different resolution and matrix size, as it is the reference volume that controls the output resolution and size.
Applying a transform (FNIRT)
Now we are going to do the same thing with a non-linear transform. Again, we will use the applywarp command

applywarp -i old_subj -r $FSLDIR/data/standard/MNI152_T1_1mm -o highres_fnirted_old_subj \

-w coef_old_subj_to_MNI152

Again we are using the same transform, but resampling onto a -ref scan with higher resolution and different
matrix size.
Visual Check
Use the command

fslview $FSLDIR/data/standard/MNI152_T1_1mm highres_flirted_old_subj highres_fnirted_old_subj

to compare the linear and non-linear registration, this time with final images of higher resolution.
Standard-space masking - a useful application
Now let's use a saved FLIRT transform (12 DOF) to take a standard-space mask and use it to define regions in
an individual functional image. The mask we will use excludes a rough estimation of the ventricles, cerebellum
and brain stem. The registration from the functional image, example_func, to the standard-space template has
already been done and is saved as exfunc2std.mat. We will use this matrix to transform the standard-space mask
to the functional space.
First invert the transform exfunc2std.mat using the InvertXFM GUI, calling the inverse transform std2exfunc.mat.
Then transform the standard-space mask $FSLDIR/data/standard/MNI152_T1_2mm_strucseg_periph into the space
of example_func using applywarp:
applywarp -i $FSLDIR/data/standard/MNI152_T1_2mm_strucseg_periph -r example_func \
--premat=std2exfunc.mat -o lowresmask

We now need to "binarise" the mask, i.e. make sure that it consists only of ones and zeros (because the
transformation above will have introduced values between zero and one representing the partial volume
overlaps). The command
fslmaths lowresmask -thr 0.5 -bin lowresmask

will first set all values below 0.5 to 0 and then set all values above 0.5 to 1.
Now apply this mask using:
fslmaths example_func -mas lowresmask masked_example_func

and view the result.

Aside: If we had wanted to do the same using a non-linear registration (via FNIRT,
as we most often would) then we'd call invwarp instead of InvertXFM and substitute
the resulting inverse warpfield for the inverse matrix (but using the -w option instead
of --premat). However, it all takes a little longer so we opted for the quicker version
above (and for intra-subject registration you would deal with linear registrations
anyway, so it is still useful).

Viewing Deformation Fields (Optional)

cd ~/fsl_course_data/reg

This section leads on from the section on Inter-subject Registration, so you must have completed that before
starting this part.
Deformation Field
The output file coef_old_subj_to_MNI152 is not a normal image but is a special coefficient file format used by
FNIRT, which is used to store the deformation field (also called a warpfield). It also contains the information that
we passed from FLIRT to FNIRT (i.e. the information in flirted_old_brain.mat). However, it is not really useful to
view it (the coefficients) using fslview.
To view the deformation field itself it is necessary to convert the coefficients stored in coef_old_subj_to_MNI152
into fields. This is done using the command fnirtfileutils to change it into a vector image. If you run the
fnirtfileutils --in=coef_old_subj_to_MNI152 \
--ref=$FSLDIR/data/standard/MNI152_T1_2mm \

you will get a field (field_old_subj_to_MNI152_nonlinear_only) containing only the non-linear part of the warp,
i.e. not including the affine part of the transform. This is useful for visual inspection since the non-linear part tends
to be overshadowed by the affine component, making it difficult to interpret what one sees.
Alternatively, to include the affine and non-linear components (necessary to run other commands like convertwarp
or invwarp) then the option --withaff can be added.
Visual Check (deformation field)
Now view the deformation field (field_old_subj_to_MNI152_nonlinear_only) in fslview. Note that it is a 4D image
- three images combined together in the "fourth dimension". These correspond to x-displacements (volume 0), ydisplacements (volume 1) and z-displacements (volume 2). You will often need to adjust the display range for
these images since fslview determines it from the first volume only. The "intensity" values you observe when you
click in the images are displacements (c.f. the little arrows in the lecture) in units of mm.
Jacobian of deformation field (degree of expansion/compression)
Often more useful for viewing than the deformation fields themselves are the Jacobian determinants of the
deformation fields. When we ran FNIRT we used --jout=jac_old_subj_to_MNI152 (see above if you have
forgotten) to specify that we wanted the Jacobian determinants (one per voxel) written to the file
Had we forgotten that we could still have generated the Jacobians at a later stage with the command
fnirtfileutils --in=coef_old_subj_to_MNI152 --ref=$FSLDIR/data/standard/MNI152_T1_2mm \

The Jacobian field is useful for checking what liberties FNIRT has been taking with your data. The interpretation
of the Jacobian is "voxelwise relative volume change" between the original (in our case old_subj) and the warped
(in our case fnirted_old_subj) image. A jacobian of 5 indicates that a volume in the original image has been
shrunk by a factor of 5 (e.g. 1mm3 -> 0.2mm3). In general we don't want the Jacobian to be either too small or too
large since it seems reasonable that a "function" that exists in both brains should be "implemented" in
approximately equal amounts of brain tissue. To check this load jac_old_subj_to_MNI152 into fslview (you might
need to change the display range to be something like 0 to 3) and then click "Tools->Image Histogram."
It (the Jacobian) is also useful in its own right, and is an important carrier of information e.g. in VBM.

Partial FOV Registration (Optional)

cd ~/fsl_course_data/reg

The objective of this practical exercise is to use FLIRT for registering small FOV images. This will involve the use
of schedule files and initialised transforms without search which is also very useful for cases of difficult image
Small FOV Registration
For partial FOV images an initial translation alignment can be found using the schedule xyztrans.sch (found in
$FSLDIR/etc/flirtsch). Apply this schedule to register partial_brain to whole_brain and save the transformation
matrix to partial2whole.mat using:
flirt -in partial_brain -ref whole_brain -schedule $FSLDIR/etc/flirtsch/xyztrans.sch \
-omat partial2whole.mat -out partial2whole

Visual Check
Inspect the result - note that the transformation here is 3 DOF (translation only). This is necessary to limit the
flexibility, since there is insufficient data to easily obtain a good fit with more DOF.
Fine Adjustments using Initialisation
By using the previous translation as a starting point, a fine adjustment in 3D can be made to the transformation
above using:

flirt -in partial_brain -ref whole_brain -dof 6 -out partial2wholeB -omat partial2wholeB.mat \
-init partial2whole.mat -nosearch

Note the use of the two options: -init and -nosearch.

The -init option (when separate from -applyxfm) uses the specified transformation as a starting point and nosearch turns off the large-scale search for the global minimum which is necessary when the registration is illconstrained as it is for small FOV.
Visual Check
Inspect the result of the fine adjustment and compare it to the previous translation-only result. Is this an
Note that a small FOV image that is "tilted" will have an arbitrary cutoff line running across its top and bottom
slices. This is normal and a natural consequence of trying to show a tilted, small FOV on top of a larger image.
Therefore do not judge the performance based on this, but instead look at the anatomical features in the images
and how well they match.

Getting to Know the Transformation Matrix (Optional)

Decomposing the Transformation Matrix
Run avscale exfunc2std.mat and find the scaling values. This information is extracted from the transformation
matrix and shows that some scaling (value different from 1.0) was necessary to align the images well.
Note that this command is also useful for finding other information about the transformation matrix, such as
rotation angles, translations and skews as well as the forward and backward half transformations (which are
useful to take images into a "halfway" space which equalises interpolation artefacts).
The End.