The answer is NaN

4 views (last 30 days)
Huy
Huy on 25 Dec 2013
Commented: Huy on 26 Dec 2013
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
  1 Comment
Matt J
Matt J on 25 Dec 2013
What variable represents "the answer"?

Sign in to comment.

Accepted Answer

Matt J
Matt J on 25 Dec 2013
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 Comments
Matt J
Matt J on 25 Dec 2013
Edited: Matt J on 25 Dec 2013
if p(i,j)==0
blablabla
end
or, discarding the for-loops,
entropy=p.*log(p);
entropy(isnan(entropy)) = blablabla
Huy
Huy on 25 Dec 2013
thanks so much

Sign in to comment.

More Answers (1)

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

Categories

Find more on Simulink Environment Customization 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!