Skip to content

Instantly share code, notes, and snippets.

@Remi-Gau
Created May 30, 2019 12:34
Show Gist options
  • Save Remi-Gau/bb2a0159f335fb7c24a3a34f506b0936 to your computer and use it in GitHub Desktop.
Save Remi-Gau/bb2a0159f335fb7c24a3a34f506b0936 to your computer and use it in GitHub Desktop.
% small script to show some basic way to render volume data on surfaces using SPM
clear
close all
clc
% which surface to use
% uses one of the default SPM surfaces but it should be doable to create you own
% from the results of a segmentation or from some freesurfer output to have
% a better group template
gii = fullfile(spm('dir'), 'canonical', 'cortex_5124.surf.gii');
con = '/home/remi/Downloads/AV-att/spm_0001.nidm/Contrast.nii';
tstat = '/home/remi/Downloads/AV-att/spm_0001.nidm/TStatistic.nii';
%% project the data of 2 volumes on the surface at once
% can be used for up to 3 images
% will be prompted for which color to use
spm_render(char({con;tstat}),NaN,gii)
%% project the data of 1 volumes on the surface
% this one shows a couple more things from the SPM machinery you can use to
% script figure making
% open the gifti surface data file
M = gifti(gii);
% inflate the surface
M2 = spm_mesh_inflate(M, 10);
% display the surface
H = spm_mesh_render(M2);
% sample the volume data only on the vertices (only on the surface)
% taken from spm_mesh_project
V = M.vertices;
P = zeros(1,size(V,1));
hdr = spm_vol(con);
vol = spm_read_vols(hdr);
Y = vol;
mat = hdr.mat;
XYZ = double(inv(mat)*[V';ones(1,size(V,1))]);
P = spm_sample_vol(Y,XYZ(1,:),XYZ(2,:),XYZ(3,:),0);
AX = spm_mesh_render('Overlay', H, P)
%% thresholding
% in case you want to show beta values that only have t value superior to a
% certain value
t_val = spm_read_vols(spm_vol(tstat));
Y = vol;
% Y(t_val>1) = NaN;
Y(t_val<0.5) = NaN;
P = spm_sample_vol(Y,XYZ(1,:),XYZ(2,:),XYZ(3,:),0);
AX = spm_mesh_render('Overlay', H, P);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment