Thursday, April 30, 2015

Rotate Image Algorithm matlab implementation

function [R] = RotateIm(A,angle)
A=padarray(A,[50,50]);
angle=-angle*pi/180; %to make the positive rotate clock wise like required
Tinv=[cos(angle) sin(angle);-sin(angle) cos(angle)];
R=zeros(size(A,1),size(A,2));
px=(size(A,1))/2;
py=(size(A,2))/2;
for m = 1:size(A,1)-1
    for n = 1:size(A,2)-1
        x=Tinv*[m-px;n-py]+[px;py];
        I=floor((x(1,1)));
        K=floor((x(2,1)));
        a=x(1,1)-I;
        b=x(2,1)-K;
       
       
       %  R(m,n)=R(m,n)+(1-a)*(1-b)*A(I,K)+a*(1-b)*A(I+1,K)+(1-a)*b*A(I,K+1)+a*b*A(I+1,K+1);
         if(I>=1&&I<=size(A,1)&&K<=size(A,2) && K >=1 )
             R(m,n)=R(m,n)+(1-a)*(1-b)*A(I,K);
         end
        if(I<size(A,1)&&I>=1 && K >=1 && K <= size(A,2))
          R(m,n)=R(m,n)+  a*(1-b)*A(I+1,K);
        end
       
        if(I>=1 && I<=size(A,1) && K >=1&& K < size(A,2) )
          R(m,n)=R(m,n)+   (1-a)*b*A(I,K+1);
        end
        if(I<size(A,1) &&I>=1&&K>1&& K < size(A,2) )
              R(m,n)=R(m,n)+   a*b*A(I+1,K+1);
        end
    end
end
[MaxRow,MaxCol] = find(R,1,'last');

[MinRow,MinCol] = find(R,1,'first');

R=R(min(MinRow,MinCol):max(MaxRow,MaxCol),min(MinRow,MinCol):max(MaxRow,MaxCol));

R=uint8(R);

end

No comments:

Post a Comment