Calculation of entropy shows " the entropy of the image is = NaN", please help

10 views (last 30 days)
Img = imread('test1.jpg');
I=rgb2gray(Img);
[Height,Width] = size(I);
[m,Binsx] = imhist(I);
m = m/(Height*Width);
sprintf('the sum of the histogram value is = %g',sum(m));
figure,plot(Binsx,m,'k')
xlabel('pixel value'),ylabel('relative count')
H = sum(-m.*log2(m));
sprintf('the entropy of the image is = %g',H)

Answers (2)

Image Analyst
Image Analyst on 3 Dec 2019
Use entropy():
H = entropy(I)

Walter Roberson
Walter Roberson on 3 Dec 2019
H = sum(-m.*log2(m));
The problem with the code is that you have some entries which are 0. You should only be calculating based upon the non-zero entries.
mp = m(m>0);
H = sum(-mp.*log2(mp));

Categories

Find more on Biotech and Pharmaceutical in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!