I am using Matlab to transform an image to target image. I have geometric transformation(tform).
for example this is my 'tform':
1.0235 0.0022 -0.0607 0
-0.0276 1.0002 0.0089 0
-0.0170 -0.0141 1.1685 0
12.8777 5.0311 -70.0325 1.0000
In Matlab2013, it is possible to do it easily using imwarp:
%nii = 3D MR Image
I = nii.img;
dii=nii.hdr.dime.pixdim(2:4);
Rfixed=imref3d(size(I),dii(2),dii(1),dii(3));
new_img= imwarp(old_img, Rfixed, tform, 'OutputView', Rfixed);
the result is perfect using imwarp (red lung in the image)
I need to know how imwarp is working, Then I wrote my own function
function [new_img] = aff3d(old_img, tform, range_x, range_y, range_z)
[U, V, W] = ndgrid(range_x, range_y, range_z);
xyz = [reshape(U,[],1)';reshape(V,[],1)';reshape(W,[],1)'];
xyz = [xyz; ones(1,size(xyz,2))];
uvw = tform.T * xyz;
% avoid homogeneous coordinate
uvw = uvw(1:3,:)';
xi = reshape(uvw(:,1), length(range_x),length(range_y),length(range_z));
yi = reshape(uvw(:,2), length(range_x),length(range_y),length(range_z));
zi = reshape(uvw(:,3), length(range_x),length(range_y),length(range_z));
old_img = single(old_img);
new_img = interp3(old_img,yi,xi,zi,'linear');
ii = find(isnan(new_img));
if(~isempty(ii))
new_img(ii) = 0;
end
end
the result of my function (more info) is not match with imwarp output ( red lung is not locating in a correct place), anybody can help me?