function Topt = kapur(a,h)

H=cumsum(h);

e=zeros(length(H),1);

for T=1:256

    for i=1:T
        e(T) = e(T) - ( h(i)/(H(T)+eps) * log((h(i)+eps)/(H(T)+eps)) );
    end
    
    for i=(T+1):256
        e(T) = e(T) - ( h(i)/(H(T)+eps) * log((h(i)+eps)/(H(T)+eps)) );
    end

    
end

Topt = find(e==max(e));