In order to use headmodels for EEG data created of individual MRIs (e.g. BEM), it is necessary to fit the individual electrode positions to the same coordinate system as the individual MRI. This script allows transforming electrode coordinates to the individual MRI space using the 3 fiducial points 'nasion', 'preauricular left' and 'preauricular right', given in the electrode coordinates structure. Furthermore, it is necessary to provide the voxel coordinates of the same fiducial points in the MRI. Typically the MRI is aligned to MNI space, thus the resulting electrode positions are as well aligned to MNI space.
% fit electrode coordinates to an individual MRI according to the same
% fiducials (nasion, left & right preauricular points) in both systems
% this script requires the functions electrodenormalize.m & warp_apply.m from
% the private directory of fieldtrip
% provide electrode coordinates, mri structure, and fiducials in mri
% convert electrode coordiantes from cm to mm
elec.pnt = elec.pnt * 10; % should be the same unit as MRI
vox2head = mri.transform; % transformation matrix of individual MRI
% provide fiducial coordinates in electrodespace
Nas = elec.pnt(strcmp(elec.label, 'nasion'),:);
Lpa = elec.pnt(strcmp(elec.label, 'left'),:);
Rpa = elec.pnt(strcmp(elec.label, 'right'),:);
% provide fiducials (in voxelspace, e.g. [57,127,15]) of individual MRI
% find fiducials e.g. by using sourceplot(cfg, mri) which plots a figure in which
% you can interactively select slices of the mri
vox_Nas = mri.nasion; % fiducials saved in mri structure
vox_Lpa = mri.left;
vox_Rpa = mri.right;
% transform voxel indices of individual MRI to headcoordinates in mm
head_Nas = warp_apply(vox2head, vox_Nas, 'homogenous'); % nasion
head_Lpa = warp_apply(vox2head, vox_Lpa, 'homogenous'); % Left preauricular
head_Rpa = warp_apply(vox2head, vox_Rpa, 'homogenous'); % Right preauricular
elec_mni.label = {'nasion', 'left', 'right'};
elec_mni.pnt = [
head_Nas
head_Lpa
head_Rpa
];
% align using fiducials
cfg = [];
cfg.method = 'realignfiducial';
cfg.template = elec_mni;
cfg.elec = elec;
cfg.fiducial = {'nasion', 'left', 'right'};
elec_new = electroderealign(cfg);
% align interactive to surface of headmodel stored in vol.bnd
cfg = [];
cfg.method = 'interactive';
cfg.elec = elec;
cfg.headshape = vol.bnd(1);
elec_new = electroderealign(cfg);