Sendi Novianto
Sendi Novianto

Reputation: 33

Issues with reversing a rotated image in MATLAB

I will explain with a picture so you can gain a better understanding as seen below:

Final Result

With the above result, everything appears as normal. I have some image and I attempt to rotate this image with imrotate. In this case, I use a 45 degree counter-clockwise rotation. However when I try to reverse rotation (my purpose is to get normal position of image without rotation), everything becomes strange - especially at image resolution. The image resolution decreases and the size of image will change because of many noise in that picture as you can see in the above figure.

Additional information

My question is the following - How do I get a clean and nice image like the original image?

I have the code that I wrote shown below:

clc;
close all;  % Close all figure windows except those created by imtool.
imtool close all;
clearvars; % Get rid of variables from prior run of this m-file.
workspace;  % Make sure the workspace panel is showing.

Rotation=45;

%Read the image
imagepad = imread('peppers.png');

%% Image Source Information
info1=size(imagepad);
fprintf('Real Image Information : \n'); % Message sent to command window.
fprintf(' Width  : %d\n',info1(2)); % Message sent to command window.
fprintf(' Height : %d\n',info1(1)); % Message sent to command window.
fprintf('------------------------');

%%Try Rotate 45 degree
imRotate = imrotate(imagepad,Rotation);

%%
figure;
subplot(121);
imshow(imagepad);
caption = sprintf('Real Image');
title(caption, 'FontSize', 13);
subplot(122);
imshow(imRotate);
caption = sprintf('Rotate 45 degree result');
title(caption, 'FontSize', 13);

%%try to return the image position
imReturn = imrotate(imRotate,-Rotation);

%%Try to return the size of image
%% find pixel
%im = imread('im.png');      %# load image
[y,x] = find(all(imReturn>0, 3)); %# find black pixels
position = [x,y];           %# display them
[x1]=min(position);
[x2]=max(position);

%%Normal Size
Im2 = imcrop(imReturn,[x1(1) x1(2) (x2(1)-x1(1)) (x2(2)-x1(2))]);
figure, imshow(Im2);
caption = sprintf('Last Result image size');
title(caption, 'FontSize', 13);

%%
figure;
subplot(221);
imshow(imagepad);
caption = sprintf('Real Image');
title(caption, 'FontSize', 13);
subplot(222);
imshow(imRotate);
caption = sprintf('Rotate 45 degree result');
title(caption, 'FontSize', 13);
subplot(223);
imshow(imReturn);
caption = sprintf('Inverse Rotate 45 degree result');
title(caption, 'FontSize', 13);
subplot(224);
imshow(Im2);
caption = sprintf('Crop result');
title(caption, 'FontSize', 13);

%%Different Resolution
subplot(121);
imshow(imagepad);
caption = sprintf('Real Image');
title(caption, 'FontSize', 13);
subplot(122);
imshow(Im2);
caption = sprintf('Crop result');
title(caption, 'FontSize', 13);


%% Image Source Information
info2=size(Im2);
fprintf('Return Image Information : \n'); % Message sent to command window.
fprintf(' Width  : %d\n',info2(2)); % Message sent to command window.
fprintf(' Height : %d\n',info2(1)); % Message sent to command window.
fprintf('------------------------');

Upvotes: 3

Views: 632

Answers (1)

rayryeng
rayryeng

Reputation: 104535

I'm not sure what is wrong with the size of the image. When you rotate the image, because the corners of the image extend beyond the image dimensions, you have to pad the image so that you can accommodate for the rotation. After you rotate the image back using the rotated image, the image dimensions will stay the same which is why you need to crop the image to get the final result.

However, you are experiencing jagged noise errors because the default interpolation algorithm is nearest neighbour. This means that when you rotate the image, the holes are filled in with pixels from nearby neighbours. This results in "noise" because the holes should theoretically be filled in by fractional positions in the original image and so this will promote a smooth transition leaving and entering the holes. As such, you have to choose a different interpolation algorithm for imrotate. Use bilinear or bicubic interpolation instead.

Therefore, you need to change your imrotate calls to use the different interpolation algorithms. I'll use bicubic here. When you rotate first, change your code to:

%%//Try Rotate 45 degree
imRotate = imrotate(imagepad,Rotation,'bicubic');

Similarly when you rotate back:

%%//try to return the image position
imReturn = imrotate(imRotate,-Rotation,'bicubic');

When I run your code now, I get this when comparing the original and rotated forward and backwards image:

enter image description here

You'll see that there aren't any "jagged edges" because of the interpolation algorithm. There is some smoothing involved though and that's a consequence of the bicubic interpolation algorithm.

Upvotes: 3

Related Questions