The answer is NaN

2 次查看(过去 30 天)
Huy
Huy 2013-12-25
评论: Huy 2013-12-26
I don't know why the answer of this code is NaN. Help me please. thanks so much
for a = 01:20
filename = [num2str(a,'%02d') '_training' '.tif'];
img = imread(filename);
img1=imrotate(img,90,'bilinear');
C=rgb2gray(img1);
%co-occurrence matrix
glcm=graycomatrix(C,'GrayLimits',[0 255],'NumLevels',256,'Offset',[-1 -1]);
p=glcm/sum(glcm(:));
entropy=0;
for i=1:256
for j =1:256
entropy=entropy-p(i,j)*log2(p(i,j));
end
end
kq(a,:)=[entropy];
end

采纳的回答

Matt J
Matt J 2013-12-25
What prevents p(i,j) from being 0 in
entropy=entropy-p(i,j)*log2(p(i,j));
Note
>> 0*log(0)
ans =
NaN
  3 个评论
Matt J
Matt J 2013-12-25
编辑:Matt J 2013-12-25
if p(i,j)==0
blablabla
end
or, discarding the for-loops,
entropy=p.*log(p);
entropy(isnan(entropy)) = blablabla
Huy
Huy 2013-12-25
thanks so much

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2013-12-25
Often a small number is added to values before taking log
smallNumber = 1; % % Whatever...
p(p==0) = smallNumber;
Now do your log.

类别

Help CenterFile Exchange 中查找有关 Simulink Environment Customization 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by