How to get displacement field from tform?

9 views (last 30 days)
I would like to represent a rigid image transformation (tform) that I have determined as a displacement field, (D).
In the code below I try to make such a displacement field (D) from tform, but when I use tform and D on the same image I get two different results. If someone can spot what I am doing wrong when I make the displacement field I would be very grateful. Or alternatively know of another way to get the displacement field from tform...
% import matlab standard images
fixed = imread('hands1.jpg');
moving = imread('hands2.jpg');
fixed = rgb2gray(fixed);
moving = rgb2gray(moving);
% determine a rigid tform
[optimizer, metric] = imregconfig('monomodal');
tform = imregtform(moving, fixed, 'rigid', optimizer, metric);
% use found tform to warp moving image
moving_reg1 = imwarp(moving,tform);
% make a displacement field that corresponds to tform
[X,Y] = meshgrid(1:size(moving,2),1:size(moving,1)); % make grids with x and y coordinates of pixels
[XT,YT] = transformPointsForward(tform,X,Y); % Transform pixel coordinates using tform
D = zeros(size(X,1),size(X,2),2);
D(:,:,1) = X-XT; % displacement field should be the difference!?
D(:,:,2) = Y-YT;
moving_reg2 = imwarp(moving,D); % warp moving image with the new displacement field
% now I would expect the two registered images moving-reg1 and moving-reg2
% to be the same - but they are not!
figure
imshowpair(moving_reg1,moving_reg2); title('moving-reg1 and moving-reg2')
  1 Comment
Silja Heilmann
Silja Heilmann on 31 Dec 2016
Ok so I have not solved my own problem, but I have realized that I dont think my displacement field is wrong per se. The problem seems to be that you do not get the same result when you transform images with imregister() and imwarp(im,tform) even though you use the same tform. However the transformed images you get when using imregister() and imwarp(im,D), where D is the displacement field I made, are the same! What is going on?
% import matlab standard images
fixed = imread('hands1.jpg');
moving = imread('hands2.jpg');
fixed = rgb2gray(fixed);
moving = rgb2gray(moving);
% get optimizer and metric
[optimizer, metric] = imregconfig('monomodal');
% get registered image and tform1 used in function imregister()
[moving_reg11,Rreg,tform1] = imregister2(moving,fixed,'rigid',optimizer,metric);% extension of IMREGISTER that also returns the TFORM that is applied
% get tform1 used via function imregtform()
tform2 = imregtform(moving,fixed,'rigid',optimizer,metric);
% use found tforms to warp moving image
moving_reg12 = imwarp(moving,tform1);
moving_reg13 = imwarp(moving,tform2);
% make a displacement field that corresponds to tform = tform1 = tform2
[X,Y] = meshgrid(1:size(moving,2),1:size(moving,1)); % make grids with x and y coordinates of pixels
[XT,YT] = transformPointsForward(tform1,X,Y); % Transform pixel coordinates using tform
D = zeros(size(X,1),size(X,2),2);
D(:,:,1) = X-XT; % displacement field should be the difference!?
D(:,:,2) = Y-YT;
moving_reg14 = imwarp(moving,D); % warp moving image with the new displacement field
% compare the three images...
figure
subplot(3,1,1)
imshowpair(moving_reg12,moving_reg13); title('(moving-reg12 and moving-reg13). tform1 and tform2 returned by imregister2() and imregtform() are the same')
subplot(3,1,2)
imshowpair(moving_reg11,moving_reg12); title('(moving-reg11 and moving-reg12). Images transformed using imregister2() and imwarp(im,tform) with the same tform are not the same!')
subplot(3,1,3)
imshowpair(moving_reg11,moving_reg14); title('(moving-reg11 and moving-reg14). Images transformed using imregister2() and imwarp(im,D) with my displacement field are the same...')

Sign in to comment.

Accepted Answer

Vandana Rajan
Vandana Rajan on 5 Jan 2017
Hi Silja,
The issue is because you are using an incomplete syntax of 'imwarp' in the code. 'imwarp' function has an 'OutputView' parameter to preserve world limits and resolution of the fixed image when forming the transformed image.
Use the following line of code instead of 'moving_reg1 = imwarp(moving,tform);'
moving_reg1 = imwarp(moving,tform,'OutputView',imref2d(size(fixed)));

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!