Losing pixels with imrotate

2 views (last 30 days)
I have a set of frames which need to be rotated vertically to ensure consistency in orientation across images. To do so, I created a bounding box around the frames and cropped them using imcrop and then with imrotate I perform the rotation of x degrees. However, when I do so, my image loses pixels as you can see from the images attached. Does anyone know why this happen and have a solution not to lose pixels?

Sign in to comment.

Accepted Answer

Matt J
Matt J on 20 Jan 2022
Edited: Matt J on 20 Jan 2022
So my question was: is there any other way to rotate the images without losing pixels?
If you use 'linear' or 'cubic' interpolation, you will not lose white pixels (but you may gain some).
Matt J
Matt J on 20 Jan 2022
I think we need a more direct example of what you're trying to do.

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 20 Jan 2022
No there is no way. In every case where you rotate by something that is not a multiple of 90 degrees, pixels will change.
Consider for example a pixel along a horizontal line that is rotated by 45 degrees. In infinite resolution, the pixel would end up on a slant that goes exactly half way through a pixel. If you take the half occupancy and declare that is enough to fill the entire pixel after projection, then you have changed the shape. If you take the half occupancy and declare that it is not enough to fill the entire pixel after projection, then you have changed the shape in a different way. But you must choose one of the two, off or on, so either way you affect the final shape.
You can up-sample and rotate and down-sample but you still have the same ultimate problem: the pixel either gets completely filled or it stays completely empty, and either one changes the shape.

Bjorn Gustavsson
Bjorn Gustavsson on 20 Jan 2022
This happens due to aliasing-effects the discrete pixel-squares will start to mix and spread out between some neighboring pixels when you rotate the image (compare the looks of pixelated text and proper vectorized text in a post-scrip/pdf document, same effect). To guarantee that no pixels rotate outside of the image you should put the frame you want to rotate in the middle of an image/matrix with a size that is at least sqrt(2) times larger than the larger dimension of your image-frame:
D = get(gca,'Children');
D = get(D,'CData');
D2 = zeros(2*size(D)); % I didn't bother to make a tight fit here.
D2(end/4:3*end/4-1,end/4:3*end/4-1) = D; % This works at least for square images
D3 = imrotate(D2,45);
That way your entire frame will always remain inside the rotated image.
In order to reduce the antialiasing effects of interpolation you could "up-sample" your image with 2-D interpolation first:
D = get(gca,'Children');
D = get(D,'CData');
Dbigger = interp2(D,linspace(1,64,256),linspace(1,64,256)','nearest');
That should change the apparent effect of interpolation-behaviour during rotation - however your image-regions will be bigger...
  1 Comment
Matt J
Matt J on 20 Jan 2022
imrotate automatically pads the image in the manner you describe. The 'crop" flag will suppress that.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by