- 1 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.
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 surface 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:
H = getappdata(get(findobj('Tag','SPMMeshRender'),'Parent'),'handles'); spm_mesh_render('CLim',H) % return current limits spm_mesh_render('CLim',H,[0 16]); % set limits to [0 16]
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).
ROI = 'myvoi.nii'; % input image (binary, ie a mask) k = 100; % minimal cluster size ROIf = 'newvoi.nii'; % output image (filtered on cluster size) %-Connected Component labelling V = spm_vol(ROI); dat = spm_read_vols(V); [l2, num] = spm_bwlabel(double(dat>0),26); if ~num, warning('No clusters found.'); end %-Extent threshold, and sort clusters according to their extent [n, ni] = sort(histc(l2(:),0:num), 1, 'descend'); l = zeros(size(l2)); n = n(2:end); ni = ni(2:end)-1; ni = ni(n>=k); n = n(n>=k); for i=1:length(n), l(l2==ni(i)) = i; end clear l2 ni fprintf('Selected %d clusters (out of %d) in image.\n',length(n),num); %-Write new image V.fname = ROIf; spm_write_vol(V,l~=0); % save as binary image. Remove '~=0' so as to % have cluster labels as their size. % or use (l~=0).*dat if input image was not binary