File Exchange

image thumbnail

Rotate Image

version 1.0 (3.67 KB) by

Rotate an image in X degrees.

22 Ratings



View License

% rotate_image - rotates an image given inside a matrix by the amount of "degree" counter-clockwise
% using linear interpolation of the output grid points from the back-rotated input points
% in this way, the output image will never have a "blank" point
% Format: [out_image_m,out_ref_points_m] = rotate_image( degree, in_image_m, in_ref_points_m )
% Input: degree - rotation degree in dergees, counter-clockwise
% in_image_m - input image, given inside a matrix (gray level image only)
% in_ref_points_m - points on the image wich their output coordinates will be given
% after the rotation. given format of this matrix is:
% [ x1,x2,...,xn;y1,y2,...,yn]
% Output: out_image_m - the output image
% out_ref_points_m - the position of the input handle points after the rotation.
% this element is given in "in_ref_points_m" exists
% format of the matrix is the same as of "in_ref_points_m"
% NOTE: By definition of rotation, in order to perserve all the image inside the
% rotated image space, the output image will be a matrix with a bigger size.

Call the function with no parameters, to activate the demo

Comments and Ratings (26)


Maja (view profile)

Does this code still exist?
If yes, please send it to me.



Irma (view profile)

great! :)

I think that this is probably a great function, but I keep getting the error:
[rotatedSlice(:,:,imageNumber),out_ref_points_m] = rotate_image( degree, in_image_m, in_ref_points_m )
??? Error using ==> times
Integers can only be combined with integers of the same class, or scalar doubles.

Error in ==> rotate_image at 112
out_image_m(out_points_span) = ...


Kelly (view profile)

this is great, thank you for sharing!


Pearl (view profile)

vaira prakash

Amanda Lin

There is only 1 small suggestion. The images I have are in uint8. Your code at line 112 will give error message. So what I did was convert my image matrix to double and after rotation, convert the output matrix to uint8. So if you can check the input image's data first, do the conversion within your code when necessary, that'll be good.

Steve Sharples

Excellent code, thank you for sharing.

Simone Scolding


David Moyles

Ben hardy

Shamem Ahmed

Very interesting

Simon Robinson

It's a bit of a strange feature that it gives an error for 90, 180, and 270 degree rotations if you don't specify a out_ref_points_m (if you aren't interested in this) but not for other angles, otherwise excellent.

Sudhir Gupta

aa jam

Tim Peace

sulakshana borsune

Avinash .

Extremely useful and well documented code!!!

Sachin Gupta

Excellent!! can u plz give the format of input test cases..


Very useful and well documented: thanks !

Alice Huang


Francisco Lopes

Perfect. Congratulations.


Very Nice!

amit mishra

nice tool; the only problem is that the image expands after rotation, which shd not be the case;and the areas where there is no image in rest of the area; where as there shd be pixels all aroud using extrapolation!!

Meenal Bagla

Could you possibly do this for a 3D matrix.

MATLAB Release
MATLAB 6.1 (R12.1)

Inspired: Automatic Image Registration

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

» Watch video