%%acpc_coreg_for_batch_editor.m
% batch job for AC-PC reorientation
% This script tries to set AC-PC with 2 steps.
% 1. Set origin to center (utilizing a script by F. Yamashita)
% 2. Coregistration of the image to icbm152.nii under spm/toolbox/DARTEL
% 
% This script is modified to run from SPM batch editor.
%
% K. Nemoto 19/Sep/2021

%% Select images
imglist=spm_select(Inf,'image','Choose MRI you want to set AC-PC');

%% Set the origin to the center of the image
% This part is written by Fumio Yamashita.
for i=1:size(imglist,1)
    file = deblank(imglist(i,:));
    st.vol = spm_vol(file);
    vs = st.vol.mat\eye(4);
    vs(1:3,4) = (st.vol.dim+1)/2;
    spm_get_space(st.vol.fname,inv(vs));
end

%% Coregister images with icbm152.nii under spm12/toolbox/DARTEL
for i=1:size(imglist,1)
    matlabbatch{i}.spm.spatial.coreg.estimate.ref = {fullfile(spm('dir'),'toolbox','DARTEL','icbm152.nii,1')};
    matlabbatch{i}.spm.spatial.coreg.estimate.source = {deblank(imglist(i,:))};
    matlabbatch{i}.spm.spatial.coreg.estimate.other = {''};
    matlabbatch{i}.spm.spatial.coreg.estimate.eoptions.cost_fun = 'nmi';
    matlabbatch{i}.spm.spatial.coreg.estimate.eoptions.sep = [4 2];
    matlabbatch{i}.spm.spatial.coreg.estimate.eoptions.tol = [0.02 0.02 0.02 0.001 0.001 0.001 0.01 0.01 0.01 0.001 0.001 0.001];
    matlabbatch{i}.spm.spatial.coreg.estimate.eoptions.fwhm = [7 7];
end

