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

2 次查看(过去 30 天)
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)

回答(2 个)

Image Analyst
Image Analyst 2019-12-3
Use entropy():
H = entropy(I)

Walter Roberson
Walter Roberson 2019-12-3
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));

类别

Help CenterFile Exchange 中查找有关 Biomedical Imaging 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by