Home > bioelectromagnetism > cor_img_read.m

cor_img_read

PURPOSE ^

COR_IMG_READ - Read Freesurfer format data (COR-001 to COR-256)

SYNOPSIS ^

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

DESCRIPTION ^

 COR_IMG_READ - Read Freesurfer format data (COR-001 to COR-256)

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

 path - the full path to the COR-??? image files.  If empty,
        this function uses uigetfile to locate COR-???.
 
 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: AVW_IMAGE_READ, AVW_HEADER_READ

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [ avw, machine ] = cor_img_read(path,machine)
0002 
0003 % COR_IMG_READ - Read Freesurfer format data (COR-001 to COR-256)
0004 %
0005 % [ avw, machine ] = cor_img_read(path, machine)
0006 %
0007 % path - the full path to the COR-??? image files.  If empty,
0008 %        this function uses uigetfile to locate COR-???.
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: AVW_IMAGE_READ, AVW_HEADER_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@flinders.edu.au
0029 %
0030 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0031 
0032 
0033 if ~exist('path','var'),
0034     fprintf('No input path - see help cor_img_read\n');
0035     [file, path] = uigetfile({'*.*'},'Select COR-001 File');
0036 end
0037 if ~exist('machine','var'), machine = 'ieee-le'; end
0038 
0039 [path,name,ext] = fileparts(strcat(path,filesep,'COR-001'));
0040 file = fullfile(path,[name ext]);
0041 fid = fopen(file,'r',machine);
0042 
0043 if fid < 0,
0044     fprintf('Cannot open file %s\n',file);
0045     fclose(fid);
0046 else
0047     fclose(fid);
0048     avw = read_image(path,machine);
0049 end
0050 
0051 return
0052 
0053 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0054 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0055 function [ avw ] = read_image(path,machine)
0056     
0057     
0058     % Create the file header, 256^3, 1 mm^3, uchar, etc
0059     avw = avw_hdr_make;
0060     
0061     avw.hdr.dime.dim    =  int16([4 256 256 256 1 0 0 0]);
0062     avw.hdr.dime.pixdim = single([0   1   1   1 0 0 0 0]);
0063     
0064     PixelDim = double(avw.hdr.dime.dim(2));
0065     RowDim   = double(avw.hdr.dime.dim(3));
0066     SliceDim = double(avw.hdr.dime.dim(4));
0067     
0068     % Allocate memory for the image
0069     avw.img = zeros(PixelDim,SliceDim,RowDim);
0070     
0071     % Read one file/slice at a time
0072     for y = 1:SliceDim,
0073         
0074         file = sprintf('COR-%03d',y);
0075         fprintf('COR_IMAGE_READ:  Reading %s %s image.\n',machine,file);
0076         
0077         [path,name,ext] = fileparts(strcat(path,filesep,file));
0078         file = fullfile(path,[name ext]);
0079         
0080         % read the whole image into matlab (faster)
0081         fid = fopen(file,'r',machine); fseek(fid,0,'bof');
0082         tmp = fread(fid,inf,'uchar=>double');
0083         fclose(fid);
0084         
0085         % Arrange image into avw.img xyz matrix
0086         % For Freesurfer COR files the voxels are stored with
0087         % Pixels in 'x' axis (varies fastest) - from patient right to left
0088         % Rows in   'z' axis                  - from patient superior to inferior
0089         % Slices in 'y' axis                  - from patient posterior to anterior
0090         
0091         n = 1;
0092         for z = RowDim:-1:1,
0093             x = 1:PixelDim;
0094             avw.img(x,y,z) = tmp(n:n+(PixelDim-1));
0095             n = n + PixelDim;
0096         end
0097     end
0098     
0099     
0100     avw.hdr.hist.orient = 0;
0101     
0102 return

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