Home > bioelectromagnetism > avw_img_compose.m

avw_img_compose

PURPOSE ^

AVW_IMG_COMPOSE - Compose single slice Analyze files into a volume

SYNOPSIS ^

function avw = avw_img_compose(files,IMGorient,machine),

DESCRIPTION ^

 AVW_IMG_COMPOSE - Compose single slice Analyze files into a volume

 [ avw, machine ] = avw_img_compose(files, orient, machine)

 files  - a struct created with F = DIR('*.img').
          The filenames of the .img files are in F.name and
          these files are composed into a volume, in the 
          order given by the DIR command
 
 orient - force reading IMG in 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

          Note that composed volume is given in this orientation
 
 machine - a string, see machineformat in fread for details.
           The default here is 'ieee-le' but the routine
           will automatically switch between little and big
           endian to read any such Analyze header.  It
           reports the appropriate machine format and can
           return the machine value.

 Returned values:
 
 avw.hdr - a struct with image data parameters.
 avw.img - a 3D matrix of image data (double precision).
 
 See also: AVW_IMG_READ & AVW_HDR_READ (called by this function)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function avw = avw_img_compose(files,IMGorient,machine),
0002 
0003 % AVW_IMG_COMPOSE - Compose single slice Analyze files into a volume
0004 %
0005 % [ avw, machine ] = avw_img_compose(files, orient, machine)
0006 %
0007 % files  - a struct created with F = DIR('*.img').
0008 %          The filenames of the .img files are in F.name and
0009 %          these files are composed into a volume, in the
0010 %          order given by the DIR command
0011 %
0012 % orient - force reading IMG in specified orientation, integer values:
0013 %
0014 %          '', read header history orient field
0015 %          0,  transverse/axial unflipped
0016 %          1,  coronal unflipped
0017 %          2,  sagittal unflipped
0018 %          3,  transverse/axial flipped
0019 %          4,  coronal flipped
0020 %          5,  sagittal flipped
0021 %
0022 %          Note that composed volume is given in this orientation
0023 %
0024 % machine - a string, see machineformat in fread for details.
0025 %           The default here is 'ieee-le' but the routine
0026 %           will automatically switch between little and big
0027 %           endian to read any such Analyze header.  It
0028 %           reports the appropriate machine format and can
0029 %           return the machine value.
0030 %
0031 % Returned values:
0032 %
0033 % avw.hdr - a struct with image data parameters.
0034 % avw.img - a 3D matrix of image data (double precision).
0035 %
0036 % See also: AVW_IMG_READ & AVW_HDR_READ (called by this function)
0037 %
0038 
0039 % $Revision: 1.1 $ $Date: 2004/11/12 01:30:25 $
0040 
0041 % Licence:  GNU GPL, no express or implied warranties
0042 % History:  05/2002, Darren.Weber@flinders.edu.au
0043 %                    The Analyze format is copyright
0044 %                    (c) Copyright, 1986-1995
0045 %                    Biomedical Imaging Resource, Mayo Foundation
0046 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0047 
0048 if ~exist('files','var'),
0049     error('AVW_IMG_COMPOSE: No files to compose');
0050 end
0051 
0052 if ~exist('IMGorient','var'), IMGorient = ''; end
0053 if ~exist('machine','var'), machine = 'ieee-le'; end
0054 
0055 
0056 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0057 % Number of files (slices) to compose
0058 Nfiles = size(files,1);
0059 % Size of first file (slice) in bytes
0060 Fsize = files(1).bytes;
0061 
0062 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0063 % Initialise the composed volume
0064 
0065 % Read the first slice header
0066 firstslice = avw_hdr_read(files(1).name,machine);
0067 
0068 avw = firstslice;
0069 
0070 % Not sure if this is always the case, but individual
0071 % slice files output by http://xmedcon.sourceforge.net
0072 % have the FOV in .hdr.dime.pixdim(4) after reading
0073 % some Siemens .ima files.  It might be worth checking
0074 % at this point and dividing this FOV by the
0075 % number of slices
0076 if avw.hdr.dime.pixdim(4) > 20,
0077     msg = sprintf('AVW_IMG_COMPOSE: slice pixdim(4) is very large, assuming it is FOV and converting!\n');
0078     warning(msg);
0079     % OK this field is probably FOV, so lets
0080     % divide it by the total number of slices
0081     SliceFOV = double(avw.hdr.dime.pixdim(4));
0082     avw.hdr.dime.pixdim(4) = single(SliceFOV / Nfiles);
0083 end
0084 
0085 % Initialise avw.img and reset some header dimensions
0086 avw.hdr.dime.dim(4) = Nfiles;
0087 avw = avw_img_init(avw);
0088 
0089 % Now reset orient field of avw, as all data will
0090 % be in standard axial unflipped orientation after
0091 % avw_img_read.
0092 avw.hdr.hist.orient = char(0);
0093 
0094 
0095 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0096 % Add slices, depends on whether slice order is normal or flipped
0097 
0098 orient = double(firstslice.hdr.hist.orient);
0099 
0100 if orient < 3,
0101     % process slices as unflipped volume
0102     for f = 1:Nfiles,
0103         if files(f).bytes == Fsize,
0104             slice = avw_img_read(files(f).name,IMGorient,machine);
0105             avw   = avw_add_slice(avw,slice,f,orient);
0106         else
0107             msg = sprint('AVW_IMG_COMPOSE: This file is not the same size as the first file!');
0108             error(msg);
0109         end
0110     end
0111 else
0112     % process slices as flipped volume
0113     for f = Nfiles:-1:1,
0114         if files(f).bytes == Fsize,
0115             slice = avw_img_read(files(f).name,IMGorient,machine);
0116             avw   = avw_add_slice(avw,slice,f,orient);
0117         else
0118             msg = sprint('AVW_IMG_COMPOSE: This file is not the same size as the first file!');
0119             error(msg);
0120         end
0121     end
0122 end
0123 
0124 
0125 return
0126 
0127 
0128 
0129 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0130 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0131 function [avw] = avw_add_slice(avw,slice,Nslice,orient),
0132     
0133     % Check that composed volume and current slice
0134     % have the same orientation
0135     Vorient = double(  avw.hdr.hist.orient);
0136     Sorient = double(slice.hdr.hist.orient);
0137     
0138     if Vorient ~= Sorient,
0139         msg = sprintf('AVW_IMG_COMPOSE: Slice %3d has different orientation to volume',Nslice);
0140         error(msg);
0141     end
0142     
0143     switch double(orient),
0144     
0145     case 0, % axial unflipped
0146         
0147         % Slices in 'z' axis - from patient inferior to superior
0148         avw.img(:,:,Nslice) = slice.img;
0149         
0150     case 1, % coronal unflipped
0151         
0152         % Slices in 'y' axis - from patient posterior to anterior
0153         avw.img(:,Nslice,:) = slice.img;
0154         
0155     case 2, % sagittal unflipped
0156         
0157         % Slices in 'x' axis - from patient right to left
0158         avw.img(Nslice,:,:) = slice.img;
0159         
0160     case 3, % axial flipped
0161         
0162         % Slices in 'z' axis - from patient inferior to superior
0163         avw.img(:,:,Nslice) = slice.img;
0164         
0165     case 4, % coronal flipped
0166         
0167         % Slices in 'y' axis - from patient anterior to posterior
0168         avw.img(:,Nslice,:) = slice.img;
0169         
0170     case 5, % sagittal flipped
0171         
0172         % Slices in 'x' axis - from patient right to left
0173         avw.img(Nslice,:,:) = slice.img;
0174     end
0175     
0176 return
0177 
0178 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0179 function [avw] = avw_img_init(avw),
0180     
0181     PixelDim = double(avw.hdr.dime.dim(2));
0182     RowDim   = double(avw.hdr.dime.dim(3));
0183     SliceDim = double(avw.hdr.dime.dim(4));
0184     
0185     PixelSz  = double(avw.hdr.dime.pixdim(2));
0186     RowSz    = double(avw.hdr.dime.pixdim(3));
0187     SliceSz  = double(avw.hdr.dime.pixdim(4));
0188     
0189     switch double(avw.hdr.hist.orient),
0190     
0191     case {0,3}, % axial unflipped or flipped
0192         
0193         x = PixelDim;   Xsz = PixelSz;
0194         y = RowDim;     Ysz = RowSz;
0195         z = SliceDim;   Zsz = SliceSz;
0196         
0197     case {1,4}, % coronal unflipped or flipped
0198         
0199         x = PixelDim;   Xsz = PixelSz;
0200         y = SliceDim;   Ysz = SliceSz;
0201         z = RowDim;     Zsz = RowSz;
0202         
0203     case {2,5}, % sagittal unflipped or flipped
0204         
0205         x = SliceDim;   Xsz = SliceSz;
0206         y = PixelDim;   Ysz = PixelSz;
0207         z = RowDim;     Zsz = RowSz;
0208         
0209     otherwise, % assume axial unflipped or flipped
0210         
0211         msg = sprintf('AVW_IMG_COMPOSE: No specified orientation\n');
0212         warning(msg);
0213         
0214         x = PixelDim;   Xsz = PixelSz;
0215         y = RowDim;     Ysz = RowSz;
0216         z = SliceDim;   Zsz = SliceSz;
0217         
0218     end
0219     
0220     avw.img = zeros(x,y,z);
0221     avw.hdr.dime.dim(2) = x;
0222     avw.hdr.dime.dim(3) = y;
0223     avw.hdr.dime.dim(4) = z;
0224     
0225     avw.hdr.dime.pixdim(2) = Xsz;
0226     avw.hdr.dime.pixdim(3) = Ysz;
0227     avw.hdr.dime.pixdim(4) = Zsz;
0228     
0229 return

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