Wednesday, 13 February 2013

Entropy-based histogram thresholding

I read about entropy thresholding[1] and I wanted to give it a try. This technique was rather simple to implement in Matlab compared to other more complex methods and performed reasonably (see results).

Code

function [A, T] = EntropyThresholding(img)

[h, ~] = imhist(img);
h = h/sum(h); % Normalize the histogram so that it sums to 1.
entropies = zeros(256, 1); % Intialize array for storing entropies.
for t = 1:254
    White = h(1:t);
    Black = h(t+1:255);
    % Add 0.001 to prevent division by zero(nan) and log of zero(-inf).
    HB =  sum((Black/(0.001+sum(Black))).*log((Black+0.001)/(0.001 +sum(Black))));
    HW =  sum((White/(0.001+sum(White))).*log((White+0.001)/(0.001 +sum(White))));
    entropies(t) = HB+HW; 
end
[~, T] = max(abs(entropies)); % The Maximal entropy determines the threshold.
T = T - 1;
A = img > T;

Results

References

1. J.N. Kapur, P.K. Sahoo and A.K.C. Wong, "A New Method for Gray-Level Picture Thresholding Using the Entropy of the Histogram", CVGIP, (29), pp.273-285 , 1985.

2 comments:

  1. heyy
    nice code..even i am working on entropic thresholding on retinal blood vessels...could u please help me

    ReplyDelete
  2. sir
    I am working on multilevel thresholding by kapur method..can u help me with some code

    ReplyDelete