python gdal实现遥感影像所有波段比值
时间: 2024-05-16 07:14:52 浏览: 91
利用gdal批量提取遥感影像波段_单波段提取_gdal提取波段_利用gdal批量提取遥感波段中的单波段_遥感影像_GDAL_
5星 · 资源好评率100%
使用Python中的GDAL库可以很方便地实现遥感影像所有波段比值。
具体步骤如下:
1. 导入所需的库
```python
import gdal
import numpy as np
```
2. 打开遥感影像文件
```python
raster = gdal.Open("path/to/raster.tif")
```
3. 读取影像的所有波段数据,并将其转换为NumPy数组
```python
bands = []
for i in range(1, raster.RasterCount+1):
band = raster.GetRasterBand(i)
bands.append(band.ReadAsArray())
bands = np.array(bands)
```
4. 计算所有波段的比值
```python
ratios = []
for i in range(bands.shape[0]):
for j in range(i+1, bands.shape[0]):
ratio = bands[i]/bands[j]
ratios.append(ratio)
ratios = np.array(ratios)
```
5. 将所有比值写入新的遥感影像文件中
```python
driver = gdal.GetDriverByName("GTiff")
outRaster = driver.Create("path/to/output.tif", raster.RasterXSize, raster.RasterYSize, ratios.shape[0], gdal.GDT_Float32)
outRaster.SetGeoTransform(raster.GetGeoTransform())
outRaster.SetProjection(raster.GetProjection())
for i in range(ratios.shape[0]):
outband = outRaster.GetRasterBand(i+1)
outband.WriteArray(ratios[i,:,:])
outband.FlushCache()
```
这样就可以将所有波段的比值写入新的遥感影像文件中了。
阅读全文