l'image de fond

fr       en

Binary processing

edge detection

Methode

The edge detection is to detect the change of neighbor pixel from 1 to 0 or vise versa. This detection is performed with the logical operator XOR. This operator returns 1 if its two inputs are different. In this way, if there was a change between the two input pixels then the output pixel will be white, otherwise it will be black.

e1e2s
000
011
101
110

Now you must choose the correct input pixels of the XOR gate. Look at the diagram below.
frontmasque1
To see the edges in several directions, it is important to make many operations. For a square mask of order 1, we can detect edges in four directions. The direction is defined by the line between the two input pixels of XOR gates.

If the mask does not provide every direction, the edge risks to be mis identify. The image must be very few parasites to not detect edge due to noise.
If one or more front is detected in a mask then the output must be equal to 1, so we must use an OR between all the outputs of XOR. output = 1 if (n1 XOR n1')=1 OU (n2 XOR n2')=1 OU (n3 XOR n3')=1 OU (n4 XOR n4')=1, otherwise output = 0

Program

function guifront

%translationfunction guihistogramme
clear all;
close all;
figure(  'name','majorité',...
            'NumberTitle','off',...
            'color',[0.3137 0.3137 0.5098]);
       
a(1)=axes('units','normalized',...
    'position',[0.05 0.60 0.35 0.35]);
a(2)=axes('units','normalized',...
    'position',[0.05 0.15 0.35 0.35]);
a(3)=axes('units','normalized',...
    'position',[0.50 0.60 0.35 0.35]);
a(4)=axes('units','normalized',...
    'position',[0.50 0.15 0.35 0.35]);
uicontrol(  'style','pushbutton',...
            'string','load',...
            'Position', [10 10 50 20],...
            'callback',@loadimage);
uicontrol(  'style','pushbutton',...
            'string','majorité',...
            'Position', [100 10 50 20],...
            'callback',@majorite);
uicontrol(  'style','pushbutton',...
            'string','front',...
            'Position', [400 10 50 20],...
            'callback',@front);
uicontrol(  'style','popup',...
            'string','ligne|colonne|croix|carre',...
            'Min',0,'Max',4,...
            'Position', [200 10 50 20],...
            'callback',{@choix,'typemasque'});
uicontrol(  'style','popup',...
            'string','1|2|3|4|5',...
            'Min',0,'Max',4,...
            'Position', [300 10 50 20],...
            'callback',{@choix,'ordrefiltre'});
uicontrol(  'style','popup',...
            'string','1|2|3|4|5',...
            'Min',0,'Max',4,...
            'Position', [500 10 50 20],...
            'callback',{@choix,'ordrefront'});
text(1)=uicontrol(  'style','text',...
            'string','ordre majorite ',...
            'Position', [300 30 70 20]);
text(2)=uicontrol(  'style','text',...
            'string','odre front ',...
            'Position', [500 30 70 20]);
%parametre initial
setappdata(gcf,'k',8);
setappdata(gcf,'x',1);
setappdata(gcf,'img2',1);
setappdata(gcf,'typemasque',0);
setappdata(gcf,'ordrefiltre',0);
setappdata(gcf,'ordrefront',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
    img2=uint8(x);
    axes(a(2))
    imshow(img2)
    x=x/255;
    img2=img2/255;
    setappdata(1,'k',k);
    setappdata(1,'x',x);
    setappdata(1,'img2',img2);
   
end
function majorite(hObj,~)
     typemasque = getappdata(1,'typemasque');
     ordre = getappdata(1,'ordrefiltre');
     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
     if (typemasque==0 || typemasque==1)
         taillemasque = 1 + 2*ordre;
     elseif typemasque==2
         taillemasque = 1 + 2*ordre;
     elseif typemasque==3
         taillemasque = 1 + 4*ordre;
     else
         taillemasque = ((ordre*2)+1)^2;
     end
     seuilmajorite=ceil(taillemasque/2);
     for i = 1 : ligne
         for j = 1 : colonne
             if (img2(i,j)>seuilmajorite)
                 img2(i,j)=1;
             else
                 img2(i,j)=0;
             end
         end
     end
     setappdata(1,'img2',img2);
    axes(a(2))
    imshow(img2)
end
function front(hObj,~)
     typemasque = getappdata(1,'typemasque');
     ordre = getappdata(1,'ordrefront');
     if (ordre == 0 || ordre == 1)
         ordre=1;
     end
     x = getappdata(1,'x');
     img2 = getappdata(1,'img2');
     k = getappdata(1,'k');
     [ligne colonne]=size(img2);
     x2=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
                 x2(i,j)=xor(x(i-ordre,j) , x(i+ordre,j));% detection horisontal
             elseif typemasque==2
                 x2(i,j)=xor(x(i,j-ordre) , x(i,j+ordre));% detection vertical
             elseif typemasque==3
                 x2(i,j)=xor(x(i-ordre,j) , x(i+ordre,j)) |... % detection horisontal
                           xor(x(i,j-ordre) , x(i,j+ordre));     % detection vertical
             else
                 x2(i,j)=xor(x(i-ordre,j) , x(i+ordre,j)) |... % detection horisontal
                           xor(x(i,j-ordre) , x(i,j+ordre)) |... % detection vertical
                           xor(x(i-ordre,j-ordre) , x(i+ordre,j+ordre)) |... % detection diagonal decendante
                           xor(x(i-ordre,j+ordre) , x(i+ordre,j-ordre));     % detection diagonal montante
             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
                 img3(i,j)=xor(img2(i-ordre,j) , img2(i+ordre,j));% detection horisontal
             elseif typemasque==2
                 img3(i,j)=xor(img2(i,j-ordre) , img2(i,j+ordre));% detection vertical
             elseif typemasque==3
                 img3(i,j)=xor(img2(i-ordre,j) , img2(i+ordre,j)) |... % detection horisontal
                           xor(img2(i,j-ordre) , img2(i,j+ordre));     % detection vertical
             else
                 img3(i,j)=xor(img2(i-ordre,j) , img2(i+ordre,j)) |... % detection horisontal
                           xor(img2(i,j-ordre) , img2(i,j+ordre)) |... % detection vertical
                           xor(img2(i-ordre,j-ordre) , img2(i+ordre,j+ordre)) |... % detection diagonal decendante
                           xor(img2(i-ordre,j+ordre) , img2(i+ordre,j-ordre));     % detection diagonal montante
             end
         end
     end
    axes(a(3))
    imshow(x2)
    axes(a(4))
    imshow(img3)
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

Edge detection for a binary image with majority, the picture is much more noisy.
detection de mouvement de front d'une image
Same detection with other parameters which leaves less noise.
detection de mouvement de front d'une image

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