필터 지우기
필터 지우기

Weird Calculation difference using uint16 and double for an image

조회 수: 11 (최근 30일)
Jason
Jason 2019년 10월 30일
댓글: Steven Lord 2019년 10월 30일
Hello, I have a function that calculates the "contrast" of a greyscale image
function FM=CalculateBrenner(Image)
[M N] = size(Image);
DH = Image;
DV = Image;
DH(1:M-2,:) = diff(Image,2,1);
DV(:,1:N-2) = diff(Image,2,2); % second order difference between
% columns, i.e., along x-direction.
FM = max(DH, DV);
FM = FM.^2;
FM = mean2(FM);
end
when my image is a uint16, I get
FM= 100.39
max=438
min=22
But when my image is cast as a double
I get:
FM = 204.14
max=438.00
min= 22.00
So why is the calculation FM different for both cases, yet the values its using are the same (indicated by the max & min)

채택된 답변

Steven Lord
Steven Lord 2019년 10월 30일
d = [1 2 1];
u = uint16(d);
dd = diff(d)
du = diff(u)
The smallest value you can store in an unsigned 16-bit integer is 0. In double 1 - 2 is -1 but in uint16 it is 0.
  댓글 수: 2
Jason
Jason 2019년 10월 30일
편집: Jason 2019년 10월 30일
OK, so I should use a double as it doesn't ignore negative values.
Steven Lord
Steven Lord 2019년 10월 30일
You could use imabsdiff if you have Image Processing Toolbox, or just take the max of A-B and B-A (one will be zero, one may not be) for appopriate pieces A and B of your Image variable.
d = [1 2 1];
u = uint16(d);
max(u([3 2])-u([2 1]), u([2 3])-u([1 2]))
I'll leave that last line for you to generalize.

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

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by