Skip to content

Commit

Permalink
Now handles div by 0 properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Byrne committed Feb 1, 2024
1 parent ddc801b commit 8cc9d0b
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions agrinet/utils/EnviroIndices.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@

def get_ndvi(inp: np.ndarray, re: np.ndarray) -> np.ndarray:
# NDVI = (NIR - Red) / (NIR + Red)
return (re[:, :, 0] - inp[:, :, 2]) / (re[:, :, 0] + inp[:, :, 2])
denominator = np.where(
re[:, :, 0] + inp[:, :, 2] != 0, re[:, :, 0] + inp[:, :, 2], 1e-10
)
return (re[:, :, 0] - inp[:, :, 2]) / denominator


def get_ndwi(inp: np.ndarray, re: np.ndarray) -> np.ndarray:
# NDWI = (Green - NIR) / (Green + NIR)
return (inp[:, :, 1] - re[:, :, 2]) / (inp[:, :, 1] + re[:, :, 2])
denominator = np.where(
inp[:, :, 1] + re[:, :, 2] != 0, inp[:, :, 1] + re[:, :, 2], 1e-10
)
return (inp[:, :, 1] - re[:, :, 2]) / denominator


def threshold_ndvi(ndvi: np.ndarray) -> np.ndarray:
Expand Down

0 comments on commit 8cc9d0b

Please sign in to comment.