function Vout = adjust_pitch(V,theta,overwrite)
spm_defaults;
global defaults
if nargin<1 | isempty(V)
    V=spm_vol(spm_get([0 Inf],'*IMAGE','Select Images to rotate'));
end
if nargin<2 | isempty(theta)
    theta=input('pitch adjustment angle in radians? ')
end
if nargin<3
    overwrite=0;
end
for i=1:length(V)
    [pth,nam,ext] = fileparts(V(i).fname);
    matfile = fullfile(pth,[nam,'.mat']);
    oldhdr = fullfile(pth,[nam,'.hdr']);
    oldimg = fullfile(pth,[nam,ext]);
    matfile = fullfile(pth,[nam,'.mat']);
    if ~overwrite
        newhdr = fullfile(pth, ['r_',nam,'.hdr']);
        newimg = fullfile(pth, ['r_' ,nam,ext]);
        matfile = fullfile(pth,['r_' ,nam,'.mat']);
        copyfile(oldhdr,newhdr);
        copyfile(oldimg,newimg);
        Vout(i) = spm_vol(newimg);
    else
        newimg=oldimg;
    end
    if exist(matfile,'file')
        disp(['Warning: about to overwrite ',matfile])
        mls_cleanup({matfile});
    end
    %foo = spm_imatrix(V(i).mat);
    %foo(4)=theta;
    %V(i).mat=spm_matrix(foo);
    
   rotmat=eye(4);
   rotmat(2,2)=cos(theta);
   rotmat(3,3)=rotmat(2,2);
   rotmat(2,3)=sin(theta);
   rotmat(3,2)=-rotmat(2,3);
   V(i).mat = rotmat*V(i).mat;

    
    mat=V(i).mat;
    M(1,1)=-mat(1,1);
    M(1,4)=-mat(1,4);
    save(matfile,'M','mat')
    Vout(i) = spm_vol(newimg);
end