## flirt_resamp

Type:
Full Script
Category:
Language:
MATLAB

Description:
Resample a NIfTI source image using a flirt -omat matrix

Example:
flirt_resamp('src_flirtmat.txt', 'src.nii', 'trg.nii', 'res.nii')

# Versions Of This Snippet::

Ged Ridgway
 Snippet ID Download Version Date Posted Author Delete 7 0.1 2009-07-27 00:31 Ged Ridgway

# Latest Snippet Version: :0.1

```function flirt_resamp(affmat, src, trg, outname)
%flirt_resamp: Resample a NIfTI source image using a flirt -omat matrix
% Example:
%  flirt_resamp('src_flirtmat.txt', 'src.nii', 'trg.nii', 'res.nii')
% Note that this was written for testing purposes -- it is very slow, and
% better results could be achieved using e.g. spm_bsplinc/spm_bsplins.

% Copyright 2009 Ged Ridgway <ged.ridgway gmail.com>

src = nifti(src);
trg = nifti(trg);

[worldmat spmvoxmat] = flirtmat2worldmat(affmat, src, trg);

res = trg;
res.dat.fname = outname;
create(res);
res.dat(:, :, :) = resamp_vox(spmvoxmat, src.dat(:, :, :), trg.dat.dim);

%%
function res = resamp_vox(mat, src, trgdim)
% mat should be vox-vox and zero-based
x = 1:trgdim(1);
y = 1:trgdim(2);
z = 1:trgdim(3);
[x y z] = ndgrid(x, y, z);
xyz1 = [x(:) y(:) z(:) ones(size(x(:)))]';
XYZ1 = mat * xyz1; % transform to source coords
X = reshape(XYZ1(1, :), trgdim);
Y = reshape(XYZ1(2, :), trgdim);
Z = reshape(XYZ1(3, :), trgdim);
res = interpn(src, X, Y, Z);

```

## Submit a new version

You can submit a new version of this snippet if you have modified it and you feel it is appropriate to share with others..