Weird Calculation difference using uint16 and double for an image

19 views (last 30 days)
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)

Accepted Answer

Steven Lord
Steven Lord on 30 Oct 2019
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 Comments
Jason
Jason on 30 Oct 2019
Edited: Jason on 30 Oct 2019
OK, so I should use a double as it doesn't ignore negative values.
Steven Lord
Steven Lord on 30 Oct 2019
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!