180 degree |

**matlab code**. First I tried to rotate an Image by using built in functions in Matlab.

Matlab built_in function rot90(A,k) can be used to rotate images in 90 degrees.

Here is an example using

**rot90**: Assign K=1 for 90 degree, 2 for 180, 3 for 270 and 4 for 360.
A=imread('flower1.jpg');

figure,imshow(A);

R=rot90(A(:,:,1),1);

G=rot90(A(:,:,2),1);

B=rot90(A(:,:,3),1);

C(:,:,1)=rot90(A(:,:,1),1);

C(:,:,2)=rot90(A(:,:,2),1);

C(:,:,3)=rot90(A(:,:,3),1);

figure,imshow(C);

90 degree |

The output image will be rotated 90 degrees.

Another matlab built_in function

**flipud(A)**can be used to rotate the image 90 degrees. The actual function of flipud is to flip matrix up and down.
A=imread('flower1.jpg');

C=uint8(zeros(size(A)));

imshow(A);

R=flipud(A(:,:,1));

G=flipud(A(:,:,2));

B=flipud(A(:,:,3));

C(:,:,1)=R;

C(:,:,2)=G;

C(:,:,3)=B;

imshow(C);

180 degree |

To flip the image from left to right we can use the function fliplr

Original Image |

A=imread('horse2.jpg');

C=uint8(zeros(size(A)));

imshow(A);

R=fliplr(A(:,:,1));

G=fliplr(A(:,:,2));

B=fliplr(A(:,:,3));

C(:,:,1)=R;

C(:,:,2)=G;

C(:,:,3)=B;

imshow(C);

We can combine both fliplr and flipud to rotate the image (90, 180 , 270 and 360)

Now let’s see how to rotate an image

**without using matlab built in function imrotate**.
Steps to be performed:

a. Find the midpoints of the image.

b. Convert the each pixel co-ordinate to polar co-ordinate.

c. The result of conversion will yield angle and radius.

d. Convert the angle which is in radians into degree by using the function rad2deg(theta)

e. Add the degree value to be rotated to the value obtained in the above step.

f. Now again convert the degree to radian by using rad2deg function

g. Finally, convert to Cartesian co-ordinate by using the function pol2cart (theta,radius)

MATLAB CODE:

A=imread('panda2.jpg');

x1=zeros([size(A,1)*size(A,2) 1]);

x2=zeros([size(A,2)*size(A,1) 1]);

%Specify the degree

deg=90;

%Change the image size

C=uint8(zeros([size(A,1) size(A,2) 3 ]));

m=1;

%Find the midpoint

midx=ceil((size(C,1)+1)/2);

midy=ceil((size(C,2)+1)/2);

for i=1:size(A,1)

i1=i-midx;

for j=1:size(A,2)

%convert from cartesian to polar

[t,r]=cart2pol(i1,j-midy);

%Convert from radians to degree and add the degree value

t1=rad2deg(t)+deg;

%Convert from degree to radians

t=deg2rad(t1);

%Convert to Cartesian Co-ordinates

[x,y]=pol2cart(t,r);

x1(m)=round(x+midx);

x2(m)=round(y+midy);

m=m+1;

end

end

%check whether the values are within the image size.

x1(find(x1 < 1))=1;

x2(find(x2 < 1))=1;

n=1;

for i=1:size(A,1)

for j=1:size(A,2)

C(x1(n),x2(n),:)=A(i,j,:);

n=n+1;

end

end

imshow(C);

I got holes in between when I rotated at 45 degrees. So I used ceil , floor and round function to convert the decimals into whole numbers.

45 degrees |

210 degrees |

NOTE: Here I didn't re-size the image. May be in another post, I will concentrate on both scaling and rotating.

Use Meshgrid to make rotation faster.

Use Meshgrid to make rotation faster.

## 5 comments:

When i rotate an image using imrotate I get a warning like.

Actually i need to rotate a given image and then extract the boundary from it.

Since this error comes am not able to proceed..

can you help

Warning: Image is too big to fit on screen; displaying at 67%

This was so helpful!

tiny update: rad2deg has been replaced by radtodeg in newer versions of MATLAB.

I got some dots in my image when i used this code (image rotation without using imrotate function) so if possible try to post about it....

Image which i used is 'cameraman.tif'...

Como eliminar el fondo negro cuando giras la imagen con el imrotate

when I rotate 10º for example, the image rotated has black points inside de objetc.

What is this?

## Enjoyed Reading? Share Your Views