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

Pixel value may change according to different interpolation method.
Hi Simon,
Thanks. I actually did not apply any interpolation method for the examples I reported. It was just a simple imrotate (A, angle). When I applied the interpolation methods (I tried with all of them), I lost even more pixels as you say.
Simon Chan
Simon Chan on 20 Jan 2022
Edited: Simon Chan on 20 Jan 2022
The function would use the default interpolation method, which is the nearest-neighbor interpolation although you did not assign any method.
So pixel value will be re-assigned.
I see. Is there a way to imrotate without assigning default interpolation methods?
Stephen23
Stephen23 on 20 Jan 2022
Edited: Stephen23 on 20 Jan 2022
"Is there a way to imrotate without assigning default interpolation methods?"
Your question is not clear.
If you specify a method then IMROTATE will use the method that you specify.
If you do not specify a method then IMROTATE will use the default method.
Only you can decide what image rotation method suits your needs.
Yes I see what you mean and it is clear. The problem is that with the default interpolation method I lose pixels when using imrotate (see attached in the original question). If I set an interpolation method I lose pixels too. So my question was: is there any other way to rotate the images without losing pixels?
@Eleonora Montagnani: all such image rotation are lossy (unless you rotate multiples of 90deg).
Consider this binary image:
A = [false,false;true,false];
imshow(A)
Now tell me how you would rotate this image by 45deg, with the following conditions:
  • the output image has 4 pixels
  • no loss of information (i.e. three black, one white pixel with exactly the same pattern).
Please show the output image.
Yeah I see that the pattern changes. Point is that with the intensity, I needed it to be as closer as possible to the original because I need to get the highest intensity point of the image out. Losing info is not ideal but I guess not even too detrimental if the approach is well justified.
Thank you for your answers.
Stephen23
Stephen23 on 20 Jan 2022
Edited: Stephen23 on 20 Jan 2022
You can possibly reduce information loss by increasing the image resolution. Would that be an option for you?
I think so! Although I am not sure whether increasing image resolution would change intensity values too much? I have never done it so I don't know what the outcome would be.
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.
Thank you all!

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).

4 Comments

Problem is imrotate runs on binary images and my aim is then to reinsert intensity values in my images. If pixes are added or lost, I cannot re-instate intensity. Would there be a way to rotate the original images with intensity as opposed to binary images?
Why not apply imrotate directly to the ulterior, non-binary image?
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.
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.
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

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!