Home > bioelectromagnetism > siemens_ima_read.m

siemens_ima_read

PURPOSE ^

SIEMENS_IMA_READ - read Siemens .ima data file (*.ima)

SYNOPSIS ^

function [ ima, machine ] = siemens_ima_read(fileprefix,IMGorient)

DESCRIPTION ^

 SIEMENS_IMA_READ - read Siemens .ima data file (*.ima)
 
 [ ima, machine ] = siemens_ima_read(fileprefix, orient, machine)

 fileprefix - a string, the filename without the .ima extension
 
 orient - force reading of specified orientation, integer values:

          '', read header history orient field
          0,  transverse/axial unflipped
          1,  coronal unflipped
          2,  sagittal unflipped
          3,  transverse/axial flipped
          4,  coronal flipped
          5,  sagittal flipped
 
 machine - a string, see machineformat in fread for details.
           The default here is 'ieee-be' but the routine
           will automatically switch between little and big
           endian.  It reports the appropriate machine 
           format and can return the machine value.
 
 Returned values:
 
 ima.hdr - a struct with image data parameters.
 ima.img - a 3D matrix of image data (double precision).
 
 See also: IMA_HDR_READ (called by this function)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [ ima, machine ] = siemens_ima_read(fileprefix,IMGorient)
0002 
0003 % SIEMENS_IMA_READ - read Siemens .ima data file (*.ima)
0004 %
0005 % [ ima, machine ] = siemens_ima_read(fileprefix, orient, machine)
0006 %
0007 % fileprefix - a string, the filename without the .ima extension
0008 %
0009 % orient - force reading of specified orientation, integer values:
0010 %
0011 %          '', read header history orient field
0012 %          0,  transverse/axial unflipped
0013 %          1,  coronal unflipped
0014 %          2,  sagittal unflipped
0015 %          3,  transverse/axial flipped
0016 %          4,  coronal flipped
0017 %          5,  sagittal flipped
0018 %
0019 % machine - a string, see machineformat in fread for details.
0020 %           The default here is 'ieee-be' but the routine
0021 %           will automatically switch between little and big
0022 %           endian.  It reports the appropriate machine
0023 %           format and can return the machine value.
0024 %
0025 % Returned values:
0026 %
0027 % ima.hdr - a struct with image data parameters.
0028 % ima.img - a 3D matrix of image data (double precision).
0029 %
0030 % See also: IMA_HDR_READ (called by this function)
0031 %
0032 
0033 % $Revision: 1.1 $ $Date: 2004/11/12 01:32:35 $
0034 
0035 % Licence:  GNU GPL, no express or implied warranties
0036 % History:  10/2002, Darren.Weber@flinders.edu.au
0037 %
0038 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0039 
0040 
0041 fprintf('\n\nUnder development - not functional yet...sorry.\n\n');
0042 return
0043 
0044 
0045 if ~exist('fileprefix','var'),
0046     fprintf('SIEMENS_IMA_READ: No input fileprefix - see help siemens_ima_read\n');
0047     return;
0048 end
0049 if ~exist('IMGorient','var'), IMGorient = ''; end
0050 if ~exist('machine','var'), machine = 'ieee-le'; end
0051 
0052 
0053 % MAIN
0054 
0055 fid = fopen(sprintf('%s.ima',fileprefix),'r',machine);
0056 if fid < 0,
0057     msg = sprintf('Cannot open file %s.ima\n',fileprefix);
0058     error(msg);
0059 else
0060     ima = read_image(fid,fileprefix,IMGorient,machine);
0061     ima.fileprefix = fileprefix;
0062 end
0063 
0064 return
0065 
0066 
0067 
0068 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0069 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0070 function [ ima ] = read_image(fid,fileprefix,IMGorient,machine)
0071     
0072     % Read the file header
0073     [ ima, machine ] = ima_hdr_read(fileprefix,machine);
0074     
0075     if ~isequal(ima.hdr.hk.sizeof_hdr,6144),
0076         fprintf('SIEMENS_IMA_READ: Failed reading %s Analyze image\n',fileprefix);
0077         ima.img = [];
0078         fclose(fid);
0079         return
0080     end
0081     
0082     
0083     % read the whole .img file into matlab (faster)
0084     fprintf('SIEMENS_IMA_READ: Reading Siemens image data.\n');
0085     fseek(fid,6144,'bof');
0086     precision = 'double';
0087     tmp = fread(fid,inf,sprintf('%s=>double',precision));
0088     fclose(fid);
0089     
0090     % Now partition the img data into xyz
0091     
0092     
0093     
0094     PixelDim = double(ima.hdr.dime.dim(2));
0095     RowDim   = double(ima.hdr.dime.dim(3));
0096     SliceDim = double(ima.hdr.dime.dim(4));
0097     
0098     
0099     
0100     if ~isempty(IMGorient), ima.hdr.hist.orient = IMGorient; end,
0101     
0102     
0103     switch double(ima.hdr.hist.orient),
0104     
0105     case 0, % transverse/axial unflipped
0106         
0107         % For the 'transverse unflipped' type, the voxels are stored with
0108         % Pixels in 'x' axis (varies fastest) - from patient right to left
0109         % Rows in   'y' axis                  - from patient posterior to anterior
0110         % Slices in 'z' axis                  - from patient inferior to superior
0111         
0112         fprintf('SIEMENS_IMA_READ: Image orient appears to be axial unflipped\n');
0113         
0114         ima.img = zeros(PixelDim,RowDim,SliceDim);
0115         
0116         n = 1;
0117         for z = 1:SliceDim,
0118             for y = 1:RowDim,
0119                 x = 1:PixelDim;
0120                 ima.img(x,y,z) = tmp(n:n+(PixelDim-1));
0121                 n = n + PixelDim;
0122             end
0123         end
0124         
0125     end
0126 return
0127 
0128 
0129 function [ ima, machine ] = ima_hdr_read(fileprefix,machine)
0130 
0131 
0132 
0133 % Siemens Magnetom Vision Native Format
0134 %
0135 % The exact details of the format are not known, but
0136 % a little guess work has determined what follows. The
0137 % data types are Sun, hence the byte order is big-endian
0138 % and the all the floats I have found are doubles. Offsets
0139 % here are in bytes from the start of the header. The
0140 % uncompressed image data starts at offset 6144.
0141 %
0142 %         0         u_int      SiemensStudyDateYYYY
0143 %         4         u_int      SiemensStudyDateMM
0144 %         8         u_int      SiemensStudyDateDD
0145 %         12        u_int      AcquisitionDateYYYY
0146 %         16        u_int      AcquisitionDateMM
0147 %         20        u_int      AcquisitionDateDD
0148 %         24        u_int      ImageDateYYYY
0149 %         28        u_int      ImageDateMM
0150 %         32        u_int      ImageDateDD
0151 %         36        u_int      SiemensStudyTimeHH
0152 %         40        u_int      SiemensStudyTimeMM
0153 %         44        u_int      SiemensStudyTimeSS
0154 %         52        u_int      AcquisitionTimeHH
0155 %         56        u_int      AcquisitionTimeMM
0156 %         60        u_int      AcquisitionTimeSS
0157 %         68        u_int      ImageTimeHH
0158 %         72        u_int      ImageTimeMM
0159 %         76        u_int      ImageTimeSS
0160 %         96        char[7]    Manufacturer
0161 %         105       char[25]   InstitutionName
0162 %         186       char[4]    Annotation
0163 %         281       char[15]   ModelName
0164 %         412       u_int      LastMoveDateYYYY
0165 %         416       u_int      LastMoveDateMM
0166 %         420       u_int      LastMoveDateDD
0167 %         424       u_int      LastMoveTimeHH
0168 %         428       u_int      LastMoveTimeMM
0169 %         432       u_int      LastMoveTimeSS
0170 %         768       char[25]   PatientName
0171 %         795       char[12]   PatientID
0172 %         808       u_int      DOBYYYY
0173 %         812       u_int      DOBMM
0174 %         816       u_int      DOBDD
0175 %         851       char[3]    PatientAge
0176 %         854       char       PatientAgeUnits      ('Y'=years)
0177 %         1052      u_int      RegistrationDateYYYY
0178 %         1056      u_int      RegistrationDateMM
0179 %         1060      u_int      RegistrationDateDD
0180 %         1064      u_int      RegistrationTimeHH
0181 %         1068      u_int      RegistrationTimeMM
0182 %         1072      u_int      RegistrationTimeSS
0183 %         1544      double     SliceThickness
0184 %         1560      double     RepetitionTime
0185 %         1568      double     EchoTime
0186 %         1592      double     FrequencyMHz
0187 %         1639      char[5]    Station
0188 %         1712      u_int      CalibrationDateYYYY
0189 %         1716      u_int      CalibrationDateMM
0190 %         1720      u_int      CalibrationDateDD
0191 %         1724      u_int      CalibrationTimeHH
0192 %         1728      u_int      CalibrationTimeMM
0193 %         1732      u_int      CalibrationTimeSS
0194 %         1767      char[16]   ReceivingCoil
0195 %         1828      char[4]    ImagedNucleus
0196 %         2112      double     FlipAngle
0197 %         2560      double     MagneticFieldStrength
0198 %         2864      u_int      DisplayMatrixSize
0199 %         2944      char[65]   SequencePrgName
0200 %         3009      char[65]   SequenceWkcName
0201 %         3074      char[9]    SequenceAuthor
0202 %         3083      char[8]    SequenceType
0203 %         3744      double     FOVRow
0204 %         3752      double     FOVColumn
0205 %         3768      double     CenterPointX
0206 %         3776      double     CenterPointY
0207 %         3784      double     CenterPointZ
0208 %         3792      double     NormalVectorX
0209 %         3800      double     NormalVectorY
0210 %         3808      double     NormalVectorZ
0211 %         3816      double     DistanceFromIsocenter
0212 %         3832      double     RowVectorX
0213 %         3840      double     RowVectorY
0214 %         3848      double     RowVectorZ
0215 %         3856      double     ColumnVectorX
0216 %         3864      double     ColumnVectorY
0217 %         3872      double     ColumnVectorZ
0218 %         3880      char[3]    OrientationSet1Top
0219 %         3884      char[3]    OrientationSet1Left
0220 %         3888      char[3]    OrientationSet1Back
0221 %         3892      char[3]    OrientationSet2Down
0222 %         3896      char[3]    OrientationSet2Right
0223 %         3900      char[3]    OrientationSet2Front
0224 %         3904      char[32]   SequenceName
0225 %
0226 %         4136      double     GapRatio (see below)
0227 %
0228 %         5000      double     PixelSizeRow
0229 %         5008      double     PixelSizeColumn
0230 %
0231 %         5504      char[12]   TextPatientID
0232 %         5517      char       TextPatientSex
0233 %         5518      char[3]    TextPatientAge
0234 %         5521      char       TextPatientAgeUnits       ('Y'=years)
0235 %         5529      char[7]    TextPatientPosition
0236 %         5541      char[5]    TextImageNumberFlag       ('IMAGE'=image)
0237 %         5546      char[3]    TextImageNumber
0238 %         5559      char[2]    TextDateDD
0239 %         5562      char[3]    TextDateMM
0240 %         5566      char[4]    TextDateYYYY
0241 %         5571      char[2]    TextTimeHH
0242 %         5574      char[2]    TextTimeMM
0243 %         5577      char[2]    TextAcquisitionTimeFlag   ('TA'=acquisition time)
0244 %         5583      char[2]    TextAcquisitionTimeMM
0245 %         5586      char[2]    TextAcquisitionTimeSS
0246 %         5601      char[4]    TextAnnotation
0247 %         5655      char[25]   TextOrganization
0248 %         5682      char[5]    TextStation
0249 %         5695      char[3]    TextAcquisitionMatrixPhase
0250 %         5698      char       TextAcquisitionMatrixPhaseAxis  ('h'=horizontal,' '=vertical)
0251 %         5700      char[3]    TextAcquisitionMatrixFreq
0252 %         5703      char       TextAcquisitionMatrixFreqO      ('o'=o,' '=blank)
0253 %         5704      char       TextAcquisitionMatrixFreqS      ('s'=s,' '=blank)
0254 %         5706      char[8]    TextSequence
0255 %         5714      char[3]    TextFlipAngle
0256 %         5718      char[4]    TextScanNumberFlag        ('SCAN'=scan)
0257 %         5723      char[3]    TextScanNumberA
0258 %         5726      char[3]    TextScanNumberB
0259 %         5730      char[2]    TextRepetitionTimeFlag    ('TR'=tr)
0260 %         5734      char[7]    TextRepetitionTime
0261 %         5742      char[2]    TextEchoTimeFlag          ('TE'=te)
0262 %         5746      char[5]    TextEchoTime
0263 %         5752      char       TextEchoNumber
0264 %         5790      char[2]    TextSliceThicknessFlag    ('SL'=slice thickness)
0265 %         5794      char[7]    TextSliceThickness
0266 %         5802      char[2]    TextSlicePositionFlag     ('SP'=slice position)
0267 %         5806      char[7]    TextSlicePosition
0268 %         5814      char[3]    TextAngleFlag1            ('Sag'=sagittal,'Cor'=coronal,'Tra'=transverse)
0269 %         5817      char       TextAngleFlag2            ('>'=gt,'<'=lt)
0270 %         5818      char[3]    TextAngleFlag3            ('Sag'=sagittal,'Cor'=coronal,'Tra'=transverse)
0271 %         5821      char[4]    TextAngle
0272 %         5838      char[3]    TextFOVFlag               ('FoV'=field of view)
0273 %         5842      char[3]    TextFOVH
0274 %         5846      char[3]    TextFOVV
0275 %         5874      char[2]    TextTablePositionFlag     ('TP'=table position)
0276 %         5878      char[7]    TextTablePosition
0277 %         5938      char[5]    TextStudyNumberFlag       ('STUDY'=study)
0278 %         5943      char[2]    TextStudyNumber
0279 %         5956      char[2]    TextDOBDD
0280 %         5959      char[3]    TextDOBMM
0281 %         5963      char[4]    TextDOBYYYY
0282 %         5992      char[3]    TextStudyNumberFlag2      ('STU'=study)
0283 %         5996      char[3]    TextImageNumberFlag2      ('IMA'=study)
0284 %         5999      char[2]    TextStudyNumber2
0285 %         6002      char[2]    TextImageNumber2
0286 %         6013      char[5]    TextStudyImageNumber3
0287 %         6031      char[15]   TextModelName
0288 %         6058      char[25]   TextPatientName
0289 %         6085      char[2]    TextScanStartTimeHH
0290 %         6088      char[2]    TextScanStartTimeMM
0291 %         6091      char[2]    TextScanStartTimeSS
0292 
0293 
0294 % I have asked David Clunie to update his FAQ to include information for
0295 % finding size of the gap given a Siemens Vision format image. For the record
0296 % the answer is:  GapRatio is at byte 4136 (measured from the beginning of
0297 % the file) and it is of type double (8-byte float, Big Endian). For example,
0298 % if SliceThickness is 8mm and the GapRatio is 0.25, then the Gap is
0299 % (8x0.25=) 2mm, and the interslice distance is (8+2=) 10mm.
0300 
0301 
0302 return

Generated on Mon 15-Aug-2005 15:36:19 by m2html © 2003