Print

Print


Dear Sebastian,
You are right, that the problem has to do with writing.

If you type "help spm_write_vol", you will get:

   Write an image volume to disk, setting scales and offsets as appropriate
   FORMAT V = spm_write_vol(V,Y)
   V (input)  - a structure containing image volume information (see 
spm_vol)
   Y          - a one, two or three dimensional matrix containing the 
image voxels
   V (output) - data structure after modification for writing.

   Note that if there is no 'pinfo' field, then SPM will figure out the
   max and min values from the data and use these to automatically determine
   scalefactors.  If 'pinfo' exists, then the scalefactor in this is used.
  _______________________________________________________________________
   Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging

What you have to do:

 > % Save new data to file:
 > V.fname = 'rounded.nii';
 > V.pinfo(1)=1;
 > spm_write_vol(V, rnd_data);

and everything should be fine.
I don't know if it's possible to force imcalc to use scalefactor 1.

Michael.

PS: Why not just
matlabbatch{1}.spm.util.imcalc.expression = 'i2';

Am 18.05.2012 10:31, schrieb Sebastian Rieger:
> Dear SPM List Members,
>
> I write to ask for your help with the following problem:
>
> When using imcalc to resample a Brodmann atlas image (which only
> contains integer intensity values) to a different resolution, the
> resulting image contains values which are close to what I expect, but
> are no longer integers. I cannot seem to understand why this happens
> despite the use of nearest neighbour interpolation. I suspect it may
> have something to do with how the file is written and/or read, rather
> than with the actual resampling operation.
>
> Please see the Matlab script below for details on what I have tried to
> do. I would be most grateful for any hints.
>
> Best regards,
>
> Sebastian
>
> --
> Dr. Sebastian W. Rieger, Neurology & Imaging of Cognition, University of
> Geneva
> Postal address: Université de Genève / NEUFO / Sebastian Rieger / Rue
> Michel-Servet 1 / 1211 Genève / Switzerland
> Phone: +41 (0) 22 37-95360 <tel:%2B41%20%280%29%2022%2037-95360> - Web:
> http://labnic.unige.ch/
>
>
>
>
> %% Test script for resampling problem as described by Sebastian Rieger on
> %% the SPM mailing list
>
> % Start with a Brodmann atlas and a random functional image in a test
> % directory. The idea is to resample the atlas to the resolution of the
> % functional image. The problem is that when reading the resampled atlas,
> % the pixel values are no longer integers. This is not due to interpolation
> % errors (nearest neighbour interpolation is used), but there seem to be
> % rounding errors when writing or reading the image.
>
> %% Examine the input images:
>
> % Examine the Brodmann atlas file (I used the one supplied with MRIcro):
> V    = spm_vol('brodmann.nii');
> data = spm_read_vols(V);
> V.mat % It can be seen that the resolution is 1mm isotropic
> unique(data) % It can be seen that all pixel values are integers
>
> % Examining the image whose resolution the atlas will be resampled to:
> V = spm_vol('fxxx.nii');
> V.mat % It can be seen that the resolution is three point something mm
>
> %% Call imcalc to perform the resampling:
>
> % Set up imcalc job:
> matlabbatch{1}.spm.util.imcalc.input = {
> 'D:\imcalctest\fxxx.nii,1'
> 'D:\imcalctest\brodmann.nii,1'
>                                          };
> matlabbatch{1}.spm.util.imcalc.output = 'brodmann_resampled.nii';
> matlabbatch{1}.spm.util.imcalc.outdir = {''};
> matlabbatch{1}.spm.util.imcalc.expression = 'ones(size(i1)).*i2';
> matlabbatch{1}.spm.util.imcalc.options.dmtx = 0;
> matlabbatch{1}.spm.util.imcalc.options.mask = 0;
> matlabbatch{1}.spm.util.imcalc.options.interp = 0; % Nearest neighbour
> matlabbatch{1}.spm.util.imcalc.options.dtype = 4;
>
> % Save imcalc job for later examination in batch manager GUI:
> save 'D:\imcalctest\imcalcjob' matlabbatch;
>
> % Execute imcalc job:
> spm_jobman('run',matlabbatch);
>
> %% Examine the result:
>
> V    = spm_vol('brodmann_resampled.nii');
> data = spm_read_vols(V);
> V.mat % It can be seen that the resolution has changed as desired
> unique(data) % It can be seen that pixel values are no longer integers
>
> % This is where the problem lies. Why are the values almost, but not quite,
> % the same as the integer numbers found in the original atlas image?
>
> %% Try rounding the values and saving and re-reading the image:
>
> % Round to nearest integer:
> rnd_data = round(data);
> % Examine the result:
> unique(rnd_data) % It can be seen that the values are now integers
> % Save new data to file:
> V.fname = 'rounded.nii';
> spm_write_vol(V, rnd_data);
>
> % Examine the newly created file:
> V = spm_vol('rounded.nii');
> read_data = spm_read_vols(V);
> V.mat % The resolution is still what we expect
> unique(read_data) % The data are back to being not-quite-integers!
>
> % From this it is clear that the whole thing is not due to the resampling
> % or the expression used in imcalc, but rather something to do with writing
> % and reading the result to/from file.
>
>
>

-- 
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Dr. Michael Erb
Dept. Biomedical Magnetic Resonance
University Hospital Tübingen
Hoppe-Seyler-Str. 3
72076 Tübingen, Germany
Tel.: +49(0)7071/2987753    priv. +49(0)7071/61559
Fax.: +49(0)7071/294371
e-mail: <[log in to unmask]>
www: 
http://www.medizin.uni-tuebingen.de/Forschung/Kliniken/Radiologie/Biomedizinische+Magnetresonanz.html
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<