Home > rest_20090422 > rest_spm5_files > nic_spm_write_vol.m

nic_spm_write_vol

PURPOSE ^

Write an image volume to disk, setting scales and offsets as appropriate

SYNOPSIS ^

function V = nic_spm_write_vol(V,Y)

DESCRIPTION ^

 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.
_______________________________________________________________________
 Copyright (C) 2005 Wellcome Department of Imaging Neuroscienic_e

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

nic_spm_write_vol.m

SOURCE CODE ^

0001 function V = nic_spm_write_vol(V,Y)
0002 % Write an image volume to disk, setting scales and offsets as appropriate
0003 % FORMAT V = spm_write_vol(V,Y)
0004 % V (input)  - a structure containing image volume information (see spm_vol)
0005 % Y          - a one, two or three dimensional matrix containing the image voxels
0006 % V (output) - data structure after modification for writing.
0007 %_______________________________________________________________________
0008 % Copyright (C) 2005 Wellcome Department of Imaging Neuroscienic_e
0009 
0010 % John Ashburner
0011 % $Id: spm_write_vol.m 250 2005-10-07 16:08:39Z john $
0012 
0013 
0014 if ndims(Y)>3, error('Can only handle a maximum of 3 dimensions.'), end
0015 
0016 if ~isfield(V,'pinfo'), V.pinfo = [1,0,0]'; end
0017 
0018 dim = [size(Y) 1 1 1];
0019 if ~all(dim(1:3) == V.dim(1:3)) || (size(V.pinfo,2)~=1 && size(V.pinfo,2)~=dim(3)),
0020     error('Inic_ompatible dimensions.');
0021 end
0022 
0023 
0024 % Set scalefactors and offsets
0025 %-----------------------------------------------------------------------
0026 dt           = V.dt(1);
0027 s            = find(dt == [2 4 8 256 512 768]);
0028 dmnmx        = [0 -2^15 -2^31 -2^7 0 0 ; 2^8-1 2^15-1 2^31-1 2^7-1 2^16 2^32];
0029 dmnmx(1,:)   = 0; % assume an intercept of zero
0030 dmnmx        = dmnmx(:,s);
0031 
0032 V.pinfo(1,:) = 1;
0033 V.pinfo(2,:) = 0;
0034 mxs          = zeros(dim(3),1)+NaN;
0035 mns          = zeros(dim(3),1)+NaN;
0036 if ~isempty(s),
0037     for p=1:dim(3),
0038         tmp    = double(Y(:,:,p));
0039         tmp    = tmp(finite(tmp));
0040         if ~isempty(tmp),
0041             mxs(p) = max(tmp);
0042             mns(p) = min(tmp);
0043         end;
0044     end;
0045 
0046     if size(V.pinfo,2) ~= 1,
0047         for p=1:dim(3),
0048             mx = mxs(p);
0049             mn = mns(p);
0050             if ~finite(mx), mx = 0; end;
0051             if ~finite(mn), mn = 0; end;
0052             if mx~=mn,
0053                 V.pinfo(1,p) = (mx-mn)/(dmnmx(2)-dmnmx(1));
0054                 V.pinfo(2,p) = ...
0055                     (dmnmx(2)*mn-dmnmx(1)*mx)/(dmnmx(2)-dmnmx(1));
0056             else
0057                 V.pinfo(1,p) = 0;
0058                 V.pinfo(2,p) = mx;
0059             end;
0060         end;
0061     else
0062         mx = max(mxs(finite(mxs)));
0063         mn = min(mns(finite(mns)));
0064         if isempty(mx), mx = 0; end;
0065         if isempty(mn), mn = 0; end;
0066         if mx~=mn,
0067             V.pinfo(1,1) = (mx-mn)/(dmnmx(2)-dmnmx(1));
0068             V.pinfo(2,1) = (dmnmx(2)*mn-dmnmx(1)*mx)/(dmnmx(2)-dmnmx(1));
0069         else
0070             V.pinfo(1,1) = 0;
0071             V.pinfo(2,1) = mx;
0072         end;
0073     end;
0074 end;
0075 
0076 %-Create and write image
0077 %-----------------------------------------------------------------------
0078 V = nic_spm_create_vol(V);
0079 for p=1:V.dim(3),
0080     V = nic_spm_write_plane(V,Y(:,:,p),p);
0081 end;

Generated on Wed 29-Apr-2009 01:06:38 by m2html © 2005