To perform this treatment, we just have to move the pixel values ??of a defined number of pixels vertically
(up or down) and horizontally (left or right). The operation is simple because it just reassigns the values of
the pixels to another position.
img2(i,j) = img(i+bas,j+droite);
Here, for each pixel of the new image img2 we assign a pixel located at x1 pixels lower and x2 pixels more right (x can be negative). A simple test must be done to see if the pixel to be assigned exists. If not (it is not part of the original image) then the new pixel is set to 0 if we want to make a black part or if we prefer a 1 for white.
function guitranslation
clear all;
close all;
figure( 'name','translation',...
'NumberTitle','off',...
'MenuBar','none');
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','droite',...
'Min' ,-100,'Max',100, ...
'Position', [70 10 100 20],...
'Value', 0,...
'SliderStep',[0.01 0.1], ...
'callback',{@translation,'bas'});
text(1)=uicontrol( 'style','text',...
'string','translation droite ',...
'Position', [70 30 100 20]);
uicontrol( 'style','slider',...
'string','bas',...
'Min' ,-100,'Max',100, ...
'Position', [180 10 100 20],...
'Value', 0,...
'SliderStep',[0.01 0.1], ...
'callback',{@translation,'droite'});
text(2)=uicontrol( 'style','text',...
'string','translation bas ',...
'Position', [180 30 100 20]);
%parametre initial
setappdata(gcf,'k',8);
setappdata(gcf,'x',1);
setappdata(gcf,'bas',0);
setappdata(gcf,'droite',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);
bas = getappdata(1,'bas');
droite = getappdata(1,'droite');
set( text(1), 'String', bas );
set( text(2), 'String', droite );
img = getappdata(1,'x');
k = getappdata(1,'k');
[ligne colonne]=size(img);
img2=uint8(zeros(ligne,colonne));
bas = round(ligne*bas/100);
droite = round(colonne*droite/100);
for i=1:ligne
for j=1:colonne
if ( (i+bas)>0 && (i+bas)<=ligne && (j+droite)>0 && (j+droite)<=colonne )
img2(i,j) = img(i+bas,j+droite);
else
img2(i,j)=0;
end
end
end
axes(a(2))
imshow(img2)
end
end
Copyright © 2010-2014, all rights reserved, contact: operationpixel@free.fr