why am i getting NaN from my function
조회 수: 1 (최근 30일)
이전 댓글 표시
tomer orenshtein
2020년 1월 26일
댓글: tomer orenshtein
2020년 1월 26일
i was asked to write a function that gets b&w image and returns its entropy. im not allowed to use im_hist or entrtopy (obviously).
this is the function i wrote:
function [Entropy] = EntropyCalc(image)
image_hist = histogram(image);
values = image_hist.Values;
[size_x, size_y] = size(image);
total = size_x * size_y;
p = values./total;
Entropy = - sum(p.*log2(p));
end
but i keep getting NaN.
any ideas??
thanks!
댓글 수: 2
채택된 답변
Thiago Henrique Gomes Lobato
2020년 1월 26일
As David said your log2(p) is not safe, you can solve this problem by using this line of code:
Entropy = - sum(p.*log2(p+eps));
댓글 수: 3
Thiago Henrique Gomes Lobato
2020년 1월 26일
편집: Thiago Henrique Gomes Lobato
2020년 1월 26일
The difference is in the histogram function, you need to explicitely define the number of bins to 256 (make sure image is uint8) so it has the same result.
image_hist = histogram(image,256);
Or faster:
values = imhist(image(:));
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Descriptive Statistics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!