FreesurferGoodies/coregisterSPMtoFreeSurfer - MRC CBU Imaging Wiki

Upload page content

You can upload content for the page named below. If you change the page name, you can also upload content for another page. If the page name is empty, we derive the page name from the file name.

File to load page content from
Page name
Comment
Type the odd characters out in each group: abz2a 125t7 HhHaHh year.s 5433r21 worl3d

location: FreesurferGoodies / coregisterSPMtoFreeSurfer

function coregisterSPMtoFreeSurfer
freesurferPath='/the/path/to/your/freesurfer/subjectdirectory/';
subjects = {...
    'CBUXXXYYY',...
    'CBUXXXYY2',...
    };

nSubjects=length(subjects);


% parameters for co-registration:
% ---------------------------------------------------
% (will be replaced by defaults if not specified):
coreg.cost_fun = 'nmi'; % which cost function to use - normalised mutual information
coreg.sep = [4 2];      % resolution at which to sample images
coreg.tol = [0.0200    0.0200    0.0200    0.0010    0.0010    0.0010 ...
    0.0100    0.0100 0.0100    0.0010    0.0010    0.0010]; % tolerance for parameters
coreg.fwhm = [7 7]; % degree of smoothing to apply to images before registration



for subI=1:nSubjects
   
    % specify the reference high resolution anatomical (freeSurfer)
    fs_anat  = fullfile(freesurferPath,subjects{subI},'mri','anat.nii');
    
    % specify the source high resolution anatomical (copied SPM one)
    spm_anat = get_files(fullfile(freesurferPath,subjects{subI},'spm_maps'),'s*.nii');
    
    x=spm_coreg(fs_anat,spm_anat,coreg); % find the coreg parameters
    
    % convert the coreg parameters into an affine transformation matrix
    M  = inv(spm_matrix(x));
    MM = zeros(4,4,1);
    MM(:,:,1) = spm_get_space(spm_anat);
    
    % modify the header of the source structural image
    spm_get_space(spm_anat, M*MM(:,:,1));
    
end
end

function files = get_files(direc, filt)
% =========================================================================
% return a list of files
% filt = filter string
% direc = cell array of directory names
% revised 07-2011 Ian Charest
if nargin~=2, error('get_files:missing inputs, Please input folder(s) and file filter.'); end%if
files = [];
if ischar(direc) % if direc is already a character array
    currDir = direc;
    tmp = dir(fullfile(currDir,filt)); % find all files matching f*.nii
    tmp = [repmat([currDir filesep],size(tmp,1),1) char(tmp.name)]; % build the full path name for these files
    files = char(files,tmp);
else % if direc is a cell array
    if size(direc,1)>size(direc,2)
        nRuns=size(direc,1);
    else
        nRuns=size(direc,2);
    end
    for runI=1:nRuns % loop through each EPI session
        currDir = char(direc{runI});
        tmp = dir(fullfile(currDir,filt)); % find all files matching f*.nii
        tmp = [repmat([currDir filesep],size(tmp,1),1) char(tmp.name)]; % build the full path name for these files
        files = char(files,tmp);
    end
end
files = files(~all(files'==' ')',:);
end