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

  1. function [A, T] = EntropyThresholding(img)
  2.  
  3. [h, ~] = imhist(img);
  4. h = h/sum(h); % Normalize the histogram so that it sums to 1.
  5. entropies = zeros(256, 1); % Intialize array for storing entropies.
  6. for t = 1:254
  7. White = h(1:t);
  8. Black = h(t+1:255);
  9. % Add 0.001 to prevent division by zero(nan) and log of zero(-inf).
  10. HB = sum((Black/(0.001+sum(Black))).*log((Black+0.001)/(0.001 +sum(Black))));
  11. HW = sum((White/(0.001+sum(White))).*log((White+0.001)/(0.001 +sum(White))));
  12. entropies(t) = HB+HW;
  13. end
  14. [~, T] = max(abs(entropies)); % The Maximal entropy determines the threshold.
  15. T = T - 1;
  16. 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