function [kappa_min,Ibin] = binarisation(I,N);  
[m,n]      = size(I); L= m*n; cases = [0:N-1]/(N-1); 
Iliste     = reshape(I,[1,L]); [H,indice] = histc(Iliste,cases); 
H=H/L; varintra = [];
for t=2:N-1
    P_moins = sum (H(1:t-1)); 
    P_plus  = 1-P_moins; 
    I_moins = [0 : t-1]/(N-1); 
    I_plus  = [t : N-1]/(N-1);
    if P_moins  == 0  
    moy_moins   = 0; var_moins = 0; 
    else
      moy_moins = sum(I_moins.*H(1:t))/P_moins;
      var_moins = sum((I_moins.^2).*H(1:t))/P_moins -(moy_moins)^2;
    end
    if P_plus   == 0  
    moy_plus    = 0; var_plus = 0; 
    else
      moy_plus  = sum(I_plus.*H(t+1:N))/P_plus; 
      var_plus  = sum((I_plus.^2).*H(t+1:N))/P_plus -(moy_plus)^2;
    end
    v = (P_moins)*(var_moins) + (P_plus)*(var_plus); 
    varintra = [varintra v]; 
end
[minimum,Idx] = min(varintra); kappa_min     = Idx(1);  
Ibin = ceil((sign(I - kappa_min/(N-1)) + ones(m,n))/2);  
end  


