Lets Learn together... Happy Reading

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

Image Rotation in MATLAB - Examples without imrotate function – Part 2


To flip an image left to right without using ‘fliplr’
Grayscale:
 A = imread('cameraman.tif');
 LR = A(1:end,end:-1:1);
 figure, subplot(1,2,1);imshow(A);title('original');         
         subplot(1,2,2);imshow(LR);title('Flipped left to right');



RGB Image:
RGB = imread('peppers.png');
 LR  = RGB(1:end,end:-1:1,:);
 figure, subplot(1,2,1);imshow(RGB);title('original');         
         subplot(1,2,2);imshow(LR);title('Flipped left to right');



To flip an image upside down without using ‘flipud’
Gray Scale:
 A = imread('liftingbody.png');
 UD = A(end:-1:1,1:end);
 figure, subplot(1,2,1);imshow(A);title('original');         
         subplot(1,2,2);imshow(UD);title('Upside Down');


RGB Image:
 UD = RGB(end:-1:1,1:end,:);
 figure, subplot(1,2,1);imshow(RGB);title('original');         
         subplot(1,2,2);imshow(UD);title('Upside Down');


MATLAB code to rotate a gray scale Image:
A = imread('cameraman.tif');


%Specify the degree
deg = 35;



%Find the midpoint
if(deg > 155)
    midx = ceil((size(A,1))/2);
    midy = ceil((size(A,2))/2);
else
    midx = ceil((size(A,2))/2);
    midy = ceil((size(A,1))/2);
end

[y,x] = meshgrid(1:size(A,2), 1:size(A,1));
[t,r] = cart2pol(x-midx,y-midy);
t1 = radtodeg(t)+deg;

%Convert from degree to radians
t = degtorad(t1);

%Convert to Cartesian Co-ordinates
[x,y] = pol2cart(t,r);

%Add the mid points to the new co-ordinates
tempx = round(x+midx);
tempy = round(y+midy);

if ( min(tempx(:)) < 0 )
   
newx = max(tempx(:))+abs(min(tempx(:)))+1;
tempx = tempx+abs(min(tempx(:)))+1;
else
    newx = max(tempx(:));
end

if( min(tempy( : )) < 0 )
   
newy = max(tempy(:))+abs(min(tempy(:)))+1;
tempy = tempy+abs(min(tempy(:)))+1;
else
    newy = max(tempy(:));
end
tempy(tempy==0) = 1;
tempx(tempx==0) = 1;

C = uint8(zeros([newx newy]));


for i = 1:size(A,1)
    for j = 1:size(A,2)
        C(tempx(i,j),tempy(i,j)) = A(i,j);
       
    end
  
end

figure,imshow(C);

Output = C;
%FILL THE HOLES OR GAPS-NEAREST NEIGHBOR
for i = 2:size(C,1)-1
    for j = 2:size(C,2)-1
       
        temp = C(i-1:i+1,j-1:j+1);
        if(temp(5)==0&&sum(temp(:))~=0)
            pt = find(temp~=0);
           
            [~,pos] = sort(abs(pt-5));
            Output(i,j) = temp(pt(pos(1)));
          
        end
       
    end
end
figure,imshow(uint8(Output));


EXPLANATION:
This is a faster implementation of the older version and also the gaps are filled with the nearest neighbor .



MATLAB code to rotate a RGB Image
A=imread('peppers.png');


%Specify the degree
deg=45;

%Find the midpoint
if(deg>155)
    midx = ceil((size(A,1))/2);
    midy = ceil((size(A,2))/2);
else
    midx = ceil((size(A,2))/2);
    midy = ceil((size(A,1))/2);
end

[y,x] = meshgrid(1:size(A,2), 1:size(A,1));
[t,r] = cart2pol(x-midx,y-midy);
t1 = radtodeg(t)+deg;
%Convert from degree to radians
t = degtorad(t1);
%Convert to Cartesian Co-ordinates
[x,y] = pol2cart(t,r);
tempx = round(x+midx);
tempy = round(y+midy);

if ( min(tempx ( : ) ) < 0 )
   
newx = max(tempx(:))+abs(min(tempx(:)))+1;
tempx = tempx+abs(min(tempx(:)))+1;
else
    newx = max(tempx(:));
end

if( min(tempy ( : ) ) < 0 )
   
newy = max(tempy(:)) + abs(min(tempy(:)))+1;
tempy = tempy + abs(min(tempy(:)))+1;
else
    newy = max(tempy(:));
end
tempy(tempy==0) = 1;
tempx(tempx==0) = 1;


C = uint8(zeros([newx newy 3]));





for i = 1:size(A,1)
    for j = 1:size(A,2)
        
        C( tempx(i,j),tempy(i,j) ,:) = A(i,j,:);
      
       
    end
  
end

figure,imshow(C);


Output=C;
%FILL THE HOLES OR GAPS - NEAREST NEIGHBOR
for i = 2:size(C,1)-1
    for j = 2:size(C,2)-1
       
        temp = C(i-1:i+1,j-1:j+1,:);
        if(temp(5)==0 && sum(temp(:))~=0)
           
             pt = find(temp(:,:,1)~=0);
            [~,pos] = sort(abs(pt-5));
           
            temp = C(i-1:i+1,j-1:j+1,1);
            Output(i,j,1) = temp(pt(pos(1)));
           
            temp = C(i-1:i+1,j-1:j+1,2);
            Output(i,j,2) = temp(pt(pos(1)));
           
            temp = C(i-1:i+1,j-1:j+1,3);
            Output(i,j,3) = temp(pt(pos(1)));
        end
       
    end
end


figure,imshow(uint8(Output));title([num2str(deg),'Degree']);


like button Like "IMAGE PROCESSING" page

Image Rotation in MATLAB - Examples without imrotate function

180 degree
                                                   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=radtodeg(t)+deg;
        %Convert from degree to radians
        t=degtorad(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.


Faster Implementation: ImageRotation_part 2
like button Like "IMAGE PROCESSING" page
Next Post Home
Google ping Hypersmash.com