# Introduction

This page lists a series of How-tos using SPM. They should work with all recent versions of the software.

## How to manually change the orientation of an image?

To change the origin of an image:
* open the image with SPM Display
* move the crosshair position so that it roughly points to the anterior commissure (AC).
* click on the Set Origin button
* click on the Reorient button and press done (your image is already selected). If you want to apply the same transformation to other images (e.g. if you have a series of functional images), select them all at this stage.
* say No to Do you want to save the reorientation matrix?
This will set the origin of the image (0 0 0 mm coordinates) to AC. You might also want to rotate the image such that it is better aligned with MNI space: to do so, you also need to edit the entries for the rotations (in radian) along the pitch, roll and yaw axes.
You can also try to automate the process using coregistration.
See also: Finding Commissures.
For earlier versions of SPM, see Repositioning MRIs.

## How to compute the mean of a set of images?

Use the ImCalc facility with expression "mean(X)" and Data Matrix option set to "yes - read images into data matrix". You can also choose the output filename (say mean.img) for the mean image and its directory (default is current folder).
Alternatively, you can use the spm_mean_ui function but the number of options is limited - ImCalc should be preferred.
If you want to compute the sum instead, just use expression "sum(X)".

## How to change file paths in a batch file or SPM.mat?

If you change the location of your data and/or the SPM installation, the full paths contained in batch files and SPM.mat will be broken. An utility function spm_changepath is available in SPM to replace all occurrences of one string with another within a MAT-file. For example:
will update the SPM.mat file to replace all occurrence of C:\mydata with D:\Experiments\data. A backup of the initial file can be found in SPM.mat.old. A list of all the altered paths will be displayed at runtime.

## How to choose the colour limits in a rendering?

By default, the colour limits are such that they span min/max of the rendered data. If you want to specify them yourself, you can use the following piece of code.
For surface rendering, use:
For rendering on the three orthogonal views, use:

## How to remove clusters under a certain size in a binary mask?

The following piece of code will read a binary mask (ROI), perform connected component labelling, filter out all clusters under a certain size in voxel (variable k) and save it as another mask (ROIf).

## How to display NIfTI images in SPM when double-clicking on them in MATLAB Current Folder browser?

Save the following functions in your MATLAB path:

## How to overlay a mask image on another one with transparency?

After using Display > Add Overlay or CheckReg > Overlay > Add coloured image, type the following:

## How to change the voxel size of an image?

Run the following (with the appropriate voxel size) and select the images you want to resample. The resliced images will be saved with an 'r' prefix:

## How to automatically reorient images?

The following function (from here) will rigidly align images to a T1 template: