ctf_write_mri.m

来自「含有多种ICA算法的eeglab工具箱」· M 代码 · 共 416 行 · 第 1/2 页

M
416
字号
function ctf_write_mri(mri, fileName, force)% ctf_write_mri - write a CTF .mri file%% ctf_write_mri(mri,fileName,force)%% mri is a data struct returned from ctf_read_mri.  It may contain% mri.file, in which case, you do not need the fileName input here.  If the% file exists, you are prompted for a new file name, unless force = 1.%% The CTF MRI File format used by MRIViewer consists of a binary file with% a 1,028 byte header. The MRI data can be in 8-bit (unsigned character) or% 16-bit (unsigned short integer) format and consists of 256 x 256 pixel% slices, stored as 256 contiguous sagittal slices from left to right (or% right to left if head orientation is left-on-right). Each slice is stored% as individual pixels starting at the left, anterior, superior% corner and scanning downwards row by row. Therefore the coronal% position is fastest changing, axial position second fastest% changing and sagittal position slowest changing value in the% file, always in the positive direction for each axis (see section% on Head Coordinate System for axis definitions). By default CTF% MRI files have the file extension .mri %%      <>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> %%      <                                                      > %  %      <                    DISCLAIMER:                       > %%      <                                                      > %%      < THIS PROGRAM IS INTENDED FOR RESEARCH PURPOSES ONLY. > %%      < THIS PROGRAM IS IN NO WAY INTENDED FOR CLINICAL OR   > %%      <                    OFFICIAL USE.                     > %%      <                                                      > %%      <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<> %%% $Revision: 1.6 $ $Date: 2004/07/18 06:10:17 $% Copyright (C) 2003  Darren L. Weber% % This program is free software; you can redistribute it and/or% modify it under the terms of the GNU General Public License% as published by the Free Software Foundation; either version 2% of the License, or (at your option) any later version.% % This program is distributed in the hope that it will be useful,% but WITHOUT ANY WARRANTY; without even the implied warranty of% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the% GNU General Public License for more details.% % You should have received a copy of the GNU General Public License% along with this program; if not, write to the Free Software% Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.% History:  08/2003, Darren.Weber_at_radiology.ucsf.edu%                    - adapted from an appendex to CTF document%                    MRIConverter.pdf, which is copied at the end of this%                    function.%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%ver = '[$Revision: 1.6 $]';fprintf('\nCTF_WRITE_MRI [v%s]\n',ver(12:16));  tic;if ~exist('mri','var'), error('no input mri data struct'); endif isempty(mri), error('empty input mri data struct'); endif ~exist('force','var'), force = 0; end % don't overwriteif isempty(force), force = 0; endif ~exist('fileName','var'),    if isfield(mri,'file'),        file = mri.file;    else        [fileName, filePath, filterIndex] = uigetfile('*.mri', 'Locate CTF .mri file');        file = fullfile(filePath, fileName);    endelse    [filePath, fileName, fileExt] = fileparts(fileName);    file = fullfile(filePath, [fileName,'.mri']);endif isempty(file),  error('...file is empty\n');endif exist(file,'file'),    if force,        fprintf('...file already exists, overwriting it.\n');    else        fprintf('...file already exists\n');        [fileName, pathName] = uiputfile('*.mri', 'Specify CTF .mri file to write');        file = fullfile(filePath, [fileName,'.mri']);    endend%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% open the file for writing%    'ieee-be.l64' or 's' - IEEE floating point with big-endian byte%                            ordering and 64 bit long data type.[fid,message] = fopen(file,'wb','s');if fid < 0, error('cannot open file for writing'); end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% write the file headerfprintf('...writing header ');Version_2_Header_write(fid, mri.hdr);%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% check header size, header should be 1028 bytesheader_bytes = ftell(fid);fprintf('(wrote %d bytes)\n',header_bytes);if header_bytes ~= 1028,  msg = sprintf('failed to write 1028 bytes into the header, wrote %d bytes',header_bytes);  error(msg);end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% seek beyond the header, to the beginning of the data matrix%fseek(fid,1028,'bof');%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% check if the data is 8 or 16 bitsswitch mri.hdr.dataSize,  case 1, % we have 8 bit data    fprintf('...writing 8 bit image data\n');    precision = 'uchar';  case 2, % we have 16 bit data    fprintf('...writing 16 bit image data\n');    precision = 'int16';  otherwise,    msg = sprintf('unknown mri.hdr.dataSize: %g',mri.hdr.dataSize);    error(msg);end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% now have the data array in a 3D matrix, we have to write it out in the% correct byte order% The CTF MRI File format used by MRIViewer consists of a binary file with a% 1,028 byte header. The MRI data can be in 8-bit (unsigned character) or 16-bit% (unsigned short integer) format and consists of 256 x 256 pixel slices, stored as% 256 contiguous sagittal slices from left to right (or right to left if head orientation% is "left-on-right"). Each slice is stored as individual pixels starting at the% top left corner and scanning downwards row by row. Therefore the coronal% position is fastest changing, axial position second fastest changing and sagittal% position slowest changing value in the file, always in the positive direction for% each axis (see section on Head Coordinate System for axis definitions). By% default CTF MRI files have the file extension ".mri"% MRIViewer uses these cardinal directions as axes in an internal coordinate system% where sagittal = X, coronal = Y and axial = Z forming an additional% right-handed coordinate system which is translated and rotated with respect to% the Head Coordinate System and has its origin at the upper left anterior corner% of the volume.PixelDim = 256;RowDim   = 256;SliceDim = 256;% imageOrientation, 0 = left on left, 1 = left on rightswitch mri.hdr.imageOrientation,  case 0,    fprintf('...sagittal slices are neurological orientation (left is on the left)\n');    fprintf('...+X left to right, +Y anterior to posterior, +Z superior to inferior\n');  case 1,    fprintf('...sagittal slices are radiological orientation (left is on the right)\n');    fprintf('...+X right to left, +Y anterior to posterior, +Z superior to inferior\n');  otherwise,    msg = sprintf('...unknown mri.hdr.imageOrientation: %d\n',mri.hdr.imageOrientation);    error(msg);end% output into sagittal slices, with the fastest moving index being Y,% from anterior to posterior, then Z, from superior to inferior, then X,% from left to right (or vice versa; depending on input mri struct).n = 1;y = 1:PixelDim; % +Y is from anterior to posteriorfor x = 1:SliceDim, % +X is from left to right (or vice versa)  for z = 1:RowDim, % +Z is from superior to inferior            count = fwrite(fid,mri.img(x,y,z),precision);            if count ~= PixelDim,          error('failed to output 256 data points');      end        endendfclose(fid);t=toc; fprintf('...done (%5.2f sec).\n\n',t);return%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?