l'image de fond

fr       en

Gray Level Image Processing

Rotation

Method

Like for the translation, we will assign the value of a pixel in the original image to another coordinate. The associations are defined as follows:


rotation1

Where n are the pixels of the new image and p pixels in the original image.
To make a correct rotation, a point must be defined to be the center of rotation, we use the variables and centrecolonne to define the center. The center will be the middle of the image. In addition, the final image will be larger than the original image, otherwise the image will be truncated. To avoid this we use the variable radius, it contains the modulus of the diagonal of the image. The size of the new image will be radius * radius.


rotation3

Of course, the coordinates must be tested to know if they exist in the original image. If they do not exist, we put the new pixel to zero.

Program

function guitranslation
clear all;
close all;
figure(  'name','translation',...
            'NumberTitle','off',...
            'color',[0.3137 0.3137 0.5098]);
       
a(1)=axes('units','normalized',...
    'position',[0.05 0.4 0.4 0.4]);
a(2)=axes('units','normalized',...
    'position',[0.5 0.4 0.4 0.4]);


uicontrol(  'style','pushbutton',...
            'string','load',...
            'Position', [10 10 50 20],...
            'callback',@loadimage);

uicontrol(  'style','slider',...
            'string','degree',...
            'Min' ,0,'Max',360, ...
            'Position', [70 10 100 20],...
            'Value', 0,...
            'SliderStep',[0.01 0.1], ...
            'callback',{@translation,'degree'});
text(1)=uicontrol(  'style','text',...
            'string','translation droite ',...
            'Position', [70 30 100 20]);

%parametre initial
setappdata(gcf,'k',8);
setappdata(gcf,'x',1);
setappdata(gcf,'degree',0);
     
function loadimage(~,~)
    % appeler quand appui check box
    [filename, pathname] = uigetfile({'*.jpg;*.tif;*.PNG;*.gif;*.bmp','All Image Files';...
          '*.*','All Files' },'mytitle',...
          'C:\Work\myfile.jpg')

    x = imread(filename);
    if (length(size(x))>2)
        x=x(:,:,1);% on prend une seul plan image noir et blanc chaque plan sont egaux
    end
    k = whos('x');
    if k.class == 'uint8'
        k=8;
    end
    setappdata(1,'k',k);
    setappdata(1,'x',x);
    axes(a(1))
    imshow(x)
    axes(a(2))
    imshow(x)
   
end
function translation(hObj,~,name)
     val = round(get(hObj,'Value'));
     setappdata(1,name,val);
     degree = getappdata(1,'degree');
     set( text(1), 'String', strcat(num2str(degree),'°'));
     img = getappdata(1,'x');
     k = getappdata(1,'k');
     
     [ligne colonne]=size(img);
     
     rayon = round((ligne^2+colonne^2)^0.5);
     rayon2 = round(rayon/2);
     centreligne = round(ligne / 2);
     centrecolonne = round(colonne/2);

     img2=uint8(zeros(rayon,rayon));

     degree = degree*2*pi/360;
     for i=1:rayon
       for j=1:rayon
           n1=round(cos(degree)*(i-rayon2) - sin(degree)*(j-rayon2))+ centreligne;
           n2=round(sin(degree)*(i-rayon2) + cos(degree)*(j-rayon2))+ centrecolonne;
           if ( (n1)>0 && (n1)<=ligne && (n2)>0  && (n2)<=colonne )
               img2(i,j) = img(n1,n2);
           else
               img2(i,j)=0;
           end
       end
     end
     axes(a(2))
     imshow(img2)
     
end
end

Example


rotation d'une image

Copyright © 2010-2014, all rights reserved, contact: operationpixel@free.fr