Losing pixels with imrotate

9 ビュー (過去 30 日間)
Eleonora Montagnani
Eleonora Montagnani 2022 年 1 月 20 日
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 件のコメント
Bjorn Gustavsson
Bjorn Gustavsson 2022 年 1 月 20 日
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.
Eleonora Montagnani
Eleonora Montagnani 2022 年 1 月 21 日
Thank you all!

サインインしてコメントする。

採用された回答

Matt J
Matt J 2022 年 1 月 20 日
編集済み: Matt J 2022 年 1 月 20 日
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).
  4 件のコメント
Eleonora Montagnani
Eleonora Montagnani 2022 年 1 月 20 日
the problem is that each image has its on reference system so I thought to create a common one with the bounding box, which however requires binary images if I am right.
Matt J
Matt J 2022 年 1 月 20 日
I think we need a more direct example of what you're trying to do.

サインインしてコメントする。

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2022 年 1 月 20 日
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 2022 年 1 月 20 日
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 件のコメント
Matt J
Matt J 2022 年 1 月 20 日
imrotate automatically pads the image in the manner you describe. The 'crop" flag will suppress that.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by