Preprocessing lab
From NITP Summer Course Wiki
Contents |
Preprocessing with FSL tools
Adapted from the FLIRT and FUGUE practical from FSL
FLIRT and FUGUE - Practical
Head motion
In this exercise you will see the effect of motion correction using MCFLIRT. Subjects tend to move their heads slightly during the course of an experiment. If we do not do motion correction on our images, the position of an area in the brain at the beginning of the experiment does not correspond to the position of that area at the end of the experiment.
The data for this exercise are also in the additional_data directory. The data file is called “motion.nii”. First, view the raw data to see if you can detect motion in the timeseries.
In FSLView, first close any other images you may have open, and then select the motion.nii file from the File->Open menu.
Next, click the Movie mode button, which looks like a section of movie film in the middle of the button bar.
This will display each of the timepoints in the scan consecutively, generating a ‘movie’ of your brain over time.
Based on visual inspection alone, can you see any motion that might introduce artifacts in later analysis if not corrected? If so, in what direction(s)?
Now we will actually run the correction, and see what effect motion correction has on these data.
To run MCFLIRT, FSL’s Motion Correction LInear Registration Tool, we will use the command line. Open a new terminal window by clicking on the X11 icon in the dock, and then selecting “new terminal” from the file menu.
In that terminal, change to the directory where the motion file is located by typing:
cd ~/FSL/additional_data/
Be sure to include the space between the command ‘cd’ and the path. From there, we can run MCFLIRT by typing:
mcflirt –in motion.nii
This tells mcflirt to use the motion file as input. By default, it will add a “_mcf” to the input filename when generating the output. It will also compress the file, so there will be a “.gz” added as well. Load this new file into FSLView like we did with the non-corrected file. What has changed between the two? If you are curious, feel free to explore the various parameters available for motion correction by typing mcflirt -help, which will list all of the options that are user configurable.
Intra-subject Registration
Begin by moving to the data we will be manipulating in the Terminal. Do this by typing the following command in the prompt of the X11 terminal window:
cd ~/FSL/fsl_course_data/flirt
We start by using FLIRT for image-to-image registration within a single-subject. The images we start with are of a different modality (T1-weighted and T2-weighted), and are called sub2_t1 and sub2_t2.
- Apply BET before FLIRT
Apply BET to the images sub2_t1 and sub2_t2 (saving the results as sub2_t1_brain and sub2_t2_brain). To do this type the following commands in the terminal:
bet sub2_t1 sub2_t1_brain
and
bet sub2_t2 sub2_t2_brain
Check the outputs in FSLView - note that these do not need to be "perfect" but should have removed the majority of the non-brain structures without removing any of the brain material. It doesn't matter if there are some differences between them, especially around the eyeballs.
To load the files in FSLView, type:
fslview sub2_t1_brain
where sub2_t1_brain could be the filename of any of the volumes. You can also view both the original and extracted images in the same view by loading both files simultaneously:
fslview sub2_t2 sub2_t2_brain
- Registration using the FLIRT GUI
Start the FLIRT GUI (by typing "Flirt_gui &" in the command window) and register sub2_t2_brain (input image) to sub2_t1_brain (reference image). Choose 6 DOF (which is not the default) since these images are from the same subject, and choose Correlation Ratio as the cost function (which is the default) as the images are different modalities.
- Save the output image as a name of your choice. This will take a few minutes to run...
While you wait...
Load the input and reference volumes into FSLView (note that this is normally not possible as FSLView requires the same resolution and FOV, which this pair just happens to have but is not the norm). 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).
- Visual Check - FSLView
Once FLIRT has finished, add the resulting transformed image (output) to FSLView. Compare it to the reference image and the input image. Try to see the differences by flicking between pairs of images. Make sure that you only have two of them visible at any one time.
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 like these do.
- Visual Check - slices
Now use slices to look at the result by: slices output_image sub2_t1_brain The red lines are edges generated from the second image (sub2_t2_brain) and should be aligned with edges in the output image, although sometimes they will show small differences in the BET masking around the outer brain surface.
Inter-subject Registration
The objective of this practical is to try to register images of one subject to standard space.
- Registration command
Use FLIRT to register sub2_t1_brain to avg152T1_brain (default reference image) using 12 DOF and the Correlation Ratio cost function with the command-line call:
flirt -in sub2_t1_brain -ref $FSLDIR/etc/standard/avg152T1_brain -dof 12 -cost corratio -out sub2_t1_to_avg -omat sub2_t1_to_avg.mat
Note that this call must be typed as a single line, and that the standard image is not in the course data directory but can be found in $FSLDIR/etc/standard (type ls $FSLDIR/etc/standard to see the contents of this directory). To see the full range of command-line options just type flirt. If this looks hard, you can also look at the command-line calls that the GUIs made in the previous examples.
Inspect the output image of your (correlation ratio) registration to see if it registered well to the reference image avg152T1_brain. 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. However, in this case the enlarged ventricles do not fit well with the standard brain (which is derived from a group without pathology).
- Transformation Matrix
A transformation matrix is also saved with the same name as the output image but ending in .mat Look at this transformation matrix (it is a text file) using cat sub2_t1_to_avg.mat from the shell window. It should be a 4 by 4 array of numbers.
- Decomposing the Transformation Matrix
Run avscale sub2_t1_to_avg.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.
- Changing DOF
The objective of this practical is to explore the effect that the Degrees of Freedom (DOF) have on registration results.
- Restricting the DOF
Perform another registration of sub2_t1_brain to avg152T1_brain but now use 6 DOF, instead of the default 12. Make sure you save the transformation matrix (if using the command line) and give the new output a different name!
- Visual Check
Look at the results of the 6 DOF registration (using either FSLView or slices). How do they differ from the 12 DOF results?
- Decomposing the Transformation Matrix
Run avscale on the new transformation matrix and check that the scaling values are exactly equal to 1.0 in this case.
Applying and Inverting Transformations
The objective of this practical is to become familiar with applying transformations and matrices in FLIRT as well as their inverses.
- Creating an inverse transformation
Find the inverse of the transformation matrix found previously that registers sub2_t1_brain to sub2_t2_brain The inverse can be found using the InvertXFM_gui.
- Applying a transformation
Generate the inverse registration result: an image of sub2_t2_brain transformed into the space of sub2_t1_brain This can be done by: flirt -applyxfm -init inv_matrix.mat -in sub2_t2_brain -ref sub2_t1_brain -out sub2_t2_brain_transformed Note that sub2_t1_brain is treated as the reference (determining output size) in this case.
- Command line version - a useful application
Now let's use a saved FLIRT transform to mask out the ventricles, cerebellum and brain stem in example_func, using a crude standard-space mask which excludes those regions.
First invert the transform exfunc2std.mat (which takes example_func to standard space) with: convert_xfm -omat std2exfunc.mat -inverse exfunc2std.mat
Now transform the standard-space mask $FSLDIR/etc/standard/MNI152T1_2mm_strucseg_periph into the space of example_func using flirt with -applyxfm as above, calling the output lowresmask
Now apply this mask using: fslmaths example_func -mas lowresmask masked_example_func and view the result
Can you think of an easy way of masking out the left side of the brain, even if the head appears "skewed" in the original image?
