I am running the following code on 2 images:
ndvi = np.divide(img8 - img4, img8+img4)
invalid = (ndvi > 1).any()
if invalid:
print("Stopping Execution")
print(ndvi)
img8
and img4
are 2 images and have all positive values.
ndvi
is (img8-img4)/(img8+img4)
Hence, by definition, all elements of ndvi should be between -1 and 1. But I am getting some values>1
The dtypes of all the variables in this context are 'uint16'
When I check the index of the invalid values, and ran the individual code:
temp = (img8[88][118]-img4[88][118])/(img8[88][118]+img4[88][118])
I got the following warning:
<stdin>:1: RuntimeWarning: overflow encountered in ushort_scalars
The values are: img8[88][118] = 1462 img4[88][118] = 1652
The values themself are not large to result in an overflow, but when array sizes become large overflow happens.