registration practical

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. Y ou will learn in this practical how to register (align) images that may be from different modalities, come fr om different subjects, and/or contain different distortions. Being able to get good registrations is crucial for all analyses: structural, functional and diffusion. Contents: 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 bot h FLIRT and FNIRT. This provides experience in transforming ordinary images and masks between the different "image spaces" (functional, structural, standard) 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 part ial 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 FEA T GUI (the main fMRI analysis GUI). This involves two-stage registration and fieldmap-based unwarping, and requires the images t o be suitably prepared. Registration in other FSL GUIs works ver y 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). T o do t his 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):

Upload: jawadmelhem

Post on 04-Nov-2015

5 views

Category:

Documents


1 download

DESCRIPTION

FSL brain normalization

TRANSCRIPT

  • Registration and Unwarping Practical

    Practical OverviewIn this practical you will learn to use the tool FEAT for registration (in the context of a fMRI data analysis) as wellas calling the specific tools FLIRT and FNIRT directly. The optional parts explore more ways of using the toolsFLIRT and FNIRT and are strongly recommended for anyone working on structural MRI image analysis orworking with pathological, elderly or very young populations, since these often need a little extra intervention toget registrations to work well.

    You will learn in this practical how to register (align) images that may be from different modalities, come fromdifferent subjects, and/or contain different distortions. Being able to get good registrations is crucial for allanalyses: structural, functional and diffusion.

    Contents:

    Two-stage Registration and Unwarping with FEATRegister functional EPI images to the individual structural image and to standard space using the FEATGUI. This includes fieldmap-based unwarping and the preparation of these fieldmap scans.Inter-subject RegistrationPerform registration of a subject to standard space with affine and non-linear registration.Applying and Inverting TransformationsCalculate and apply transforms and inverses for both FLIRT and FNIRT. This provides experience intransforming ordinary images and masks between the different "image spaces" (functional, structural,standard)

    Optional extensions:

    Viewing Deformation FieldsGain a better understanding of the deformation fields and Jacobians resulting from the non-linearregistration in the Inter-subject Registration example.Partial Field of View (FOV) RegistrationUse the FLIRT schedule files to achieve limited DOF registrations for partial FOV images (limited numberof slices).Getting to Know the Transformation MatrixDecomposing 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 inthe 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 thelecture). To do this just run BET on the image struct.nii.gz, and save the result as struct_brain.nii.gz. Checkthe results with fslview.

    Fieldmap images

    There are three images that the Siemens scanner saves from a fieldmap sequence. These are (in this case):

  • images_006_grefieldmap521Hzpx3mm1001.nii.gzimages_006_grefieldmap521Hzpx3mm2001.nii.gzimages_007_grefieldmap521Hzpx3mm2001.nii.gz

    where the first two represent the standard magnitude parts of the images with the different echo times (that thescanner acquired to generate the fieldmap), and the last one represents the difference of the phase parts of thetwo 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 imageas it normally has less signal but contains the same information and so we do not need it). Run brain extractionon the first magnitude image (images_006_grefieldmap521Hzpx3mm1001.nii.gz) and save the result asfmap_mag_brain.

    Check what the results look like in fslview and also view the phase imageimages_007_grefieldmap521Hzpx3mm2001.nii.gz.

    Note that it is important that the brain extracted image does not include non-brain tissue, especially in areaswhere 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, asany 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 to0 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 echotimes of the fieldmap acquisitions. This latter value is 2.46ms and can be found in the text file README.txt, whichis conveniently given here but will not exist normally. Therefore it is important to record this echo time differencewhen 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 theseimages come from a Siemens scanner, which is, at present, all this script can process. You can use thecommand 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 theinferior 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 forregistration. Note that we will actually run a whole FMRI analysis but the statistical parts have already been setup for you.

    Start the FEAT GUI by typing Feat in the terminal. Select Load from the buttons at the bottom of the GUI, thenselect the file premade_design.fsf and click OK. This will load up some settings for the statistical part of the fMRIanalysis, which you can ignore today. However, none of the B0 unwarping (fieldmap-based correction) orregistration 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 theSelect 4D data button and select the input file: func_data.nii.gz from the fsl_course_data/reg/feat_exampledirectory. 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 Selectionbox 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. motioncorrection) are set to their default values and we will leave them with these settings. Now go through andpopulate 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 youroperator - and if you are using parallel acceleration then it needs to be divided by the acceleration factor); 28msfor "EPI TE"; "-y" for "Unwarp direction" (again your operator can tell you whether the unwarp direction, or phaseencode direction, is x, y or z, but whether it is + or - needs to be determined, currently, by trial and error, whichhas 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 selectstruct_brain. We will not select the "Nonlinear" button in the Standard space section for this part of the practicalin 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 thatthe "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 toappear.

    While you wait

    The FEAT job will take about 10-15 minutes to finish. In the meantime there are two previously run jobs that youcan 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 registrationnor fieldmap-based correction (B0 unwarping) were done. You can look at the outputs from these FEAT runs byopening new tabs in the browser that FEAT started and opening the files:

    firefox ~/fsl_course_data/reg/feat_example/func_data_fnirt.feat/report.htmlfirefox ~/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 formercase (where fieldmap-based correction was run). Which results seem better and which seem the same? You canalso 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 thestructural 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-basedcorrection but without non-linear registration). Compare the results to the other two above, either using thewebpage 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 ' fslviewold_subj old_brain -l Red ' to directly load both images). Note that the brain extracted image does not need tobe "perfect" but should have removed the majority of the non-brain structures without removing much of the brainmaterial. 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) toMNI152_T1_2mm_brain (the default reference image which is already brain extracted i.e. we register our skull-stripped brain to a skull-stripped template).

    Choose 12 DOF (which should be the default) as we are doing a subject-template registration and select theoutput name to be flirted_old_brain. Use the browse button to make sure the output is saved to a place whereyou can find it later.

    Visual Check - FSLView

    When Flirt is finished (a couple of minutes) load the output and reference volumes into FSLView (the referencecan be loaded most easily using the "Add Standard ..." option from the "File" menu). Once these are loaded, tryto see the difference by flicking between them: that is, by turning the "top" one off and on (i.e. making them visibleor 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 canonly be done for the reference and output images from FLIRT, as it is unusual that pre- and post- registeredimages share a common resolution and FOV. That is why we cannot load the input image, only the output andthe reference together.

    Note that affine registration to standard space is never "perfect", but should get major features such as thebrainstem and brain outline quite close. Variation in the population is expected and is reflected in the blurring ofstructures within the standard (average) brain - although this standard brain was produced with non-linearregistration and is less blurred than the affine version MNI152lin_T1_2mm. However, in this case the image is of anolder 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. Wecan put this transformation matrix to use straight away by transforming the original (non-skull-stripped) imagewith 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 thesame 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 toerrors in brain extraction so we do not use the BET output nor do we register it to the skull stripped template, asit 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 notrun 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 \ --jacrange=0.1,10

    FNIRT actually takes a large number of parameters but here we have used the configuration fileT1_2_MNI152_2mm.cnf to specify most of these for us. The configuration file is simply a text file that contains manyof the parameters that FNIRT takes (and values for them). This enables us to come up with a set of parametersthat works well for matching a T1-weighted structural scan to the MNI152 T1-weighted standard brain and othercommon 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 exactlywhat you are doing you should use existing configurations. FSL comes with configuration files for doing thingslike matching T1 structural images to the MNI152 T1 standard brain (T1_2_MNI152_2mm.cnf) and for matching FA-images to the FMRIB58 FA standard brain (FA_2_FMRIB58_1mm.cnf). In the example above we have restricted theJacobian-determinant (see below) to be in a more narrow range than is normally the case (default 0.01,100). Wedo this to facilitate calculating the inverse of the field (covered in the full FSL course practical).

    Notethat the FNIRT command requires that you specify the FLIRT matrix (flirted_old_brain.mat) that wegenerated previously. If you forget that you will most likely get poor results and may spend a long time tryingto figure out why.

    Also notethat 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 youwill 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 addedwith "Add standard..."). Flip between the registered images and the template to see how well they match. It canalso 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 withyour images, and on the whole the warps are relatively small and well behaved. It will appear as if the 2 imageshave 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 asusing 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 \ --premat=flirted_old_brain.mat

    Note that the important thing here is that we have specified a different reference scan (with -r), one with adifferent 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 differentmatrix 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 inan individual functional image. The mask we will use excludes a rough estimation of the ventricles, cerebellumand brain stem. The registration from the functional image, example_func, to the standard-space template hasalready been done and is saved as exfunc2std.mat. We will use this matrix to transform the standard-space maskto 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 spaceof 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 thetransformation above will have introduced values between zero and one representing the partial volumeoverlaps). 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 substitutethe resulting inverse warpfield for the inverse matrix (but using the -w option insteadof --premat). However, it all takes a little longer so we opted for the quicker versionabove (and for intra-subject registration you would deal with linear registrationsanyway, 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 beforestarting 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 byFNIRT, which is used to store the deformation field (also called a warpfield). It also contains the information thatwe passed from FLIRT to FNIRT (i.e. the information in flirted_old_brain.mat). However, it is not really useful toview it (the coefficients) using fslview.

    To view the deformation field itself it is necessary to convert the coefficients stored in coef_old_subj_to_MNI152into fields. This is done using the command fnirtfileutils to change it into a vector image. If you run thecommand

    fnirtfileutils --in=coef_old_subj_to_MNI152 \--ref=$FSLDIR/data/standard/MNI152_T1_2mm \--out=field_old_subj_to_MNI152_nonlinear_only

    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 tendsto 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 convertwarpor 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), y-displacements (volume 1) and z-displacements (volume 2). You will often need to adjust the display range forthese images since fslview determines it from the first volume only. The "intensity" values you observe when youclick 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 thedeformation fields. When we ran FNIRT we used --jout=jac_old_subj_to_MNI152 (see above if you haveforgotten) to specify that we wanted the Jacobian determinants (one per voxel) written to the filejac_old_subj_to_MNI152.

    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 \ --jac=jac_old_subj_to_MNI152

    The Jacobian field is useful for checking what liberties FNIRT has been taking with your data. The interpretationof 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 beenshrunk by a factor of 5 (e.g. 1mm3 -> 0.2mm3). In general we don't want the Jacobian to be either too small or toolarge since it seems reasonable that a "function" that exists in both brains should be "implemented" inapproximately equal amounts of brain tissue. To check this load jac_old_subj_to_MNI152 into fslview (you mightneed 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 useof schedule files and initialised transforms without search which is also very useful for cases of difficult imageregistration.

    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 transformationmatrix 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 theflexibility, 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 transformationabove 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 ill-constrained 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 animprovement?

    Note that a small FOV image that is "tilted" will have an arbitrary cutoff line running across its top and bottomslices. 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 imagesand 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 transformationmatrix 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 asrotation angles, translations and skews as well as the forward and backward half transformations (which areuseful to take images into a "halfway" space which equalises interpolation artefacts).

    The End.