Inverse transform image in matlab

1.7k views Asked by At

I am using this code to translate an image but when I apply inverse on the image it did not show the whole image it shows only transformed image.

tran= [1 0 0; 0 1 0; -130 -100 1];
traninv= [1 0 0; 0 1 0; 130 100 1];
% apply translation
tform= maketform('affine', tran);
 ttform=maketform('affine', traninv);
 out=imtransform(img, tform, 'XData',[1 size(img,2)],'YData',[1 size(img,1)]);
 figure;
 imagesc(out);
 title ('translated image');
 colormap gray
 ot=imtransform(out, ttform, 'XData',[0 size(out,0)],'YData',[0 size(out,0)]);
 figure;
 imagesc(ot);
  title ('inverse translated image');
  colormap gray
1

There are 1 answers

13
Benoit_11 On BEST ANSWER

I think your problem comes from the size of the XData and YData you provide imtransform. Since they correspond to the size of the original image and the later is translated, MATLAB padds with 0 to fill the blanks but since you specify that you want a particular dimension for your image you seem to get only part of the image.

Here is a workaround to show you that your translation does work:

1) Create a canvas containing the original image; the canvas is large enough to contain the image and the rest is just black. I use the coins image that ships with MATLAB.

img = imread('coins.png');

%// Get size of original image

[height,width,~] = size(img)

canvas = zeros(2*size(img),'like',img);

%// Put image in canvas.
canvas(height+1:end,width+1:end) = img;

figure;
imagesc(canvas)
colormap gray
title('Original image','FontSize',16)

Looking like this:

enter image description here

Then apply your code to translate the image. Notice that change in XData and YData in imtransform:

tran= [1 0 0; 0 1 0; -130 -100 1];
traninv= [1 0 0; 0 1 0; 130 100 1];

% apply translation
tform= maketform('affine', tran);
ttform=maketform('affine', traninv);
out=imtransform(canvas, tform, 'XData',[1 size(canvas,2)],'YData',[1 size(canvas,1)]); %// Size is important!

figure;
imagesc(out);
title ('translated image','FontSize',16);
colormap gray

ot=imtransform(out, ttform, 'XData',[1 size(canvas,2)],'YData',[1 size(canvas,1)]);
figure;
imagesc(ot);
title ('inverse translated image','FontSize',16);
colormap gray

which gives the following translated image:

enter image description here

and inverse-translated image:

enter image description here

which looks exactly like the original. Note that it is preferable to use affine2d combined with imwarp instead of maketform and imtransform, but that's your call.

Hope that helps!