-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_02_create_headmodels.m
88 lines (73 loc) · 2.78 KB
/
example_02_create_headmodels.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
%% Create headmodels
%
% Vinding, M. C., & Oostenveld, R. (2022). Sharing individualised template MRI data for MEG source reconstruction: A solution for open data while keeping subject confidentiality. NeuroImage, 119165. https://doi.org/10.1016/j.neuroimage.2022.119165
%
% Create headmodels (volume conductor models) from anatomical MRIs for MEG
% source analysis. This is done for the template MRI warped to subject and for
% the original subject MRI for comparison. This script run the process
% step by step for both the warped template and the orignal MRI to compare
% the results later on.
%% Setup
addpath '~/fieldtrip/fieldtrip/'
ft_defaults
%% Data paths
data_path = '~/mri_warpimg/data/0177/170424';
%% Load MRI
fprintf('Loading data... ')
load(fullfile(data_path, 'mri_warptmp.mat')); % Warped template MRI
load(fullfile(data_path, 'mri_org_resliced.mat')); % original subject MRI
disp('done')
%% STEP 1: Segment inner volume of MRI.
cfg = [];
cfg.output = 'brain';
cfg.spmmethod = 'new';
mri_tmp_seg = ft_volumesegment(cfg, mri_warptmp);
mri_org_seg = ft_volumesegment(cfg, mri_org_resliced);
% Save (optional)
save(fullfile(data_path, 'mri_tmp_seg.mat'), 'mri_tmp_seg')
save(fullfile(data_path, 'mri_org_seg.mat'), 'mri_org_seg')
%% Plot segmentations for inspection
% Plot segmentation for each
mri_tmp_seg.anatomy = mri_warptmp.anatomy;
mri_org_seg.anatomy = mri_org_resliced.anatomy;
cfg = [];
cfg.anaparameter = 'anatomy';
cfg.funparameter = 'brain';
ft_sourceplot(cfg, mri_tmp_seg);
ft_sourceplot(cfg, mri_org_seg);
%% STEP 2: Construct mesh from inner volume and create the headmodel
% Step 2A: create 3D brain mesh
cfg = [];
cfg.method = 'projectmesh';
cfg.tissue = 'brain';
cfg.numvertices = 3000;
mesh_brain_tmp = ft_prepare_mesh(cfg, mri_tmp_seg);
mesh_brain_org = ft_prepare_mesh(cfg, mri_org_seg);
% Step 2B: create headmodels
cfg = [];
cfg.method = 'singleshell';
headmodel_tmp = ft_prepare_headmodel(cfg, mesh_brain_tmp);
headmodel_org = ft_prepare_headmodel(cfg, mesh_brain_org);
% Save headmodels
save(fullfile(data_path, 'headmodel_tmp.mat'), 'headmodel_tmp')
save(fullfile(data_path, 'headmodel_org.mat'), 'headmodel_org')
disp('done')
%% Plot headmodels for inspection
load(fullfile(data_path, 'headshape.mat'))
load(fullfile(data_path, 'grad.mat'))
% Plot alignment with sensors
subplot(1,2,1); hold on
ft_plot_headmodel(headmodel_tmp, 'edgecolor','r', 'facealpha',0.5)
ft_plot_sens(grad)
ft_plot_headshape(headshape)
title('template')
subplot(1,2,2); hold on
ft_plot_headmodel(headmodel_org, 'edgecolor','b', 'facealpha',0.5)
ft_plot_sens(grad)
ft_plot_headshape(headshape)
title('orig')
% Plot just headmodels
figure; hold on
ft_plot_headmodel(headmodel_tmp, 'edgecolor','r', 'facealpha',0.5)
ft_plot_headmodel(headmodel_org, 'edgecolor','b', 'facealpha',0.5)
%END