Home > bioelectromagnetism > convert_mri2brainstorm.m

convert_mri2brainstorm

PURPOSE ^

Script to convert freesurfer analyze files into brainstorm format

SYNOPSIS ^

This is a script file.

DESCRIPTION ^

 Script to convert freesurfer analyze files into brainstorm format
 The Freesurfer analyze files were created using 
 mri_convert -oid 1 0 0 -ojd 0 1 0 -okd 0 0 1 orig subject_orig_axial_ras.img
 to create an axial volume in with neurological orientation,
 ie, +x is Right, +y is Anterior, +z is Superior (RAS).

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 
0002 % Script to convert freesurfer analyze files into brainstorm format
0003 % The Freesurfer analyze files were created using
0004 % mri_convert -oid 1 0 0 -ojd 0 1 0 -okd 0 0 1 orig subject_orig_axial_ras.img
0005 % to create an axial volume in with neurological orientation,
0006 % ie, +x is Right, +y is Anterior, +z is Superior (RAS).
0007 
0008 clear all
0009 
0010 coregister = 0;
0011 elecplot = 0;
0012 
0013 
0014 % Fiducial points in RAS volume, obtained using avw_view (in meters)
0015 %
0016 %                  Nasion                 Right                Left
0017 %
0018 mriFID.sub{ 1} = 'c01';
0019 mriFID.xyz{ 1} = [  0.005  0.097 -0.022;  0.071  0.023 -0.052; -0.072  0.016 -0.050 ];
0020 mriFID.sub{ 2} = 'c02';
0021 mriFID.xyz{ 2} = [  0.007  0.088 -0.007;  0.081  0.015 -0.046; -0.070  0.012 -0.051 ];
0022 mriFID.sub{ 3} = 'c03';
0023 mriFID.xyz{ 3} = [ -0.002  0.098 -0.008;  0.055  0.040 -0.038; -0.095  0.030 -0.046 ];
0024 mriFID.sub{ 4} = 'c04';
0025 mriFID.xyz{ 4} = [  0.000  0.092  0.000;  0.070  0.024 -0.036; -0.076  0.031 -0.036 ];
0026 mriFID.sub{ 5} = 'c05';
0027 mriFID.xyz{ 5} = [  0.010  0.094 -0.010;  0.075  0.024 -0.048; -0.072  0.024 -0.052 ];
0028 mriFID.sub{ 6} = 'c06';
0029 mriFID.xyz{ 6} = [ -0.002  0.088  0.000;  0.077  0.008 -0.048; -0.075 -0.008 -0.048 ];
0030 mriFID.sub{ 7} = 'c07';
0031 mriFID.xyz{ 7} = [ -0.001  0.097 -0.018;  0.078  0.008 -0.055; -0.076  0.008 -0.055 ];
0032 mriFID.sub{ 8} = 'c08';
0033 mriFID.xyz{ 8} = [  0.000  0.096 -0.019;  0.087  0.005 -0.039; -0.065  0.004 -0.038 ];
0034 mriFID.sub{ 9} = 'c09';
0035 mriFID.xyz{ 9} = [  0.010  0.086 -0.020;  0.100 -0.020 -0.080; -0.060  0.020 -0.058 ];
0036 mriFID.sub{10} = 'c10';
0037 mriFID.xyz{10} = [  0.000  0.091 -0.018;  0.076  0.011 -0.052; -0.068  0.006 -0.052 ];
0038 
0039 mriFID.sub{11} = 'p02';
0040 mriFID.xyz{11} = [  0.002  0.091  0.016;  0.080  0.025 -0.036; -0.080  0.015 -0.043 ];
0041 mriFID.sub{12} = 'p04';
0042 mriFID.xyz{12} = [ -0.001  0.086 -0.009;  0.086  0.002 -0.062; -0.070  0.003 -0.065 ];
0043 mriFID.sub{13} = 'p05';
0044 mriFID.xyz{13} = [ -0.002  0.094 -0.001;  0.068  0.025 -0.040; -0.071  0.005 -0.042 ];
0045 mriFID.sub{14} = 'p06';
0046 mriFID.xyz{14} = [ -0.002  0.084  0.000;  0.082  0.013 -0.050; -0.066  0.013 -0.052 ];
0047 mriFID.sub{15} = 'p07';
0048 mriFID.xyz{15} = [  0.001  0.092  0.015;  0.080  0.003 -0.033; -0.070  0.004 -0.032 ];
0049 mriFID.sub{16} = 'p08';
0050 mriFID.xyz{16} = [ -0.003  0.095 -0.002;  0.070  0.018 -0.040; -0.074  0.022 -0.035 ];
0051 mriFID.sub{17} = 'p09';
0052 mriFID.xyz{17} = [ -0.002  0.100  0.004;  0.100  0.002 -0.028; -0.050  0.000 -0.036 ];
0053 
0054 
0055 
0056 
0057 
0058 data = 'd:\matlab\brainstorm_v1\subjects\';
0059 
0060 cd(data);
0061 
0062 % Load data
0063 for s = {'c01','c02','c03','c04','c05','c06','c07','c08','c09','c10',...
0064                'p02',      'p04','p05','p06','p07','p08','p09'},
0065     
0066        sub = sprintf('%s',char(s));
0067        cd(sub)
0068        
0069        p = eeg_toolbox_defaults;
0070        
0071        % Load the MRI volume (256^3, 1mm^3)
0072        p.mri.path = sprintf('d:\\freesurfer\\subjects\\ptsdpet-%s\\mri\\analyze\\',char(sub));
0073        p.mri.file = sprintf('%s_orig_axial_ras.img',char(sub));
0074        p.mri.plot = 0;
0075        p = mri_open(p);
0076        
0077        % -- Create the Patient Coordinate System (PCS) struct
0078        
0079        % Get surface fiducials from mriFID struct above
0080        Nfid = strmatch(sub,mriFID.sub);
0081        % 3x4, nasion, right, left, origin fiducial points in rows
0082        % Multiply the FID points by 1000 to get mm, rather than meters
0083        % Also add the origin (128)
0084        p.mriFID = [mriFID.xyz{Nfid} .* 1000 + 128; 128 128 128]';
0085        
0086        PCS.R = eye(3);     % [3x3 double] rotations
0087        PCS.t = zeros(3,1); % [3x1 double] translations
0088        PCS.Comment = 'NEUROMAG';
0089        PCS.PCSFiducial(:,1)  = p.mriFID(:,1); % NAS
0090        PCS.PCSFiducial(:,2)  = p.mriFID(:,3); % LPA
0091        PCS.PCSFiducial(:,3)  = p.mriFID(:,2); % RPA
0092        PCS.PCSFiducial(:,4)  = p.mriFID(:,4); % Origin
0093        PCS.CubeFiducial      = PCS.PCSFiducial;
0094        PCS.FiducialName = {'NAS'  'LPA'  'RPA'  'Origin'};
0095        
0096        % Load the scalp mesh
0097        p.mesh.path = sprintf('d:\\data_source\\%s\\meshes\\',char(sub));
0098        p.mesh.file = sprintf('%s_scalp.wfr',char(sub));
0099        p.mesh.type = 'emse';
0100        p = mesh_open(p);
0101        
0102        Scalp = p.mesh.data.vertices{1}';
0103        
0104        % The electrodes have already been coregistered and the transformed
0105        % coordinates saved, otherwise this code might do the job
0106        
0107        if coregister,
0108            
0109            % Get surface fiducials from mriFID struct above
0110            Nfid = strmatch(sub,mriFID.sub);
0111            % 3x3, nasion, right, left fiducial points in rows
0112            p.mriFID = mriFID.xyz{Nfid};
0113            
0114            % Load the electrode data
0115            p.elec.path = sprintf('d:\\data_source\\%s\\meshes\\',char(sub));
0116            p.elec.file = sprintf('%s_124fit.txt',char(sub));
0117            p.elec.plot = 0;
0118            p = elec_open(p);
0119            
0120            % Get electrode fiducials
0121            Efid = [p.elec.data.nasion; p.elec.data.rpa; p.elec.data.lpa];
0122            
0123            % Calculate coregistration transform
0124            T = elec_coregister(Efid,p.mriFID);
0125            % Create the PCS struct
0126            PCS.R = T([1:3;1:3]);
0127            PCS.t = T(4,1:3);
0128        end
0129        
0130        if elecplot,
0131            patch('vertices',p.mesh.data.vertices{1},'faces',p.mesh.data.faces{1},...
0132                'FaceColor',[1 0 0],'Edgecolor','none','FaceAlpha',.6);
0133            lighting phong, material dull, camlight headlight, hold on
0134            % Load the electrode data
0135            p.elec.path = sprintf('d:\\matlab\\brainstorm_v1\\studies\\%s\\',char(sub));
0136            p.elec.file = sprintf('%s_channel.mat',char(sub));
0137            p.elec.type = 'brainstorm';
0138            p.elec.plot = 0;
0139            p = elec_open(p);
0140            plot3(p.elec.data.x,...
0141                  p.elec.data.y,...
0142                  p.elec.data.z,'bo')
0143            daspect([1 1 1]); axis tight
0144            mouse_rotate
0145            return
0146        end
0147        
0148        
0149        % Maybe use FSL FAST result here?
0150        Segment = [];
0151        
0152        p.mri.data.fileprefix = sprintf('%s',char(sub));
0153        avw2brainstorm(p.mri.data,Segment,Scalp,PCS);
0154        
0155        cd ..
0156        
0157 end

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