l'image de fond

fr       en

Binary processing

Majority

Methode

This time, the operation is still done on the pixels contained within the mask, but the result is given by the largest number of value 1 or 0. If there is a majority of 1 then the resulting pixel will be 1, if 0 are majority then the pixel will be 0. The operation is rather simple, we count the number of pixels at 1 contained in the mask if it is bigger than one-half of the number of pixels of the mask then the resulting pixel is set to 1 if not it is put to 0.

Program

function guimajorite

%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.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','majorité',...
            'Position', [100 10 50 20],...
            'callback',@majorite);
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 majorite(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
     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
     
    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

Majority with cross mask of degree 2
filtre majorité d'une image
Majority with square mask of degree 3, the majorities of wrong have benn removed.
filtre majorité d'une image

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