function guierosiondilatation
%translationfunction guihistogramme
clear all;
close all;
figure( 'name','erosion dilatation',...
'NumberTitle','off',...
'color',[0.3137 0.3137 0.5098]);
a(1)=axes('units','normalized',...
'position',[0.05 0.55 0.44 0.44]);
a(2)=axes('units','normalized',...
'position',[0.05 0.10 0.44 0.44]);
uicontrol( 'style','pushbutton',...
'string','load',...
'Position', [10 10 50 20],...
'callback',@loadimage);
uicontrol( 'style','pushbutton',...
'string','erosion',...
'Position', [100 10 50 20],...
'callback',@erosion);
uicontrol( 'style','pushbutton',...
'string','dilatation',...
'Position', [200 10 50 20],...
'callback',@dilatation);
uicontrol( 'style','popup',...
'string','ligne|colonne|croix|carre',...
'Min',0,'Max',4,...
'Position', [300 10 50 20],...
'callback',{@choix,'typemasque'});
uicontrol( 'style','popup',...
'string','1|2|3|4|5',...
'Min',0,'Max',4,...
'Position', [400 10 50 20],...
'callback',{@choix,'ordre'});
%parametre initial
setappdata(gcf,'k',8);
setappdata(gcf,'x',1);
setappdata(gcf,'typemasque',0);
setappdata(gcf,'ordre',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);
axes(a(1))
imshow(x)
if (length(size(x))>2)
x=mean(x,3);% on prend une seul plan image noir et blanc chaque plan sont egaux
end
x=uint8(x);
k = whos('x');
if k.class == 'uint8'
k=8;
end
% on inverse l'image les objets sont noir
x=x/255;
setappdata(1,'k',k);
setappdata(1,'x',x);
end
function erosion(hObj,~)
typemasque = getappdata(1,'typemasque');
ordre = getappdata(1,'ordre');
if (ordre == 0 || ordre == 1)
ordre=1;
end
img = getappdata(1,'x');
k = getappdata(1,'k');
[ligne colonne]=size(img);
img2=ones(ligne,colonne);
for i=ordre+1:ligne-ordre%attention aux debordement
for j =ordre+1:colonne-ordre%attention aux debordement
if (typemasque==0 || typemasque==1) %ligne
for o = -ordre : 1 : ordre
img2(i,j)=img2(i,j) & img(i+o,j);
end
elseif typemasque==2
for o = -ordre : 1 : ordre
img2(i,j)=img2(i,j) & img(i,j+o);
end
elseif typemasque==3
for o = -ordre : 1 : ordre
img2(i,j)=img2(i,j) & img(i+o,j) & img(i,j+o);
end
else
for o1 = -ordre : 1 : ordre
for o2 = -ordre : 1 : ordre
img2(i,j)=img2(i,j) & img(i+o1,j+o2);
end
end
end
end
end
axes(a(2))
imshow(img2)
end
function dilatation(hObj,~)
typemasque = getappdata(1,'typemasque');
ordre = getappdata(1,'ordre');
if (ordre == 0 || ordre == 1)
ordre=1;
end
img = getappdata(1,'x');
k = getappdata(1,'k');
[ligne colonne]=size(img);
img2=zeros(ligne,colonne);
for i=ordre+1:ligne-ordre%attention aux debordement
for j =ordre+1:colonne-ordre%attention aux debordement
if (typemasque==0 || typemasque==1) %ligne
for o = -ordre : 1 : ordre
img2(i,j)=img2(i,j) | img(i+o,j);
end
elseif typemasque==2
for o = -ordre : 1 : ordre
img2(i,j)=img2(i,j) | img(i,j+o);
end
elseif typemasque==3
for o = -ordre : 1 : ordre
img2(i,j)=img2(i,j) | img(i+o,j) | img(i,j+o);
end
else
for o1 = -ordre : 1 : ordre
for o2 = -ordre : 1 : ordre
img2(i,j)=img2(i,j) | img(i+o1,j+o2);
end
end
end
end
end
axes(a(2))
imshow(img2)
end
function choix(hObj,~,name)
% Called when user activates popup menu
val = get(hObj,'Value'); %0|1=ligne, 2=colonne, 3=croix, 4=carre
setappdata(1,name,val);
end
end
Voici un exemple d'érosion d'un masque carré d'ordre 3 (49 pixels)
Et ici la dilatation pour la même image avec le même masque.
Copyright © 2010-2014, tous droits réservés, contact : operationpixel@free.fr