matRad_writeMHA

Purpose ^

matRad function to write mha files

Synopsis ^

function matRad_writeMHA(filepath,cube,metadata)

Description ^

 matRad function to write mha files
 
 call
   matRad_writeMHA(filepath,cube,metadata)

 input
   filepath:   full filename (with extension)
   cube:       3D array to be written into file
   metadata:   struct of metadata. Writer will wrap the existing metadata 
               to MHA standard-specific fields 
               Necessary fieldnames are:
               - resolution: [x y z]
               - datatype: numeric MATLAB-Datatype

 output
   file will be written to disk

 References
   https://itk.org/Wiki/MetaIO/Documentation

 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 Copyright 2015 the matRad development team. 
 
 This file is part of the matRad project. It is subject to the license 
 terms in the LICENSE file found in the top-level directory of this 
 distribution and at https://github.com/e0404/matRad/LICENSES.txt. No part 
 of the matRad project, including this file, may be copied, modified, 
 propagated, or distributed except according to the terms contained in the 
 LICENSE file.

 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Cross-reference information ^

This function calls: This function is called by:

Subfunctions ^

Source code ^

0001 function matRad_writeMHA(filepath,cube,metadata)
0002 % matRad function to write mha files
0003 %
0004 % call
0005 %   matRad_writeMHA(filepath,cube,metadata)
0006 %
0007 % input
0008 %   filepath:   full filename (with extension)
0009 %   cube:       3D array to be written into file
0010 %   metadata:   struct of metadata. Writer will wrap the existing metadata
0011 %               to MHA standard-specific fields
0012 %               Necessary fieldnames are:
0013 %               - resolution: [x y z]
0014 %               - datatype: numeric MATLAB-Datatype
0015 %
0016 % output
0017 %   file will be written to disk
0018 %
0019 % References
0020 %   https://itk.org/Wiki/MetaIO/Documentation
0021 %
0022 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0023 %
0024 % Copyright 2015 the matRad development team.
0025 %
0026 % This file is part of the matRad project. It is subject to the license
0027 % terms in the LICENSE file found in the top-level directory of this
0028 % distribution and at https://github.com/e0404/matRad/LICENSES.txt. No part
0029 % of the matRad project, including this file, may be copied, modified,
0030 % propagated, or distributed except according to the terms contained in the
0031 % LICENSE file.
0032 %
0033 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0034 
0035 %Sanity checks and restrictions
0036 dimensions = size(cube);
0037 if numel(dimensions) ~= 3
0038     error('Sorry! matRad only supports 3-dimensional MHA output');
0039 end
0040 
0041 fid = fopen(filepath, 'wb');
0042 if fid <= 0
0043     error('Could not open MHA destination file!');
0044 end
0045 cleaner = onCleanup(@() fclose(fid));
0046 
0047 %We perform the permutation
0048 if isfield(metadata,'axisPermutation')
0049     cube = permute(cube,metadata.axisPermutation);
0050 end
0051 %The transformation matrix is now the unit matrix
0052 transformMatrix = diag(ones(1,numel(dimensions)));
0053 tmString = sprintf(' %d',transformMatrix(:));
0054 
0055 %Determine the endian
0056 [~,~,endian] = computer;
0057 switch endian
0058     case 'L'
0059         byteOrderMSB = 'True';
0060     case 'B'
0061         byteOrderMSB = 'False';
0062     otherwise
0063         error('Unknown endian!');
0064 end
0065 
0066 fprintf(fid, 'ObjectType = Image\n');
0067 fprintf(fid, 'NDims = %d\n',numel(dimensions));
0068 fprintf(fid, 'BinaryData = True\n');
0069 fprintf(fid, 'BinaryDataByteOrderMSB = %s\n',byteOrderMSB); %Not sure about this field
0070 fprintf(fid, 'ElementByteOrderMSB = %s\n',byteOrderMSB); %Not sure about this field
0071 fprintf(fid, 'TransformMatrix =%s\n',tmString);
0072 fprintf(fid, 'Offset = %f %f %f\n',metadata.imageOrigin(1),metadata.imageOrigin(2),metadata.imageOrigin(3));
0073 fprintf(fid, 'AnatomicalOrientation = RAI\n'); %Did not double check this line
0074 fprintf(fid, 'ElementSpacing = %f %f %f\n',metadata.resolution(1),metadata.resolution(2),metadata.resolution(3));
0075 fprintf(fid, 'DimSize = %d %d %d\n',dimensions(1),dimensions(2),dimensions(3));
0076 fprintf(fid, 'ElementType = %s\n',matlabTypeToMHAtype(metadata.datatype));
0077 fprintf(fid, 'ElementDataFile = LOCAL\n');
0078 fwrite(fid,cube,metadata.datatype,'b');
0079 fclose(fid);
0080 
0081 end
0082 
0083 function newType = matlabTypeToMHAtype(datatype)
0084 switch datatype
0085     case {'single','float'}
0086         newType = 'MET_FLOAT';     
0087     case 'double'
0088         newType = 'MET_DOUBLE';
0089     case {'uchar','uint8'}
0090        newType = 'MET_UCHAR';
0091     case {'logical','int8','char'}
0092         newType = 'MET_CHAR';
0093     case 'int16'
0094         newType = 'MET_SHORT';
0095     case 'uint16'
0096         newType = 'MET_USHORT';
0097     case 'int32'
0098         newType = 'MET_INT';
0099     case 'int64'
0100         newType = 'MET_LONG';
0101     case 'uint32'
0102         newType = 'MET_UINT';
0103     case 'uint64'
0104         newType = 'MET_ULONG';
0105    otherwise
0106        error(['Datatype ' datatype ' not supported by MHA exporter!']);
0107 end
0108 end
0109 
0110

| Generated by m2html © 2005