Losing pixels with imrotate
14 views (last 30 days)
Show older comments
Eleonora Montagnani
on 20 Jan 2022
Commented: Eleonora Montagnani
on 21 Jan 2022
Hello,
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?
Thanks
12 Comments
Bjorn Gustavsson
on 20 Jan 2022
Why would the intensities change and how? Why wouldn't you be able to how a possible intensity-measure varies with such an up-sampling? (if you take some sum of intensities in a bright region it will increase by a factor of 4 if you double number of pixels with nearest-intepolation.) Try and test and think a few things.
Accepted Answer
More Answers (2)
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.
0 Comments
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:
imagesc;
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);
imagesc(D3)
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:
imagesc;
D = get(gca,'Children');
D = get(D,'CData');
Dbigger = interp2(D,linspace(1,64,256),linspace(1,64,256)','nearest');
imagesc(Dbigger)
That should change the apparent effect of interpolation-behaviour during rotation - however your image-regions will be bigger...
HTH
1 Comment
Matt J
on 20 Jan 2022
imrotate automatically pads the image in the manner you describe. The 'crop" flag will suppress that.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!