Home > bioelectromagnetism > cor2avw.m

cor2avw

PURPOSE ^

cor2avw - Read Freesurfer MRI data (COR-001 to COR-256)

SYNOPSIS ^

function [ avw, machine ] = cor2avw(path,machine)

DESCRIPTION ^

 cor2avw - Read Freesurfer MRI data (COR-001 to COR-256)

 [ avw, machine ] = cor2avw(path, machine)

 path - the full path to the COR-??? image files.  If empty,
        this function uses uigetfile to locate COR-001.
 
 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.
 
 This function returns an Analyze data structure, with fields:
 
 avw.hdr - a struct with image data parameters.
 avw.img - a 3D matrix of image data (double precision).

 See also: avw2cor, avw_read, avw_img_read, avw_hdr_read

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [ avw, machine ] = cor2avw(path,machine)
0002 
0003 % cor2avw - Read Freesurfer MRI data (COR-001 to COR-256)
0004 %
0005 % [ avw, machine ] = cor2avw(path, machine)
0006 %
0007 % path - the full path to the COR-??? image files.  If empty,
0008 %        this function uses uigetfile to locate COR-001.
0009 %
0010 % machine - a string, see machineformat in fread for details.
0011 %           The default here is 'ieee-le' but the routine
0012 %           will automatically switch between little and big
0013 %           endian to read any such Analyze header.  It
0014 %           reports the appropriate machine format and can
0015 %           return the machine value.
0016 %
0017 % This function returns an Analyze data structure, with fields:
0018 %
0019 % avw.hdr - a struct with image data parameters.
0020 % avw.img - a 3D matrix of image data (double precision).
0021 %
0022 % See also: avw2cor, avw_read, avw_img_read, avw_hdr_read
0023 %
0024 
0025 % $Revision: 1.1 $ $Date: 2004/11/12 01:30:25 $
0026 
0027 % Licence:  GNU GPL, no express or implied warranties
0028 % History:  06/2002, Darren.Weber_at_radiology.ucsf.edu
0029 %
0030 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0031 
0032 
0033 % Create the file header, 256^3, 1 mm^3, uchar, etc
0034 avw = avw_hdr_make;
0035 
0036 avw.hdr.dime.dim    =  int16([4 256 256 256 1 0 0 0]);
0037 avw.hdr.dime.pixdim = single([0   1   1   1 0 0 0 0]);
0038 
0039 
0040 version = '[$Revision: 1.1 $]';
0041 fprintf('\ncor2avw [v%s]\n',version(12:16));
0042 
0043 if ~exist('path','var'),
0044     fprintf('No input path - see help cor2avw\n');
0045     [file, path] = uigetfile({'*.*'},'Select COR-001 File');
0046 end
0047 if ~exist('machine','var'), machine = 'ieee-le'; end
0048 
0049 [path,name,ext] = fileparts(strcat(path,filesep,'COR-001'));
0050 file = fullfile(path,[name ext]);
0051 fid = fopen(file,'r',machine);
0052 
0053 if fid < 0,
0054     fprintf('Cannot open file %s\n',file);
0055     fclose(fid);
0056 else
0057     fclose(fid);
0058     avw = read_image(avw,path,machine);
0059 end
0060 
0061 return
0062 
0063 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0064 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0065 function [ avw ] = read_image(avw,path,machine)
0066 
0067 tic;
0068 
0069 PixelDim = double(avw.hdr.dime.dim(2));
0070 RowDim   = double(avw.hdr.dime.dim(3));
0071 SliceDim = double(avw.hdr.dime.dim(4));
0072 
0073 % Allocate memory for the image
0074 avw.img = zeros(PixelDim,SliceDim,RowDim);
0075 
0076 fprintf('...reading ');
0077 
0078 % Read one file/slice at a time
0079 for y = 1:SliceDim,
0080     
0081     if y > 1,
0082         backspaces = repmat('\b',1,22);
0083     else
0084         backspaces = '';
0085     end
0086     file = sprintf('COR-%03d',y);
0087     fprintf([backspaces,'%s %s image.'],machine,file);        
0088     
0089     [path,name,ext] = fileparts(strcat(path,filesep,file));
0090     file = fullfile(path,[name ext]);
0091     
0092     % read the whole image into matlab (faster)
0093     fid = fopen(file,'r',machine); fseek(fid,0,'bof');
0094     tmp = fread(fid,inf,'uchar=>double');
0095     fclose(fid);
0096     
0097     % Arrange image into avw.img xyz matrix
0098     % For Freesurfer COR files (coronal) the voxels are stored with
0099     % Pixels in 'x' axis (varies fastest) - from patient right to left
0100     % Rows in   'z' axis                  - from patient superior to inferior
0101     % Slices in 'y' axis                  - from patient posterior to anterior
0102     
0103     n = 1;
0104     for z = RowDim:-1:1,
0105         x = 1:PixelDim;
0106         avw.img(x,y,z) = tmp(n:n+(PixelDim-1));
0107         n = n + PixelDim;
0108     end
0109 end
0110 
0111 avw.hdr.hist.orient = uint8(0);
0112 
0113 t=toc; fprintf('\n...done (%5.2f sec).\n',t);
0114 
0115 return

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