从tiff图像查找NDVI索引时python脚本中出现越界错误
我正在将NDVI指数从图像转换成数字,以便打印NDVI数值 为此,我在谷歌上搜索了一下,找到了一个python代码。我正在使用Ubuntu,我已经安装了pyhton 2和pyhton 3 我对python非常陌生从tiff图像查找NDVI索引时python脚本中出现越界错误,python,indexoutofboundsexception,Python,Indexoutofboundsexception,我正在将NDVI指数从图像转换成数字,以便打印NDVI数值 为此,我在谷歌上搜索了一下,找到了一个python代码。我正在使用Ubuntu,我已经安装了pyhton 2和pyhton 3 我对python非常陌生 # Import the Python 4 print function from __future__ import print_function # Import the "gdal" and "gdal_array" submodules from within the "os
# Import the Python 4 print function
from __future__ import print_function
# Import the "gdal" and "gdal_array" submodules from within the "osgeo" module
from osgeo import gdal
from osgeo import gdal_array
# Import the NumPy module
import numpy as np
# Open a GDAL dataset
dataset = gdal.Open('/home/path to directory/A0_200_T.TIF', gdal.GA_ReadOnly)
# Allocate our array using the first band's datatype
image_datatype = dataset.GetRasterBand(1).DataType
image = np.zeros((dataset.RasterYSize, dataset.RasterXSize, dataset.RasterCount),
dtype=gdal_array.GDALTypeCodeToNumericTypeCode(image_datatype))
# Loop over all bands in dataset
for b in xrange(dataset.RasterCount):
# Remember, GDAL index is on 1, but Python is on 0 -- so we add 1 for our GDAL calls
band = dataset.GetRasterBand(b + 1)
# Read in the band's data into the third dimension of our array
image[:, :, b] = band.ReadAsArray()
print('Red band mean: {r}'.format(r=image[:, :, 2].mean()))
print('NIR band mean: {nir}'.format(nir=image[:, :, 3].mean()))
b_red = 2
b_nir = 3
ndvi = (image[:, :, b_nir] - image[:, :, b_red]) / \
(image[:, :, b_nir] + image[:, :, b_red]).astype(np.float64)
print('NDVI matrix: ')
print(ndvi)
print('\nMax NDVI: {m}'.format(m=ndvi.max()))
print('Mean NDVI: {m}'.format(m=ndvi.mean()))
print('Median NDVI: {m}'.format(m=np.median(ndvi)))
print('Min NDVI: {m}'.format(m=ndvi.min()))
现在,当我使用“python name.py”运行这个脚本时,它会导致以下错误
Traceback (most recent call last):
File "ndvi.py", line 29, in <module>
print('Red band mean: {r}'.format(r=image[:, :, 5].mean()))
IndexError: index 5 is out of bounds for axis 2 with size 1
回溯(最近一次呼叫最后一次):
文件“ndvi.py”,第29行,在
打印('Red band mean:{r}'。格式(r=image[:,:,5].mean())
索引器:索引5超出大小为1的轴2的界限
阵列似乎正在存储越界数据,但我不知道如何解决此问题。(!??)
我也不知道它是否给出了NDVI指数,但我希望如此。(如果有其他方法,请建议我。)
这方面的任何帮助都将是非常可观的
如果您需要更多信息,请告诉我
先谢谢你。
R