SPM2 Gem 10: Creating a mask from a XYZ

This is a “Jan’s Gem” from Jan Gläscher…

Subject: Re: [SPM] creating a mask from voxel coordinates
From: Jan Gläscher <glaescher@UKE.UNI-HAMBURG.DE>
Date: Tue, 15 Mar 2005 12:24:27 +0100
To: SPM@JISCMAIL.AC.UK


Dear Susana,

you can try the following recipe.  It might not be the easiest way, but it
should work.

Suppose your n MNI coordinates are saved in a 3xn matrix called mni.
(This is essential (not the name, but the format of 3xn), otherwise the
steps below won't work).

1. Create an spm_vol handle to the image, that you determined the
   coordinates from

   Vin = spm_vol(spm_get(1,'*.img','Select image'));

2. Read the information in the image. (The data are not needed, this is
   done purely for the purpose of setting up a matrix of voxel coordinates.

   [Y,XYZ] = spm_read_vols(Vin);

3. Setup a matrix of zeros in the dimensions of the input image

   mask = zeros(Vin.dim(1:3));

4. Now loop over all voxels in your mni variable and set the corresponding
   location in the mask matrix to 1:

   for v = 1:size(mni,2)
      mask(find(XYZ(1,:)==mni(1,v)&XYZ(2,:)==mni(2,v)&XYZ(3,:)==mni(3,v))) = 1;
   end

5. Setup an spm_vol output file handle and change the filename

   Vout = Vin;
   Vout.fname = '/path/to/the/directory/mask.img';

6. Finally, write the new mask to the output file.

   spm_write_vol(Vout,mask);


Cheers,
Jan

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s