📄 ctf_write_mri.m
字号:
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.1 $ $Date: 2004/05/21 18:58:11 $
% Licence: GNU GPL, no implied or express warranties
% 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.1 $]';
fprintf('\nCTF_WRITE_MRI [v%s]\n',ver(12:16)); tic;
if ~exist('mri','var'), error('no input mri data struct'); end
if isempty(mri), error('empty input mri data struct'); end
if ~exist('force','var'), force = 0; end % don't overwrite
if isempty(force), force = 0; end
if ~exist('fileName','var'),
if isfield(mri,'file'),
file = mri.file;
else
[fileName, filePath, filterIndex] = uigetfile('*.mri', 'Locate CTF .mri file');
file = fullfile(filePath, fileName);
end
else
[filePath, fileName, fileExt] = fileparts(fileName);
file = fullfile(filePath, [fileName,'.mri']);
end
if isempty(file),
error('...file is empty\n');
end
if 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']);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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 header
fprintf('...writing header ');
Version_2_Header_write(fid, mri.hdr);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% check header size, header should be 1028 bytes
header_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 bits
switch 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 right
switch 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 posterior
for 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
end
end
fclose(fid);
t=toc; fprintf('...done (%5.2f sec).\n\n',t);
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Version_2_Header_write(fid,Version_2_Header),
identifierString = sprintf('%-32s', Version_2_Header.identifierString);
if length(identifierString) < 32,
paddingN = 32 - length(identifierString);
padding = char(repmat(double(' '),1,paddingN));
identifierString = [identifierString,padding];
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -