Montag, 23. November 2015

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 versionand 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']);


Keine Kommentare:

Kommentar veröffentlichen