File Exchange

image thumbnail

Rotate an image around a point

version 1.3.0.0 (2.28 KB) by Jan Motl
Performs rotation of an image around any point inside of the image.

16 Downloads

Updated 27 Oct 2014

View License

The rotation around a point is performed in following sequence:
1) Padding of the image.
2) Rotation of the image around the image's center with IMROTATE.
3) Cropping of the image.

Cite As

Jan Motl (2020). Rotate an image around a point (https://www.mathworks.com/matlabcentral/fileexchange/40469-rotate-an-image-around-a-point), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (23)

Jan Motl

Hi Roos, you may try your luck with one of many reimplementations of IMROTATE here on File Exchange. Just make sure that in the end the chosen IMROTATE and ROTATE_AROUND work together (e.g.: with the test file distributed with ROTATE_AROUND).

Roos

Hello Jan,
Thank you very much for your prompt response!
Just looked and I'm afraid not. That explains why the function does not work.
Do you happen to know a solution on top of your head without using this function?

Jan Motl

Roos, do you have "Image Processing Toolbox" (i.e.: is it listed in the output of "ver" function)? This function relies on its presence.

Roos

Hello!
For a project I need my code to do this!
But the rotateAround function does not work in my version (R2019a).
I cannot find what it has changed and how to do it now.
Can anyone help?

Jan Motl

Yemini, that is correct. Support for 'loose' method is not implemented. A workaround: pad the image with padarray(). And only then rotate the image around the (shifted) point.

dror yemini

what about = rot = imrotate(padded, angle, method, 'loose');
then in draw the point is not in the right location

Jithin Kunnummal

Lance

very good code. solve my problem

mohammad eslami

Georg Lousy

Fantastic work!

ALEXEY SHASHKOV

Zainab Un

Thank you...wonderful work

sara

Bauyrzhan Aubakir

Jan Motl

Thanks Maider for the comment.

Regarding the small angle problem, I believe that for small angles (like 0.1) and small images (like 300x200) rotateAround should give images identical with the source images (and also identical with the results from imrotate).

Only for bigger images (like 1000x1000) or bigger angles (like 1) the results should be different.

Regarding the loosing of pixels. If I rotate an image by 0 degrees, I should always get the original image (regardless of the point of rotation or image size). With round function, I am not loosing any pixel on testing machines. With floor function, I am loosing pixels.

But it is possible, that internals of imrotate can differ in rounding.

Nevertheless, the function as it is behaves correctly in MATLAB R2012b and MATLAB R2014a.

Ehsan

It's Really work!
Thank you, your code solve a major problem in my project.............

Maider

I meant to change lines 35 and 36 in rotateAround from
shiftX = round(pointX-(centerX+newX));
shiftY = round(pointY-(centerY-newY));
to
shiftX = floor(pointX-(centerX+newX));
shiftY = floor(pointY-(centerY-newY));

Maider

Thanks for this submision. This is a very clever way to use imrotate.
But the function can be improved. For example:
1. If you use rotateAround_test and use pointX = 1; and pointY = 1; and run, the result shows that position (1,1) and (2,1) are black as they belong to the lost pixels in the rotation, that should not happen.
2. It does not work well with small angles such as 0.1. In such case rotateAround produces a rotation identical to imrotate.
Running
%% Set test parameters
imageX = 300;
imageY = 200;
pointX = 1;
pointY = 1;
angle = 0.1;

%% Create test image - black dot on white field
image = ones(imageY, imageX);
image(pointY, pointX) = 0;
rot = imrotate(image,angle,'bilinear', 'crop');

%% Rotate the image around the black dot
rotated = rotateAround(image, pointY, pointX, angle,'bilinear');

>> norm(rot - rotated)

ans =

0

I suggest to change lines 35 and 36 in rotateAround from
shiftX = round(pointX-(centerX+newX));
shiftY = round(pointY-(centerY-newY));
to
centerX = floor(imageWidth/2+1);
centerY = floor(imageHeight/2+1);

That solved both problems for me.

Gaurav Mittal

Thanks, works great.

Yun Inn

Jan Motl

Thanks for suggestion. I swapped the parameters of the function call.

Juan P. Viera

Really useful. I think you should change the rotation line:
rot = imrotate(padded,angle,'crop',method);
for
rot = imrotate(padded,angle,method,'crop');

jamesyu92

Works great for me.

Updates

1.3.0.0

Removed unnecessary Mac metainformation.

1.2.0.0

Removed dependency on deg2rad function.

1.1.0.0

Extended support from grayscale to RGB images. Swapped order of the parameters in imrotate function call.

MATLAB Release Compatibility
Created with R13
Compatible with any release
Platform Compatibility
Windows macOS Linux