Filter löschen
Filter löschen

Math operations with Double or Uint8

24 Ansichten (letzte 30 Tage)
Carlo Grillenzoni
Carlo Grillenzoni am 21 Okt. 2023
Bearbeitet: Carlo Grillenzoni am 22 Okt. 2023
Hi Matlab community :-)
I have to perform mathematical operations on a sequence of images Mt with a fixed background landscape Mo.
The images are in Uint8 format, and the transformation mt=(Mt-Mo) provide the moving subjects, with mt(i,j)>0 all i,j,
however, if I perform dt=(double(Mt)-double(Mo)) a different result is obtained, with certain dt(i,j)<0 and dt~=double(mt).
Now, I would like to work with Uint8, but this format does not accept all mathematical transformations, such as log(mt);
in that case, I have to pass at the Double transformation, as log(double(mt)), but this seems to me inconsistent ...
In summary, working with Double or Uint8 provide different numerical results, how to make them consistent?
Thanks for your attention, Carlo
  2 Kommentare
Stephen23
Stephen23 am 21 Okt. 2023
Bearbeitet: Stephen23 am 21 Okt. 2023
"if I perform dt=(double(Mt)-double(Mo)) a different result is obtained, with certain dt(i,j)<0 and dt~=double(mt)."
Clearly some of the values of Mo are greater than the corresponding values in Mt. If you subtract them as UINT8 then those locations will be zero (and discard information), whereas if you subtract them as DOUBLE you will get negative values (and retain information). We cannot tell you what is appropriate for your data processing: is it suitable or even meaningful for your processing/algorithm to have loss of information? How do you expect random people on the internet to answer that, when you do not tell them anything about what you are doing with that data?
"I would like to work with Uint8, but this format does not accept all mathematical transformations, such as log(mt)"
It is a bit odd to require the natural logarithm ... do you really expect log base 2?
In any case, tell me the exact output value and class you expect to get for this very simple piece of code:
log(uint8(3))
"In summary, working with Double or Uint8 provide different numerical results, how to make them consistent?"
How exactly should they be "consistent" ? Do you expect binary floating point operations and integer operations to provide excactly the same results for all arithmetic operations, i.e. be completely indistinguishable? If that were possible, why do you think would computer scientists bother having different numeric classes?
Carlo Grillenzoni
Carlo Grillenzoni am 22 Okt. 2023
Bearbeitet: Carlo Grillenzoni am 22 Okt. 2023
dear Steven, thank you very much for your technical remarks. So, in image processing, it seems better to work with the Double format of arrays: But, is this true even when Negative values of the "pixels" are produced?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Steven Lord
Steven Lord am 21 Okt. 2023
What is the range of numbers you can store in an uint8 array?
[intmin('uint8'), intmax('uint8')]
ans = 1×2
0 255
If the results of computations on uint8 data is outside that range, it saturates at the closest edge of that range. The same holds if you try to convert a value outside the range into uint8.
two = uint8(2)
two = uint8 2
three = uint8(3)
three = uint8 3
three-two % 1 is in range so the result is uint8(1)
ans = uint8 1
two - three % -1 is outside the range so the result is intmin('uint8') = uint8(0)
ans = uint8 0
z = uint8(-999)
z = uint8 0
What's the range of numbers you can store in double?
format longg
[-realmax, realmax]
ans = 1×2
1.0e+00 * -1.79769313486232e+308 1.79769313486232e+308
If the result of a computation would be outside that range, it is either -Inf or Inf.
How can you make them consistent? You could always make sure to subtract the smaller of the two numbers from the larger, to get an absolute difference:
absoluteDifference = @(x, y) max(x, y)-min(x, y);
absoluteDifference(two, three)
ans = uint8
1
absoluteDifference(2, 3)
ans =
1
You are correct that the log function is not defined for the integer types. The general rule is that for functions like log, exp, sin, etc. the type of the output is the same as the type of the input. There are very few integer values whose natural logarithm is also an integer value. So it doesn't make sense to define the function for those types.
log(1)
ans =
0

Weitere Antworten (0)

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by