MATLAB Answers

Get confused (twice) with entropy of grayscale image

10 views (last 30 days)
pavel on 9 Sep 2013
First, the quote from help says: "entropy converts any class other than logical to uint8 for the histogram count calculation"
I did following 2 tests:
1) A = 0:255; % here A is double and
entropy(A) gives 0.0369
2) A = uint8(0:255);
entropy(A) gives 8
It looks like it doesn't convert to uint8.
Second, I tried formula -sum(p.*log2(p))
[p,x] = imhist(A);
And then
-sum(p.*log2(p)) gives 0, which is obvious because histogram of the given A makes 256 bins with 1 in every bin, and log2(1) = 0.
What was wrong in my tests?
Thanks, Pavel
  1 Comment
pavel on 9 Sep 2013
No more confusion! The answers are in the "entropy" code!

Sign in to comment.

Answers (1)

Jeff E
Jeff E on 9 Sep 2013
This boils down to the way IMHIST calculates the bins for an image of type double. For some (all?) functions, Matlab assumes pixel values of this type fall within a range of zero to one. IMTOOL, for example, scales its display to this range as a default. IMHIST does the same, as can be seen if you call it directly on your vector A:
A_counts = imhist(A);
you will see that all the pixels fall in the highest bin.
Jeff E
Jeff E on 9 Sep 2013
I wouldn't say it's wrong, but the conversion they are performing isn't the one you are expecting. For example, look at the result of :
Again, Matlab assumes an image of type double will fall in the range of zero to one. In this case, given a uint8 image, it essentially performs the conversion:
B_conv = double(B) ./ 255 ;
This remaps the uint8 values of 0-255, into the double range of 0-1.
>> entropy(0:255)
ans =
>> entropy(uint8(0:255))
ans =
>> entropy(im2double(uint8(0:255)))
ans =

Sign in to comment.


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!