python NDVI
时间: 2023-10-28 18:57:40 浏览: 98
NDVI (Normalized Difference Vegetation Index) is a commonly used vegetation index in remote sensing and image analysis. It is calculated using the near-infrared (NIR) and red (R) bands of satellite or aerial imagery.
The formula to calculate NDVI is:
NDVI = (NIR - R) / (NIR + R)
In Python, you can compute NDVI using various libraries, such as numpy and rasterio. Here's an example code snippet to calculate NDVI from a satellite image:
```python
import numpy as np
import rasterio
# Open the satellite image file
with rasterio.open('path_to_image.tif') as src:
# Read the red and near-infrared bands
red = src.read(3)
nir = src.read(4)
# Perform NDVI calculation
ndvi = (nir - red) / (nir + red)
# Save the NDVI image
ndvi_image = rasterio.open('path_to_save_ndvi.tif', 'w', driver='GTiff', width=src.width, height=src.height, count=1, crs=src.crs, transform=src.transform, dtype=np.float32)
ndvi_image.write(ndvi, 1)
ndvi_image.close()
```
Make sure to replace `'path_to_image.tif'` with the actual path to your satellite image file. The code reads the red and near-infrared bands from the image, calculates the NDVI values, and saves the resulting NDVI image as a GeoTIFF file.
Please note that this is just a basic example, and you may need to modify it based on your specific requirements and the library you choose to work with.
阅读全文