l'image de fond

fr       en

Binary processing

Opening and Closing

Methode

To perform these programs, we need to know the erosion and dilation for the opening and closing, we must use them.
Closing is made by a dilation followed by an erosion of the original image. closing (f) = erosion (dilation (f)).
Opening is made by an erosion followed by a dilatation of the original image. opening(f) = dilatation(erosion(f)).

Program

function guiouverturefermeture
%translationfunction guihistogramme
clear all;
close all;
figure(  'name','ouverture fermeture',...
            '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','ouverture',...
            'Position', [100 10 50 20],...
            'callback',@ouverture);
uicontrol(  'style','pushbutton',...
            'string','fermeture',...
            'Position', [200 10 50 20],...
            'callback',@fermeture);
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 ouverture(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
     img3=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
                     img3(i,j)=img3(i,j) & img2(i+o,j);
                 end
             elseif typemasque==2
                 for o = -ordre : 1 : ordre
                     img3(i,j)=img3(i,j) & img2(i,j+o);
                 end
             elseif typemasque==3
                 for o = -ordre : 1 : ordre
                      img3(i,j)=img3(i,j) & img2(i+o,j) & img2(i,j+o);
                 end
             else
                 for o1 = -ordre : 1 : ordre
                     for o2 = -ordre : 1 : ordre
                         img3(i,j)=img3(i,j) & img2(i+o1,j+o2);
                     end
                 end
             end
         end
     end
    axes(a(2))
    imshow(img3)
end
function fermeture(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
     img3=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
                     img3(i,j)=img3(i,j) | img2(i+o,j);
                 end
             elseif typemasque==2
                 for o = -ordre : 1 : ordre
                     img3(i,j)=img3(i,j) | img2(i,j+o);
                 end
             elseif typemasque==3
                 for o = -ordre : 1 : ordre
                         img3(i,j)=img3(i,j) | img2(i+o,j) | img2(i,j+o);
                 end
             else
                 for o1 = -ordre : 1 : ordre
                     for o2 = -ordre : 1 : ordre
                         img3(i,j)=img3(i,j) | img2(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

Here the opening with a square mask of degree 2
ouverture fermeture d'une image
And the closing with the same parameters.
ouverture fermeture d'une image

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