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

Matt J
Matt J 2013년 12월 25일
What variable represents "the answer"?

댓글을 달려면 로그인하십시오.

 채택된 답변

Matt J
Matt J 2013년 12월 25일

0 개 추천

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

Huy
Huy 2013년 12월 25일
how can i fix it? how can i overcome this problem?
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일

0 개 추천

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

질문:

Huy
2013년 12월 25일

댓글:

Huy
2013년 12월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by