Problems with Matlab Projects? You may face many Problems, but do not worry we are ready to solve your Problems. All you need to do is just leave your Comments. We will assure you that you will find a solution to your project along with future tips. On Request we will Mail you Matlab Codes for Registered Members of this site only, at free service...Follow Me.

Image Rotation in MATLAB - Examples without imrotate function


        We can develop our own code to rotate an Image.In this article, I have discussed about the built in functions and the code to rotate an image without using imrotate function. At the end of the article you can find the 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.

0 comments:

Post a Comment

Recent Comments

Popular Matlab Topics

Share your knowledge - help others

Crazy over Matlab Projects ? - Join Now - Follow Me

Sites U Missed to Visit ?

Related Posts Plugin for WordPress, Blogger...

Latest Articles

Special Search For Matlab Projects

MATLAB PROJECTS

counter

Bharadwaj. Powered by Blogger.