0

I have written the following code without any Matlab built-in functions to rotate an image. I tried to write another loop to invert the rotation. the image does rotate back but I still get the size of the previously rotated image. How can I get rid of the black parts in the image?

INPUT_IMAGE = 'forest.png';
img_in=double(imread(INPUT_IMAGE))./255;

h=size(img_in,1);  
w=size(img_in,2); 

R=[cos(th) -sin(th) 0 ; sin(th)  cos(th) 0 ; 0 0 1];
T=[1 0 (-w/2) ; 0 1 (-h/2) ; 0 0 1];
F=inv(T)*R*T;

img_out=zeros(h,w,3);

%Rotate image
for i=1:w
   for j=1:h

    a = [i ; j ; 1];
    b = inv(F) * a;

    x = b(1)/b(3);
    y = b(2)/b(3);

    x = floor(x);
    y = floor(y);

       if (x>0 & x<=W & j>0 & j<=H) 
          img_out(y,x,:)=img_in(j,i,:);
       end

   end
end

img_out2=zeros(h,w,3);

%invert rotation 
for i=1:w
   for j=1:h

    a = [i ; j ; 1];
    b = F * a;

    x = b(1)/b(3);
    y = b(2)/b(3);

    x = floor(x);
    y = floor(y);

       if (x>0 & x<=W & j>0 & j<=H) 
          img_out2(y,x,:)=img_out(j,i,:);
       end

   end
end

The result:

result

I know the image has black gaps due to the forward mapping but I'm not concerned about that as I'm trying to implement a code without built-in functions that would only rotate the image back so I can calculate the error.

Anna1994
  • 140
  • 1
  • 12
  • 1
    The code you posted is not doing the inverse rotation. You are applying the same transform in the first loop as in the second, thus they are doing the same thing! – Ander Biguri Oct 31 '16 at 09:02
  • However, to answer your question: If you transform with the forward mapping, then the information gets lost! If a pixel was never accessed in the first image, then it will just not exist in the second image, so you can not use this second image to create the first one again! – Ander Biguri Oct 31 '16 at 10:01
  • In case if you don't know. MATLAB is case-sensitive. (Saying this because you're using `W` and `H` in your code which are initialized nowhere) – Sardar Usama Oct 31 '16 at 11:11
  • @AnderBiguri I do not want to create the first image, all I want to do is rotate the output image back so I can compare it with the source image and calculate the error – Anna1994 Oct 31 '16 at 17:22
  • @Anna1994 however, my first comment holds. You are not doing the inverse rotation, you are just rotating again – Ander Biguri Oct 31 '16 at 17:26
  • Please, read [mcve]. Your code is full of errors. Fix it so we can help. – Ander Biguri Oct 31 '16 at 17:38

1 Answers1

3

Instead of iterating the source image, inverse transformation matrix, and iterate destination image.

Iterating destination image guarantees to have no holes (each pixel gets a value).

The code you have posted is not working, please fix it...
I based my answer on your previous post: Matlab image rotation

I used 'peppers.png' instead of 'forest.png' (I can't find 'forest.png', next time, please add the image to your post).

The example code do the following:

  • Rotate input image (You may treat it as "reverse transformation").
  • Rotate result image back (using inverse transformation matrix).
  • Display absolute difference of original image and result image.

close all;
clear all;

img_in = 'peppers.png';

img_in =double(imread(img_in))./255;
orig_in = img_in;

h=size(img_in,1);  
w=size(img_in,2); 

th = pi/4;
R=[cos(th) -sin(th) 0 ; sin(th)  cos(th) 0 ; 0 0 1];
T=[1 0 (-w/2) ; 0 1 (-h/2) ; 0 0 1];
F=inv(T)*R*T;

img_out=zeros(h,w,3);

%Rotate image
for i=1:w
    for j=1:h

        x = [i ; j ; 1];
        y = F * x;

        a = y(1)/y(3);
        b = y(2)/y(3);

        a = round(a);
        b = round(b);

        if (a>0 && a<=w && b>0 && b<=h) 
           img_out(j,i,:)=img_in(b,a,:);
        end
    end
end

figure;imshow(img_out);

%Rotate back
%---------------------------------------------------------

img_in = img_out;
img_out = zeros(h,w,3);

%Inverse transformation matrix.
F = inv(F);

%Rotate image (back)
for i=1:w
    for j=1:h

        x = [i ; j ; 1];
        y = F * x;

        a = y(1)/y(3);
        b = y(2)/y(3);

        a = round(a);
        b = round(b);

        if (a>0 && a<=w && b>0 && b<=h) 
           img_out(j,i,:)=img_in(b,a,:);
        end
    end
end

figure;imshow(img_out);

img_diff = abs(orig_in - img_out);
figure;imshow(img_diff);

img_diff image:
enter image description here

Community
  • 1
  • 1
Rotem
  • 30,366
  • 4
  • 32
  • 65