Shortcuts

# Indices¶

In this tutorial, we demonstrate how to use TorchGeo’s functions and transforms to compute popular indices used in remote sensing and provide examples of how to utilize them for analyzing raw imagery or simply for visualization purposes. Some common indices and their formulas can be found at the following links:

It’s recommended to run this notebook on Google Colab if you don’t have your own GPU. Click the “Open in Colab” button above to get started.

## Setup¶

Install TorchGeo

[ ]:

%pip install torchgeo


## Imports¶

[ ]:

%matplotlib inline
import os
import tempfile

import matplotlib.pyplot as plt

from torchgeo.datasets import EuroSAT100
from torchgeo.transforms import AppendNDVI, AppendNDWI, AppendNDBI


## Dataset¶

We will use the EuroSAT dataset throughout this tutorial. Specifically, a subset containing only 100 images. EuroSAT contains 13-channel multispectral imagery captured by the Sentinel-2 satellite.

[ ]:

root = os.path.join(tempfile.gettempdir(), "eurosat100")
sample = ds[21]


## True Color (RGB) Image¶

We can plot a true color image consisting of the first 3 channels (RGB) to visualize the sample.

[ ]:

ds.plot(sample)
plt.show()
plt.close()


## Normalized Difference Vegetation Index (NDVI)¶

Below we use TorchGeo’s indices.AppendNDVI to compute the Normalized Difference Vegetation Index (NDVI). NDVI is useful for measuring the presence of vegetation and vegetation health. It can be calculated using the Near Infrared (NIR) and Red bands using the formula below, resulting in a value between [-1, 1] where low NDVI values represents no or unhealthy vegetation and high NDVI values represents healthy vegetation. Here we use a diverging red, yellow, green colormap representing -1, 0, and 1, respectively.

$\text{NDVI} = \frac{\text{NIR} - \text{R}}{\text{NIR} + \text{R}}$
[ ]:

# NDVI is appended to channel dimension (dim=0)
index = AppendNDVI(index_nir=7, index_red=3)
image = sample["image"]
image = index(image)[0]

# Normalize from [-1, 1] -> [0, 1] for visualization
image[-1] = (image[-1] + 1) / 2

plt.imshow(image[-1], cmap="RdYlGn")
plt.axis("off")
plt.show()
plt.close()


## Normalized Difference Water Index (NDWI)¶

Below we use TorchGeo’s indices.AppendNDWI to compute the Normalized Difference Water Index (NDWI). NDWI is useful for measuring the presence of water content in water bodies. It can be calculated using the Green and Near Infrared (NIR) bands using the formula below, resulting in a value between [-1, 1] where low NDWI values represents no water and high NDWI values represents water bodies. Here we use a diverging brown, white, blue-green colormap representing -1, 0, and 1, respectively.

$\text{NDWI} = \frac{\text{G} - \text{NIR}}{\text{G} + \text{NIR}}$
[ ]:

# NDWI is appended to channel dimension (dim=0)
index = AppendNDWI(index_green=2, index_nir=7)
image = index(image)[0]

# Normalize from [-1, 1] -> [0, 1] for visualization
image[-1] = (image[-1] + 1) / 2

plt.imshow(image[-1], cmap="BrBG")
plt.axis("off")
plt.show()
plt.close()


## Normalized Difference Built-up Index (NDBI)¶

Below we use TorchGeo’s indices.AppendNDBI to compute the Normalized Difference Built-up Index (NDBI). NDBI is useful for measuring the presence of urban buildings. It can be calculated using the Short-wave Infrared (SWIR) and Near Infrared (NIR) bands using the formula below, resulting in a value between [-1, 1] where low NDBI values represents no urban land and high NDBI values represents urban land. Here we use a terrain colormap with blue, green-yellow, and brown representing -1, 0, and 1, respectively.

$\text{NDBI} = \frac{\text{SWIR} - \text{NIR}}{\text{SWIR} + \text{NIR}}$
[ ]:

# NDBI is appended to channel dimension (dim=0)
index = AppendNDBI(index_swir=11, index_nir=7)
image = index(image)[0]

# Normalize from [-1, 1] -> [0, 1] for visualization
image[-1] = (image[-1] + 1) / 2

plt.imshow(image[-1], cmap="terrain")
plt.axis("off")
plt.show()
plt.close()


© Copyright 2021, Microsoft Corporation. Revision acad7d47.

Built with Sphinx using a theme provided by Read the Docs.
Versions
latest
stable
v0.5.2
v0.5.1
v0.5.0
v0.4.1
v0.4.0
v0.3.1
v0.3.0
v0.2.1
v0.2.0
v0.1.1
v0.1.0
Project Home
Builds

Free document hosting provided by Read the Docs.

## Docs

Access comprehensive developer documentation for PyTorch

View Docs

## Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials