View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Rotate an image around a point

5.0 | 9 ratings Rate this file 52 Downloads (last 30 days) File Size: 2.28 KB File ID: #40469 Version: 1.3
image thumbnail

Rotate an image around a point


Jan Motl (view profile)


22 Feb 2013 (Updated )

Performs rotation of an image around any point inside of the image.

| Watch this File

File Information

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.

Required Products Image Processing Toolbox
MATLAB release MATLAB 6.5 (R13)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (13)

12 Sep 2015 Zainab Un

Thank you...wonderful work

Comment only
15 Jan 2015 sara

sara (view profile)

27 Oct 2014 Bauyrzhan Aubakir

27 Oct 2014 Jan Motl

Jan Motl (view profile)

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.

Comment only
26 Aug 2014 Ehsan

Ehsan (view profile)

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

13 Aug 2014 Maider

Maider (view profile)

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

Comment only
13 Aug 2014 Maider

Maider (view profile)

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.
%% 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 =


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

That solved both problems for me.

19 Jul 2014 Gaurav Mittal

Thanks, works great.

01 May 2014 Yun Inn

21 Apr 2014 Jan Motl

Jan Motl (view profile)

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

Comment only
03 Mar 2014 Juan P. Viera

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

12 Jul 2013 jamesyu92

Works great for me.

21 Apr 2014 1.1

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

01 May 2014 1.2

Removed dependency on deg2rad function.

27 Oct 2014 1.3

Removed unnecessary Mac metainformation.

Contact us