Home > Software design >  Normalized index between two arrays gives different result when calculate manually
Normalized index between two arrays gives different result when calculate manually

Time:11-03

I have two-d arrays with the same shape:

b8
>>>array([[42,42,42,...,43,37,47],  
[41,41,40,...,44,33,34],...],dtype=uint8)

b11
>>>array([[77,77,77,...,41,41,42],
[77,77,77,...,41,41,42],...] dtype=uint8)

I want to use the normalized difference index, to create new array. The normalized index should be caluclated based on this formula:

(b8-b11)/(b8 b11)

and it should produce new array with values between -1 to 1. However, I have realized that I get unexcpected values when I try to calcualte it this way:

(b8-b11)/(b8 b11)
>>>
array([[1.85714286, 1.85714286, 1.85714286, ..., 0.02380952, 3.23076923,
        0.05617978],
       [1.86440678, 1.86440678, 1.87179487, ..., 0.03529412, 3.35135135,
        3.26315789],...

The 1.857 and the other numbers seems to be wrong, when I calculate manually the bottom left pixels I get different result:

(42-77)/(42 77)
>>>-0.29411764705882354

I believe that there is very basic concept that I'm missing here with the matrices, I would like to understand my mistake and to be able to get the desired results.

CodePudding user response:

The problem is uint8, which is unsigned 8-bit. So your subtraction overflows:

np.uint8(42) - np.uint8(77)
# 221

CodePudding user response:

If you can guarantee that the values in b8 and b11 are small enough that any addition will not exceed 256, then you can get around this by exploiting the promotion to float64 upon division prior to subtraction:

denom = b8 b11 

ans = b8/denom - b11/denom

If overflow/underflow is still a risk at any step, then you will have to convert to a larger integer type or a float type sooner. Floats are very difficult to overflow.

  • Related