Home > rest_20090422 > rest_WriteNiftiImage.m

rest_WriteNiftiImage

PURPOSE ^

Write file(NIFTI, ...) for REST by CHEN Gui-Wen and YAN Chao-Gan

SYNOPSIS ^

function [flag] = rest_WriteNiftiImage(Data,Head,imageOUT)

DESCRIPTION ^

 Write file(NIFTI, ...) for REST by CHEN Gui-Wen and YAN Chao-Gan
 %------------------------------------------------------------------------
 Write data (Data) with a specified header (Head) into a image file with format 
 of Nifti 1.1. The data (Data) should be 3D matrix, the header (Head) should 
 be a structure the same as SPM5. If the filename (imageOUT) is with 
 extra name as '.img', then it will generate two files (header and
 data seperately), or else, '.nii', it will generate single file with header 
 and data together.

 Usage: [flag] = rest_WriteNiftiImage(Data,Head,imageOUT)

 Input:
 1. Data -  Data of 3D matrix to write
 2. Head - a structure containing image volume information, the structure
    is the same with a structure have read
    The elements in the structure are:
       Head.fname - the filename of the image. If the filename is not set, 
                    just use the parameter.
       Head.vox - A 1x3 array. It is the size of a voxel. (Must)
       Head.origin - A 1x3 array. It is the origin of coordinate. (Must)
       Head.dim   - the x, y and z dimensions of the volume
       Head.dt    - A 1x2 array.  First element is datatype (see spm_type).
                 The second is 1 or 0 depending on the endian-ness.
       Head.mat   - a 4x4 affine transformation matrix mapping from
                 voxel coordinates to real world coordinates.
       Head.pinfo - plane info for each plane of the volume.
              Head.pinfo(1,:) - scale for each plane
              Head.pinfo(2,:) - offset for each plane
                 The true voxel intensities of the jth image are given
                 by: val*Head.pinfo(1,j) + Head.pinfo(2,j)
              Head.pinfo(3,:) - offset into image (in bytes).
                 If the size of pinfo is 3x1, then the volume is assumed
                 to be contiguous and each plane has the same scalefactor
                 and offset.
              The scale and intercept will be changed according to the
              data to write
 3. imageOUT - the path and filename of image file to output [path\*.img or *.nii]
 Output:
 1. flag - a flag for all done, 1: successful, 0: fail
 %------------------------------------------------------------------------
 Copyright (C) 2007 Neuroimage Computing Group, State Key Laboratory of
 Cognitive Neuroscience and Learning

 Guiwen Chen, gwenchill@gmail.com
 @(#)rest_WriteNiftiImage.m  ver 2.0, 07/11/21
 %------------------------------------------------------------------------
 Revised by YAN Chao-Gan 080621
 ycg.yan@gmail.com

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

rest_WriteNiftiImage.m

SOURCE CODE ^

0001 function [flag] = rest_WriteNiftiImage(Data,Head,imageOUT)
0002 % Write file(NIFTI, ...) for REST by CHEN Gui-Wen and YAN Chao-Gan
0003 % %------------------------------------------------------------------------
0004 % Write data (Data) with a specified header (Head) into a image file with format
0005 % of Nifti 1.1. The data (Data) should be 3D matrix, the header (Head) should
0006 % be a structure the same as SPM5. If the filename (imageOUT) is with
0007 % extra name as '.img', then it will generate two files (header and
0008 % data seperately), or else, '.nii', it will generate single file with header
0009 % and data together.
0010 %
0011 % Usage: [flag] = rest_WriteNiftiImage(Data,Head,imageOUT)
0012 %
0013 % Input:
0014 % 1. Data -  Data of 3D matrix to write
0015 % 2. Head - a structure containing image volume information, the structure
0016 %    is the same with a structure have read
0017 %    The elements in the structure are:
0018 %       Head.fname - the filename of the image. If the filename is not set,
0019 %                    just use the parameter.
0020 %       Head.vox - A 1x3 array. It is the size of a voxel. (Must)
0021 %       Head.origin - A 1x3 array. It is the origin of coordinate. (Must)
0022 %       Head.dim   - the x, y and z dimensions of the volume
0023 %       Head.dt    - A 1x2 array.  First element is datatype (see spm_type).
0024 %                 The second is 1 or 0 depending on the endian-ness.
0025 %       Head.mat   - a 4x4 affine transformation matrix mapping from
0026 %                 voxel coordinates to real world coordinates.
0027 %       Head.pinfo - plane info for each plane of the volume.
0028 %              Head.pinfo(1,:) - scale for each plane
0029 %              Head.pinfo(2,:) - offset for each plane
0030 %                 The true voxel intensities of the jth image are given
0031 %                 by: val*Head.pinfo(1,j) + Head.pinfo(2,j)
0032 %              Head.pinfo(3,:) - offset into image (in bytes).
0033 %                 If the size of pinfo is 3x1, then the volume is assumed
0034 %                 to be contiguous and each plane has the same scalefactor
0035 %                 and offset.
0036 %              The scale and intercept will be changed according to the
0037 %              data to write
0038 % 3. imageOUT - the path and filename of image file to output [path\*.img or *.nii]
0039 % Output:
0040 % 1. flag - a flag for all done, 1: successful, 0: fail
0041 % %------------------------------------------------------------------------
0042 % Copyright (C) 2007 Neuroimage Computing Group, State Key Laboratory of
0043 % Cognitive Neuroscience and Learning
0044 %
0045 % Guiwen Chen, gwenchill@gmail.com
0046 % @(#)rest_WriteNiftiImage.m  ver 2.0, 07/11/21
0047 % %------------------------------------------------------------------------
0048 % Revised by YAN Chao-Gan 080621
0049 % ycg.yan@gmail.com
0050 
0051 % get the SPM path to use
0052 FilePath = which('rest.m');
0053 [giftPath, fileN, extn] = fileparts(FilePath);
0054 spmPath = fullfile(giftPath, 'rest_spm5_files');
0055 oldDir = pwd;
0056 addpath(spmPath);
0057 
0058 try
0059 %     cd(spmPath);
0060     [pth,nam,ext] = fileparts(imageOUT);  %%Added by YAN Chao-Gan
0061     if isempty(ext)
0062         imageOUT=[imageOUT,'.img'];
0063     end
0064 
0065 
0066     V=Head;
0067     
0068     %construct the file name and path
0069     if(~exist('imageOUT','var'))
0070       if(~isfield(Head,'fname')),
0071         fName=Head.fname;
0072       end
0073     else
0074       fName=imageOUT;
0075     end
0076     %fName=imgPath;
0077     if isempty(fileparts(fName))
0078         fName = fullfile(oldDir, fName);
0079     end
0080      V.fname=fName;
0081 
0082     nic_spm_write_vol(V, Data);
0083 
0084     clear V;    
0085     flag=1;% Successful
0086     rmpath(spmPath);
0087 %     cd(oldDir);
0088 catch
0089    rmpath(spmPath);
0090    flag=0;% fail
0091 %    cd(oldDir);
0092    error('Meet error while writing the data');
0093 end
0094

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