Jump to: navigation, search

See Also: mrRx

rxAlign is a script that calls the mrRx tool to align inplane anatomicals to a volume.


[edit] Getting started

To start rxAlign, type


at the matlab prompt, or select Edit | Edit/View Alignment from a mrVista window.

[edit] Background about mrRx

mrRx (for “mister prescribe”) is a Matlab tool for applying a slice prescription onto an anatomical volume. This slice prescription can be described mathematically as a 4x4 affine transformation matrix. This matrix maps between coordinates in the original volume matrix, and a new coordinate space, which we call the "prescription".

In general, this tool doesn't require a second volume to make a prescription: you could test a prescription before going to the scanner, for instance (details for things like this are on the main mrRx page). However, when you call rxAlign, you are specifically trying to make a prescription that matches the Inplane anatomies for your mrVista session.

rxAlign allows you to perform both automatic and manual alignment steps. You can save this 4x4 matrix in a number of formats, including the one used in the mrSESSION.alignment field. It allows you to alter translation and rotation settings (and, when necessary, flipping the volume along one axis or another); shows the ‘interpolated’ inplane slices that result for these settings; and lets you compare those images to a reference image. In this case, the reference images are the inplane images collected during the scanning session. The goal is to make the interpolated inplanes as much like the collected inplanes as possible.

[edit] The math part

Here's a rough guide to how the mapping works: suppose you have a set of coordinates in your inplane anatomy, which you keep in a matrix you call InplaneCoords. InplaneCoords is a matrix with 3 rows and several (N) columns: each column is the (row, column, slice) position of a voxel, respectively, in the inplane anatomy data matrix. What you want to know is, what is the set of coordinates in my reference volume anatomy which match these inplane coordinates? We can name this desired matrix VolumeCoords. The mapping we want would be:

VolumeCoords = Xform * [InplaneCoords; ones vector]

In this case, Xform is the 4x4 affine matrix that you edit in mrRx, and the ones vector is just a vector of ones, one for each column in VolumeCoords. (We need this vector, to allow Xform to contain a set of translation terms, which don't depend on the particular coordinates in VolumeCoords.) InplaneCoords will be a 4 x N matrix, with each row representing the inplane coordinates for the cooresponding column in VolumeCoords.

(One note about this: The mrSESSION.alignment matrix applies transformations in which the rotations are relative to the point (1,1,1), which is the upper left-hand corner of the first inplane slice. mrRx always rotates about the center of a volume; but again, this is accounted for when the alignment is saved.)

[edit] Calling mrAlignMI alignment from mrRx

You can run the same alignment steps as in mrAlignMI, by first starting rxAlign (step 1 below) and selecting the menu:

Alignment >> Coarse >> Mutual Information (mrAlignMI)

The code that performs this is rxFineMutualInf. This alignment tool gets an initial coarse alignment from the anatomy headers (if available; they're traditionally in Raw/Anatomy/Inplane), and optimizes using the spm mutual information code. If you want to use this code, but start from your current settings in mrRx rather than from the header information, select:

 Alignment >> Fine >> Mutual Information (mrAlignMI)

These steps seem to produce high-quality alignments for many prescriptions. The one exception seems to be high-resolution sessions in which the prescription covers a very small part of the whole brain (e.g., part of one hemisphere). The code seems to do well for more common prescriptions using a surface coil, which only cover the posterior part of the brain (but a large amount of this cortex). Also note that by default, the code compensates for intensity gradients. This and other parameters can be adjusted by invoking the command (rxFineMutualInf) from the command line.

[edit] rxAlign Walkthrough

To start rxAlign:

1) Type rxAlign at the matlab prompt, or select Edit | Edit/View Alignment from a mrVista window.

2) Four figures will open on screen: a CONTROL figure at top, a PRESCRIPTION (Rx) figure to the middle-left of the screen, a PRESCRIBED SLICE figure in the center, and a REFERENCE SLICE figure to the middle-right.

Sample rxAlign Session

The Control figure contains controls for setting a 4x4 affine transformation which can be applied to the volume anatomy (vAnatomy) to produce the inplane prescription. The Rx figure shows where on the vAnatomy the prescription lies. The Prescribed Slice figure shows an an interpolated slice from the vAnatomy, after the transformation has been applied. The Reference Slice figure shows the equivalent slice from the inplane anatomies.

The goal of the alignment is to make the interpolated slice as much like the reference slice as possible.

In addition, if a screen save image is available for the session, a fifth figure opens with the screen save image:

Rx Figure and Screen Save Figure

3) If you don’t have a screen save available, go ahead to step 4. Otherwise, use the screen save as a guide for an initial alignment:

a) Use the orientation radio buttons in the Rx figure to match the orientation shown on the screen save.

b) Use the slice slider in the Rx figure to page to a slice in the Rx figure that matches that shown in the screen save.

c) Use the Translation sliders, to the right of the mrRx control figure, to adjust the up/down, anterior/posterior, and left/right locations of the prescription to match the center shown on the screen save. You can also recenter the Rx by clicking directly on the axes in the Rx window – the new center of the Rx will be where you clicked.

d) Use the Rotation sliders, in the center of the mrRx control figure, to approximate the angle shown on the screen save.

e) When you’re satisfied with the coarse alignment, press the ‘Store’ button, to the lower left-hand side of the control figure, to remember these settings. You may also want to select the menu option ‘File’ | ‘Save…’ | ‘mrRx settings’ to save out these settings to file.

f) Close the Screen Save and Rx figures.

4) Open the Prescribed and Reference Slice figures. Use the translation and rotation sliders to get a coarse alignment, such that the same features (sulci, cerebellum, skull) are visible in the same interpolated and reference slices. You can use the Rx Slice slider, to the left of the control figure, to select the slices to compare. The Slider Step slider controls the size which the rotation and translation sliders change when you click on each slider (you can manually enter amounts for arbitrary precision).

a) You can use the Rx Slice slider, to the left of the control figure, to select the slices to compare. The Slider Step slider controls the size which the rotation and translation sliders change when you click on each slider (you can manually enter amounts for arbitrary precision). The ‘Nudge In’ and ‘Nudge Out’ buttons apply a slight translation along the axis connecting the center of the first and last slices of the prescription. The size of the nudge is also determined by the Slider Step slider.

b) If you find that the prescription goes in the opposite order of the reference slices—e.g., the first prescribed slice is the most medial,but the first reference slice is the most lateral—you can select ‘Edit’ | ‘Flip Slice Order’ to set a rough flip in the order. You may need to translate the prescription back, though.

5) When the same features are visible in each slice, select the menu option ‘Window’ | ‘Open Rx/Ref Comparison Window’. A new figure will open up to the lower left:

This figure allows you to overlay the reference and prescribed slices. Three visualizations are possible: showing the prescibed slice in red and the reference slice in blue-green, such that high overlap appears gray; showing a color-coded image of the prescribed minus the reference slice; and showing a checkerboard of alternating tiles of prescribed and reference images. These can be selected with the popup at the bottom of the figure. In addition a correlation measure, and root-mean-squared error, are shown between to the two slices. Making the slices similar entails finding a local maximum for the correlation, and a local minimum for the error.

Opening the Comparison Figure

You can toggle between two images with the toggle button at the lower right-hand corner of the comparison figure. In addition, there is a menu option on the Comparison Figure: Comparison Prefs | Compare all slices (separate GUI). A new window will open with the two volumes superimposed. You can set both volumes to a grayscale colormap using Ctrl-5; and toggle between the two quickly using Ctrl-T.

6) To get a fine manual alignment, you can select corresponding points on the reference and prescribed slices:

a) Select Edit | Points | Add Points<Ctrl-P>

b) The Reference figure will turn yellow. Select a point on the reference slice for which you can find the corresponding point in the prescribed slice.

Selecting a Point on the Reference Slice

c) Select the corresponding point on the prescribed slice.

d) Repeat the process for as many points as you want to add. Press the right mouse button to stop adding points.

e) Switch to another slice, and add more corresponding points (you can do a series of even slices followed by a series of odd slices). It’s a good idea to select points in early, middle, and late slices in the inplanes, to avoid have the alignment be close for one area, but get progressively worse away from that region.

f) Store the prescription settings, using the ‘Store’ button, before computing the from-points alignment.

g) Select Alignment | Fine | From Selected Points. A new alignment will be created, and stored under the name Point Alignment. Feedback will be provided on the amount of error between each point and the computed ideal point. If one point has a much higher error than the other points, it was likely poorly chosen (the points on the reference and prescribed slices don’t really correspond). You can delete the points using ‘Edit’ | ‘Points’ | ‘Delete Points’ <Ctrl-D>. A good alignment has an error of less than 1 for all points.

7) Other, more automated alignments are available: the Nestares code refers to the alignment method developed by Oscar Nestares and David Heeger, described in this paper:

Nestares O, Heeger DJ, 2000 Robust multiresolution alignment of MRI brain volumes. Magn Reson Med. 43(5):705-15..

The Mutual-Information alignment refers to a mutual-information based metric which uses the SPM co-registration tools (see the spm_coreg function in SPM2, which cites the following papers: Template:Ref Template:Ref Template:Ref Template:Ref Template:Ref.

8) To save the alignment, select

File >> Save… >> mrVista alignment

If you are saving over a previous alignment, you will want to re-install the segmentation using the menu

Gray >> Gray/White Segmentation >> Install or Reinstall Segmentation

in the mrVista view. If you just want to save the alignment settings without saving it as the main alignment, select

File >> Save... >> mrRx Settings

This will create a file mrRxSettings.mat in the current directory, containing a list of all alignment settings.

[edit] User tips on achieving a good Inplane->Volume alignment

These are suggestions that somewhat duplicate, but reorganize the information written above. The hope is that by making our practices and heuristics explicit, we can more easily make improvements and share them across labs without relying on the somewhat unreliable oral tradition method. So expect these to change (and please suggest changes!) as we share our "best practices" with each other. In other words, caveat emptor.

[edit] Before you start your own alignments

One of the first things you should do before you begin is take a look at some well aligned brains in order to get a sense of what a good alignment is. You can find examples of alignments that we think are especially good here.

[edit] mrRx sliders

Many of the sliders are fairly straightforward to understand, and their units (mm/deg) are clearly marked. You can adjust the step size for the various sliders, which is helpful as you move from farther (larger steps) to closer (smaller steps) to a good alignment. Try to play around with all of them until you understand how each one works, and you feel like looking at a particular mismatch between inplane and volume, you would know which sliders to manipulate to get the alignment closer to ideal.

Easy options to miss are the three flip check boxes on the right-hand edge of the mrRx window.

The more confusing sliders include the three rotation sliders in the central panel. Some of their effects are fairly counterintuitive if you are just adjusting rotation slider values and looking at the Reference slice and Prescribed slice images alone. We recommend that you also see how changes in the rotation slider values affect the mrRx Prescription on Volume image (the one with the slice prescription depicted as colored lines).

  • Rotate Axials and the Sag view on the mrRx Prescription on Volume image
  • Rotate Coronals and the Axi view on the mrRx Prescription on Volume image
  • Rotate Sagittals and the Cor view on the mrRx Prescription on Volume image

[edit] Training your eye to "see" a good alignment

These are just tips to start with, it's likely they won't work for everyone and that there will be many better methods out there. Basically, the goal is be able to "see" how good the alignment is by viewing the Rx/Ref Comparison Window and seeing how much the brain appears to "move" when you toggle between the Rx and Reference slices for various slices throughout the brain. Make sure to check many different slices along the brain, as the alignment may not be of the same quality throughout all slices. Try also to learn what a "bad" alignment looks like by creating a misalignment and seeing what the consequences are.

Toggling: Toggling in the Rx/Ref Comparison Window is useful. The ideal would be to hit toggle, and not be able to distinguish when the toggling switches between the Rx image and the Ref image. In reality, this will never actually happen. But you can get close. For novice togglers, it might be helpful to change the number of times (line 16, nToggles), or the speed at which the toggling occurs (line 37, pause(0.15)) by editing rxToggle.m

Zooming: When you are toggling or viewing overlays, it's easy for things that are not the brain (e.g., skull, ears) to distract you. So, we recommend that toggles and overlays are conducted on images zoomed in at least enough so that distracting non-brain elements are cropped out and don't influence your judgement.

  • In the mrRx slider window, choose from the menu: View -> Figure menus. This will add a bunch more menu options to the window.
  • Tools -> Zoom in: Now, when you float your mouse over the Rx/Ref Comparison Window, it should take on the zoom icon (magnifying glass with a plus sign). Click and drag to define a box in which zooming will occur. Right-click to get a zoom context menu (e.g,. Reset to original view; Zoom out).

Comparison methods (mrRx Compare Interp/Ref window): edit rxCompare

  • Overlay (red/blue): Here it is helpful to adjust the amount of red and blue contributed from each image by adjusting the Rx alpha and Ref alpha sliders until you can make out the outlines of both brains.
  • Thresholded Overlay : This is one of my favorites as the different images seem easier to see than in the default method of red/blue overlay. I adjust the Ref alpha downwards (so that the red heat map starts to reveal more and more brain underneath. I dial the alpha down until I just start to see outlines of sulci and ventricles, and then all I have to do is try to match up the outlines.
  • Checkerboard: The usefulness of this method relies you your finding a slice where the checkerboards split some kind of outline (e.g., a long sulcus), such that you can try to match up the trajectory of the line across the different images. You can also make the checkerboard checks larger by editing rxCompare case 3: compareImg = regMosaic(A, B, 10) (change the third number to a smaller number like 5.
  • Subtract: Before interpreting this map, make sure you have chosen in, in the mrRx Compare Interp/Ref window, Comparison Prefs-> Use mrAlign Intensity Correction, otherwise you will mostly be seeing intensity inhomogeneity regions, not regions of misalignment.
  • Correlation [R] values: If you have trained your eye well to know what a good alignment looks like (e.g., by trying many yourself and looking at many examples), you can safely ignore this number. There does not seem to be a value at which you can rest assured you have a good alignment, correlations vary from alignment to alignment, and the number may actually go down if you have a good brain alignment (e.g., if the skull misalignment then becomes large). Trust your eye, not this number.

[edit] Alignment Settings

After you open RxAlign, you will have a list of different settings in the top left corner (add examples here).

  • Saving issues: xform settings (will just save to mrRx GUI list of alignments), mrVista alignment, dangers of forgetting / being confused between multiple alignments. When you are trying to save a finished alignment, you should always choose to save it as a mrVista alignment. If you want to finish an alignment at a later date and save your settings, you should choose to save your rxAlign settings?? [Alina]
  • Retrieve: Make sure you highlight the setting you would like to use and then select retrieve.
  • Store: Once you have completed your alignment to your satisfaction, you should select store and a new alignment will be added as a setting. [Alina]
  • Delete: In addition to storing alignments, anytime you use any fine or coarse alignments like Point Alignment or Mutual Information, this alignment will be stored in the settings. Make sure to erase any unnecessary settings by highlighting and deleting.

[edit] Initial coarse alignment

This is likely the first thing you will do in adjusting your alignment. Open a saved screen shot of your prescription (if it exists). From the mrRx window, go to Window -> Open Screen Save Window

  • Try to match up your 'mrRx Prescription on Volume Window' to your screen shot.
  • HOWEVER, if you do not have a SS, try to do a coarse alignment by hand and check to make sure:
    • The hemispheres match up (i.e., the inplanes and volume anatomy are not incorrectly right/left flipped relative to eachother)
    • Slices are close in the anterior/posterior position, up/down position, and close in terms of the slice angle.
    • This step is usually not necessary if the initial default alignment is close enough. <picture of initial close coarse alignment>

[edit] Automatic Alignment

You should make sure you have a decent initial coarse alignment before running automatic alignment because we suspect that it will make your alignment even worse otherwise (automatic alignments seem to be somewhat sensitive to starting states).

  • Mutual information (References):
    • If you were able to use the SS for alignment or you were successful in coarse aligning the inplane and volume by hand, use Alignment -> Fine -> Using Mutual Information.
      • Whenever you are using Alignment -> Fine, you are starting from the current mrRx settings instead of the initial default settings.
    • If the initial default alignment was acceptable and you didn't do any coarse hand alignment, use Alignment ->Coarse -> mrAlignMI
  • Nestares:

[edit] Fine manual alignment

Result of automatic alignment. These images are misaligned but it is hard to tell
The ROI here is the union of all visual field maps defined in this subject. Loading the ROI makes it obvious that the automatic alignment did not do a good job
After fine manual correction, the alignment is much better. You should check other slices to make sure that the alignment is good everywhere, and not just this slice.

This is by far the trickiest and most subjective step. Some people don't appear to do this step at all, as they can live with the alignment quality that results from automatic alignment. However, other research questions require the better alignment than typically results from automatic alignment alone. In my experience, good manual alignment can buy you an improvement in alignment of anywhere from 1-5mm over an automatic alignment alone, although the price is the time you put into trying to develop an "eye" for alignments, and the uncertainty of knowing that you are relying on a manual, subjective component to your alignment process. --Davie 18:35, 4 February 2009 (PST)

  • Point alignment is an option. Point alignment works best when you are pretty close to a good alignment. It should always be your last step.
    • It is important to pick clear and reproducible points whenever you are using this feature. T-junctions in the gray matter are usually the most point-like structure in the brain.
    • Choosing points is also easier and more accurate when you zoom in on a brain section.
    • It is also useful to go through and put 3-4 points for every few even slices in the brain, making sure to cover lateral, medial, ventral and dorsal parts of the brain evenly. You can then repeat this for the odd slices. [Also Alina will add extra user-notes on picking good points, and zooming, and point number placement and other choices] [Alina]
  • You can make manual adjustments to improve alignment in the Rx/Ref comparison window
  • Check the alignment in the region or ROI you care most about

Consider the alignment in the images on the right. The top image is the result of an automatic alignment. It looks OK by eye.

When you load an ROI, however, you can see the error. The error is especially clear around the cortex/cerebellum boundary. After hand-editing, the alignment is much better.

Misalignment could have caused a single blob of activation near the POS to fall on the sulcus and "break up" into three pieces on the mesh
    • Often the alignment is difficult to get to be equally good throughout all slices. A work-around is to go for the best alignment possible near the region you're interested in, and just keep notes on how bad the rest of the regions are. For example, if your project is interested in the fusiform gyrus, you should focus on aligning the central slices in that region instead of the most anterior or posterior end. However, what we've noticed is that if the alignment is really good, it's good over the entire range of the prescription.
    • In mrRx: File -> Load -> mrVista ROI: Lets you load a mrVista ROI which will be shown in the Rx/Ref comparison window. This should be an ROI of interest where you really need the alignment to be good. You can specifically go for the ROIs that show up as single blobs on the Inplane/Volume view, but that straddle a sulcus and break up into small pieces when shown on the mesh. A misalignment could cause a blob of activation that should be on a gyrus to shift over to a sulcus and artifically "break up" on the inflated cortical surface representation, as may be happening around the parietal-occipital fissure in the example image.
    • In order to see the ROI on the in the Rx/Ref comparison window, even if you toggle (usually they disappear on toggling), add the following code to rxToggle.m (written by JW)
% -------------------------------------------------------
% -- JW ADD to show ROIs
% get ROI info if it exists
rxSlice = get(rx.ui.rxSlice.sliderHandle,'Value');
rxSlice = round(rxSlice);
prefs.method = 1;
prefs.lineWidth = 1;
for R = rx.rois
   prefs.color = R.color;
   roiCoords = vol2rx(rx, R.volCoords, 1);
   inSlice = find(round(roiCoords(3,:))==rxSlice);
%  -------------------------------------------------------

[edit] Caveat emptor / Pay it forward

Talk to people, think about what you typically do (particularly after you've got several alignments under your belt). What is suggested here and what you'll do in real life might be very different, particularly as different research questions can tolerate different amounts of alignment inaccuracy, and as individual researchers develop their own preferences and potentially write new tools. Finally, pay it forward and update the wiki. If you are reading these directions, you are benefiting from someone else's work -- if you can contribute a clarification (especially references and images!), improvement, or at least a useful alternative or handy tool to know about, you will have repaid your debt in full :)

[edit] Alignment rumors and lore

Bob has proposed a whole-brain scan that can be taken in the same amount of time as a standard inplane, and that may have a chance of better automatic alignment success to the volume anatomy (since both are images of the whole brain). However, if you are conducting an experiment with a surface coil, the whole-brain alignment scan is going to have some pretty gnarly low-level image value differences from the volume anatomy. In the single attempt I tried to use a whole-brain alignment scan taken during a surface coil fMRI session to align automatically to a volume anatomy scan, but the alignment was very off (way worse than MI for aligning inplane->volume).

Of interest is the current wiki section on whole-brain alignment.

[edit] Assessing motion correction alignment quality

[edit] Functional->Inplane alignment

its called "coregistration" in spm. you might find a coreg.m file in SPM2 or SPM5 directory, if not i have a copy of spm2 coreg.m and can send it to you on Mon.

[edit] Reinstalling Alignment

If you have to re-save a new alignment for a subject, there are several steps you need to take:

  1. Save ROIs: If this person has already had ROIs made and they have been saved in the local directory, you need to make sure that you save them to the shared directory because everything in the Gray directory will get deleted.
    • You can do this moving the ROIs from the Gray/ROIs folder into the 3DAnatomy/ROIs folder.
  2. Install Alignment: If everything you need in the Gray directory has been backed up, you can save the new alignment.
  3. Reinstall Segmentation: From the Inplane window, Gray -> Gray/White Segmentation -> Install/Reinstall Segmentation
    • You also have the option of reinstalling the segmentation with all gray nodes (otherwise, it will only create nodes in the areas where your inplane falls).
    • Note: Reinstalling the segmentation will also delete everything in the Gray directoryItalic text.
  4. Retransform Maps: You will need to retransform all the maps in the dataTYPE from the Inplane to the Volume again since they were a casualty of reinstalling the alignment/segmentation.
    • From the Volume window and while you are on the GLMs dataTYPE, Xform-> 'Inplane->Volume'-> All Maps in Data Type-> Trilinear Interpolation
Personal tools