Weird Calculation difference using uint16 and double for an image

14 次查看(过去 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