SPM99 Gem 14: Down Sampling

Subject: Re: resample to lower resolution
From: John Ashburner <john@FIL.ION.UCL.AC.UK&gt
Date: Wed, 3 Jul 2002 12:35:22 +0100 (07:35 EDT)
To: SPM@JISCMAIL.AC.UK

> Is there a way to resample images of 512x512 spatial resolution down to
> 256x256 resolution?

If you copy and paste the following into Matlab, then it should do the job
for you.  Note that I haven't fully tested the code, but it worked on the
one image I tried it on.  Note that it only reduces the data using Fourier
transforms in two directions.  Combining slices in the 3rd direction is
just by averaging.

Best regards,
-John
%* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 

% Select image to reduce
V           = spm_vol(spm_get(1,'*.img','Select image'));

% Create new image header information
VO          = V;
VO.fname    = 'reduced.img';
VO.dim(1:3) = floor(VO.dim(1:3)/2);
VO.mat      = V.mat*[2 0 0 -0.5 ; 0 2 0 -0.5 ; 0 0 2 -0.5 ; 0 0 0 1];

% Write the header
VO          = spm_create_image(VO);

% Work out which bits of the Fourier transform to retain
d1  = VO.dim(1);
d2  = VO.dim(2);
if rem(d1,2), r1a = 1:(d1+1)/2; r1b = [];     
         r1c = [];          r1d = (d1*2-(d1-3)/2):d1*2;
else,         r1a = 1:d1/2;     r1b = d1/2+1; 
         r1c = d1*2-d1/2+1; r1d = (d1*2-d1/2+2):d1*2;
end;
if rem(d2,2), r2a = 1:(d2+1)/2; r2b = [];     
         r2c = [];          r2d = (d2*2-(d2-3)/2):d2*2;
else,         r2a = 1:d2/2;     r2b = d2/2+1; 
         r2c = d2*2-d2/2+1; r2d = (d2*2-d2/2+2):d2*2;
end;

for i=1:VO.dim(3),

        % Fourier transform of one slice
        f = fft2(spm_slice_vol(V,spm_matrix([0 0 (i*2-1)]),VO.dim(1:2)*2,0));

        % Throw away the unwanted region
        f1  = [f(r1a,r2a) (f(r1a,r2b)+f(r1a,r2c))/2 f(r1a,r2d)
             ([f(r1b,r2a) (f(r1b,r2b)+f(r1b,r2c))/2 f(r1b,r2d)]+...
              [f(r1c,r2a) (f(r1c,r2b)+f(r1c,r2c))/2 f(r1c,r2d)])/2
               f(r1d,r2a) (f(r1d,r2b)+f(r1d,r2c))/2 f(r1d,r2d)]/4;

        % Fourier transform of second slice
        f = fft2(spm_slice_vol(V,spm_matrix([0 0 (i*2  )]),VO.dim(1:2)*2,0));

        % Throw away the unwanted region
        f2  = [f(r1a,r2a) (f(r1a,r2b)+f(r1a,r2c))/2 f(r1a,r2d)
             ([f(r1b,r2a) (f(r1b,r2b)+f(r1b,r2c))/2 f(r1b,r2d)]+...
              [f(r1c,r2a) (f(r1c,r2b)+f(r1c,r2c))/2 f(r1c,r2d)])/2
               f(r1d,r2a) (f(r1d,r2b)+f(r1d,r2c))/2 f(r1d,r2d)]/4;

        % Create a simple average of two FTs and do an inverse FT
        f   = real(ifft2((f1+f2)))/2;

        % Write the plane to disk
        VO  = spm_write_plane(VO,f,i);
end;


SPM99 Gem 13: Setting Blob Color Bar Window

Subject: Re: colour bar
From: John Ashburner &ltjohn@FIL.ION.UCL.AC.UK&gt
Date: Thu, 28 Mar 2002 11:23:16 +0000 (06:23 EST)
To: SPM@JISCMAIL.AC.UK

> I would like to use the same colour bar for two different VBM experiments..
> Please, does anyone know how can I do this?

If this is for the display of orthogonal views, then you can tweek the
values that the blobs are scaled to by using a little bit of extra
Matlab code.  First of all, display an image, with superimposed blobs.
Then in Matlab, type:

        global st
        st.vols{1}.blobs{1}.mx

This will give the maximum intensity of the set of blobs.  Then do the
same with the other set of results.  Find the largest of both results
(suppose it is 5.6) and scale the blobs so they are displayed with
this maximum by:

        global st
        st.vols{1}.blobs{1}.mx = 5.6;

Best regards,
-John


SPM99 Gem 12: fMRI Analysis Threshold

OK, this mail isn’t from John either, and it doesn’t even reference John’s code, but it’s useful info that I’ve needed on more than one occation.

The difficulty is that the fMRI interface doesn’t querry about masking; this email spells out how to overcome this.

Subject: Re: explicit masking
From: Stefan Kiebel <skiebel@fil.ion.ucl.ac.uk&gt
Date: Tue, 27 Jun 2000 11:43:52 +0100
To: "Kevin J. Black" <kevin@npg.wustl.edu>, SPM <spm@mailbase.ac.uk&gt

Dear Kevin,

> Is it possible to instruct spm99 to search all voxels within a given
> mask image rather than all above a fixed or a %mean threshold? 

Yes, with SPM99 it's possible to use several masking options. 

To recap, there are 3 sorts of masks used in SPM99:
   1. an analysis threshold
   2. implicit masking
   3. explicit masking

1: One can set this threshold for each image to -Inf to switch off this
   threshold.
2: If the image allows this, NaN at a voxel position masks this voxel
   from the statistics, otherwise the mask value is zero (and the user
   can choose, whether implicit masking should be used at all).
3: Use mask image file(s), where NaN (when image format allows this) or
   a non-positive value masks a voxel.

On top of this, SPM automatically removes any voxels with constant
values over time.

So what you want is an analysis, where one only applies an explicit
mask. 

In SPM99 for PET, you can do this by going for the Full Monty and
choosing -Inf for the implicit mask and no 0-thresholding. Specify one
or more mask images. (You could also define a new model structure,
controlling the way SPM for PET asks questions).

With fMRI data/models, SPM99 is fully capable of doing explicit masking,
but the user interface for fMRI doesn't ask for it. One way to do this
type of masking anyway is to specify your model, choose 'estimate later'
and modify (in matlab) the resulting SPMcfg.mat file. (see spm_spm.m
lines 27 - 39 and 688 - 713).

   1. Load the SPMcfg.mat file, set the xM.TH values all to -Inf,
      set xM.I to 0 (in case that you have an image format not 
      allowing NaN). 
   2. Set xM.VM to a vector of structures, where each structure
      element is the output of spm_vol. For instance: 
               xM.VM = spm_vol('Maskimage');
   3. Finally, save by
               save SPMcfg xM -append

> If so, does the program define a voxel to be used as one which has
> nonzero value in the named mask image?

Not nonzero, but any positive value and unequal NaN. Note that you can
specify more than one mask image, where the resulting mask is then the
intersection of all mask images.

Stefan

SPM99 Gem 11: Editing Analyze Headers

OK, this mail is actually from Matthew Brett, but it references code written by John.

Subject: Re: Scale factor
From: Matthew Brett 
Date: Tue, 22 May 2001 18:47:15 -0700 (21:47 EDT)
To: SPM@JISCMAIL.AC.UK

Dear Masahiro,

> I would like to incorporate scaling factors to many header files.
> It would be nice if I can incorporate multiple scaling factors saved
> in a text file into multiple header files.  Is there a way to do
> this? 

For the same problem, I have used an utility written
by John Ashburner many eons ago, called dbedit:

http://www.mrc-cbu.cam.ac.uk/Imaging/dbedit.html

You need to script it somehow, obviously, but the line in the file
setting your scale factor might look like:

    dbedit myfile.hdr dime.funused1=$myval

where your scale factor is in the variable $myval

I've also been playing with some perl functions to do this kind of
thing, written by Andrew Janke:

http://www.cmr.uq.edu.au/~rotor/software/

Best,

Matthew
  

dbedit tarball:dbedit.tar.gz

SPM99 Gem 10: Surface Renderings: Rolling your own (w/ blobs!)

Subject: Re: rendering SPM96
From: John Ashburner <john@FIL.ION.UCL.AC.UK&gt
Date: Tue, 20 Feb 2001 16:22:05 +0000
To: SPM@JISCMAIL.AC.UK

SPM96 shows voxels that are considered to be between 5mm in front of and 20mm
behind the surface that is displayed.  This is the same as for the "old style"
rendering of SPM99.

To change this depth, try changing line 131 of spm_render.m from:
        msk = find(xyz(3,:) &lt (z1+20) &amp xyz(3,:) &gt (z1-5));
to something like:
        msk = find(xyz(3,:) &lt (z1+5) &amp xyz(3,:) &gt (z1-5));


I would rather see the rendering done on the brain of either the same
subject that the data were acquired, or on a nice smooth average brain
surface.  Rendering can be done on a smooth average in SPM99, and there
is also the capability of doing the rendering on to the individual subjects
MR.  The rendering in SPM96 is done onto a single subject brain, which can
be quite misleading as it is not the same subject that the data comes
from.  Depths behind the surface are derived from the single subject brain,
which can cause problems as spatial normalisation is not exact.  An activation
that could be on the brain surface may appear a few mm in front of or behind
the single subject brain surface.

Another option within SPM99 is to use the brain extraction feature to
identify the approximate brain surface of a segmented structural image.
This would be saved as surf_*.mat.  A routine similar the one attached
can then be used to render the blobs on to this surface.

Best regards
-John
  

Attached file:fancy_rendering.m

SPM99 Gem 9: Rotations and translations from .mat files

Subject: Re: rot+trans
From: John Ashburner <john@fil.ion.ucl.ac.uk>
Date: Fri, 6 Oct 2000 16:15:23 +0100 (BST)
To: spm@mailbase.ac.uk, pauna@creatis.insa-lyon.fr

| 1 how can I find the transformations
| (rot+translations) from the transformation matrix?

The relative translations and rotations between a pair of images
is encoded in the .mat files of the images.  So for images F.img and
G.img, you would find the transformation by:

	% The mat file contents are loaded by:
	MF = spm_get_space('F.img');
	MG = spm_get_space('G.img');
	
	% A rigid body mapping is derived by:
	MR = MF/MG; % or MR = MG/MF;
	
	% From this, a set of rotations and translations
	% can be obtained via:
	params = spm_imatrix(MR)
	
	% See spm_matrix for an explanation of what these
	% parameters mean

SPM99 Gem 8: Reslicing images

This is for soley reslicing images, but it has a nice description of how to create a basic transformation matrix.

Subject: Re: how to write out a resampled volume with new user specified
              voxel size
From: John Ashburner <john@FIL.ION.UCL.AC.UK&gt
Date: Thu, 4 Jan 2001 10:57:52 +0000
To: SPM@JISCMAIL.AC.UK


|       I would like to batch resample some volume images to isotropic
| voxels.  Is there an SPM99 command line instruction (or menu option) that
| would permit me to write out a resampled volume with a user specified voxel
| size dimensions?  I know this core function is implicit in several
| modules.  I considered using Coregister or Spatial Normalization modules
| but these require a target or template volume of the desired resolution
| which doesn't always exist in my application.
|      Many thanks for any suggestions.

You could try the attached function that I've just scribbled together.  It
hasn't been tested, so I hope it works.

Best regards,
-John

The attached function is here reslice.m and below

function reslice(PI,PO,dim,mat,hld)
% FORMAT reslice(PI,PO,dim,mat,hld)
%   PI - input filename
%   PO - output filename
%   dim - 1x3 matrix of image dimensions
%   mat - 4x4 affine transformation matrix mapping
%         from vox to mm (for output image).
%         To define M from vox and origin, then
%             off = -vox.*origin;
%              M   = [vox(1) 0      0      off(1)
%                     0      vox(2) 0      off(2)
%                     0      0      vox(3) off(3)
%                     0      0      0      1];
%
%   hld - interpolation method.
%___________________________________________________________________________
% @(#)JohnsGems.html	1.42 John Ashburner 05/02/02

VI          = spm_vol(PI);
VO          = VI;
VO.fname    = deblank(PO);
VO.mat      = mat;
VO.dim(1:3) = dim;

VO = spm_create_image(VO); end;
for x3 = 1:VO.dim(3),
        M  = inv(spm_matrix([0 0 -x3 0 0 0 1 1 1])*inv(VO.mat)*VI.mat);
        v  = spm_slice_vol(VI,M,VO.dim(1:2),hld);
        VO = spm_write_plane(VO,v,x3);
end;


SPM99 Gem 7: Reorienting images

Subject: Re: AC-PC positions
From: John Ashburner 
Date: Fri, 27 Oct 2000 15:00:05 +0100 (BST)
To: spm@mailbase.ac.uk, spm@fil.ion.ucl.ac.uk

[…]

The best that I can suggest is you try manually reorienting your
images via the <Display&gt button.  Try different rotations and
translations until the image is displayed how you want it.  The
attached Matlab function can then be used for reslicing the image(s)
in the transverse orientation, with 1mm isotropic resolution.

Best regards,
-John

Attached file: .reorient.m~
Error in c matrix fixed, June 6, 2001 -TEN

Subsequently, John offered modifications to use the native voxel size, which have been incorporated…

See also SPM5 Gem2 & Gem3

Subject: Re: reorient.m
From: John Ashburner 
Date: Thu, 14 Dec 2000 17:04:38 +0000
To: SPM@JISCMAIL.AC.UK

| I would like to reorient some images in two ways,
| using the usefull function reorient() of John Ashburner.
|
|   - first is to keep the voxel size intact,
| but still reorienting in the transverse plane

Without testing the code, and without too much thought, I think
the modification to do this is involves something like changing from:
        mat = spm_matrix([mn-1]);
        dim = (mat\[mx 1]')';
to something like:
        vox = spm_imatrix(V.mat);
        vox = vox(7:9);
        mat = spm_matrix([0 0 0  0 0 0  vox])*spm_matrix([mn-1]);
        dim = (mat\[mx 1]')';

|
|   - second is to reorient in the coronal plane,
|     with 1x1x1 mm resol.

I think this is involves something like changing from:
        mat = spm_matrix([mn-1]);
to something like:
        mat = spm_matrix([0 0 0 pi/2])*spm_matrix([mn-1]);
or maybe:
        mat = spm_matrix([0 0 0 -pi/2])*spm_matrix([mn-1]);

|
| In the two cases, the final image should have no .mat file
| and be resliced using sinc ...

To reslice using sinc interpolation, you change from:
                img = spm_slice_vol(V,M,dim(1:2),1);
to something like:
                img = spm_slice_vol(V,M,dim(1:2),-6);

I hopehese suggestions work.
Best regards,
-John
  

If you want to set arbitrary voxel size, just setvoxas desired in the fix above, but then be sure to forcedimto be an integer.

For example, I wanted to increase the resolution of my images by a factor of three, so I did

        vox = spm_imatrix(V.mat);
        vox = vox(7:9)/3;
        mat = spm_matrix([0 0 0  0 0 0  vox])*spm_matrix([mn-1]);
        dim = ceil(mat\[mx 1]')');

SPM99 Gem 6: Reading raw data

Subject: Re: raw data
From: John Ashburner <john@FIL.ION.UCL.AC.UK&gt
Date: Thu, 10 May 2001 14:33:19 +0100
To: SPM@JISCMAIL.AC.UK

> I have a very simple question.  Where does one find and how does one go
> about extracting the raw data associated with a particular voxel?

I would write a very short script to do this.  Try modifying and copy-pasting
the following....
* * * * * * * * * * * * * * * * * * * * * * * * * * * * --
V=spm_vol(spm_get(Inf,'*.img'));
x = 32;
y = 32;
z = 32;

dat = zeros(length(V),1);
for i=1:length(dat),
        dat(i) = spm_sample_vol(V(i),x,y,z,0);
end;
* * * * * * * * * * * * * * * * * * * * * * * * * * * * --

The vector dat will contain the raw data from the voxel at 32,32,32.

Best regards,
-John


SPM99 Gem 5: Mesh Plots of t–maps

Subject: Re: Mesh Plots of t-maps
From: John Ashburner <john@FIL.ION.UCL.AC.UK>
Date: Fri, 2 Feb 2001 11:14:20 +0000
To: SPM@JISCMAIL.AC.UK

| I am wishing to create Mesh plots in Matlab of the t-map produced
| from my SPM analysis. Being a somewhat Matlab virgin, I was wondering
| whether
| someone could possibly point me in the direction of exactly how to do this.
|
| Which mat file contains all t-values in the statistic image?

The t values are stored in spmT_????.img files, where ???? refers to
the contrast number.  You can read the values from these images into
Matlab something like:

        pl    = 30; % plane 30
        fname = spm_get(1,'*.img','Name of t image');
        V     = spm_vol(fname);
        M     = spm_matrix([0 0 pl]);
        img   = spm_slice_vol(V,M,V.dim(1:2),1);

Displaying the values can be dome something like:
        surf(img);

There are loads of commands for 3D visualisation in Matlab 5.x.  You can
find out what these are by typing:
        help graph3d

All the best,
-John