l'image de fond

fr       en

Binary processing

Erosion and Dilatation

Methode

The erosion and dilation both use a logical operation between all the pixels defined by a mask. For erosion, we make an AND between the pixels. This operation aims to return a 1 only if all the pixels in the mask are 1 otherwise it returns a 0. So the result will show white only if the surface of the mask is completly white on the original image. So it comes eroded the white parts of the image. For dilatation, we make an OR between the pixels. If one or more pixels in the mask is 1 then the output pixel is 1. The white areas are stretched. The white parts become more important. Of course, it is possible to apply these operations in the black part, use a NOR for erosion and NAND for dilation.

Program

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        

Example

An example of erosion of a square mask of order 3 (49 pixels)
erosion dilatation d'une image
Here, the dilatation for the same image with same mask.
erosion dilatation d'une image

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