如何读取 python 中超过 4 个波段的多波段图像文件?
How to read multiband image files with more than 4 bands in python?
我正在使用以下代码读取 geotiff 图像 (~300)。每个图像文件包含 15 个波段。
代码:
import gdal
inputPath="/content/drive/MyDrive/Notebook/test/tiled_stack"
images = []
# Load in the images
for filepath in os.listdir(inputPath):
images.append(gdal.Open(inputPath+'/{0}'.format(filepath)))
image = images.ReadAsArray()
print(type(images[1]))
错误:
AttributeError: 'list' object has no attribute 'ReadAsArray'
来自 this link 的另一个代码:
myImages= glob.glob(os.path.join(inputPath, '*.tif'))
for data_path in myImages:
images=gdal.Open(data_path, gdal.GA_ReadOnly)
type(images)
osgeo.gdal.Dataset
我如何修改代码以解决错误并获得(宽度、高度、波段数)形式的图像?
您可以使用 GDAL 阅读图像,如下所述 post。
示例:
import gdal
filepath = r'c:\Tmp\ds\images\remote_sensing\otherDatasets\sentinel_2\tif\Forest\Forest_1.tif'
#
# Load one GeoTIFF image using GDAL
dataset = gdal.Open(filepath)
image = dataset.ReadAsArray()
print(type(image)) # <class 'numpy.ndarray'>
print(image.shape) # (13, 64, 64)
print(image.dtype) # uint16
在 Windows 中安装 GDAL 并不是那么简单。
我开始使用以下 instructions,但它不是最新的。
我从 here.
下载了 gdal-204-1911-x64-core.msi
和 GDAL-2.4.4.win-amd64-py3.6.msi
(匹配 Python 3.6)
我将环境变量更新为 C:\Program Files
而不是 C:\Program Files (x86)
(如说明中所述)。
出于某种原因,它仅在我 运行 Python 脚本时有效,但在我 Debug 脚本。
更新:
images
是一个 Python 列表。
您不能使用 images.ReadAsArray()
,因为 Python 列表没有方法 ReadAsArray()
.
您可以在执行后将图像附加到列表中 image = dataset.ReadAsArray()
- 您必须为每个图像调用该方法,并且不能将其应用到列表中。
示例:
import gdal
inputPath="/content/drive/MyDrive/Notebook/test/tiled_stack"
images = []
# Load the images, and append them to a list.
for filepath in os.listdir(inputPath):
dataset = gdal.Open(inputPath+'/{0}'.format(filepath))
image = dataset.ReadAsArray() # Returned image is a NumPy array with shape (13, 64, 64) for example.
images.append(image) # Append the NumPy array to the list.
print(type(images[1]))
我正在使用以下代码读取 geotiff 图像 (~300)。每个图像文件包含 15 个波段。
代码:
import gdal
inputPath="/content/drive/MyDrive/Notebook/test/tiled_stack"
images = []
# Load in the images
for filepath in os.listdir(inputPath):
images.append(gdal.Open(inputPath+'/{0}'.format(filepath)))
image = images.ReadAsArray()
print(type(images[1]))
错误:
AttributeError: 'list' object has no attribute 'ReadAsArray'
来自 this link 的另一个代码:
myImages= glob.glob(os.path.join(inputPath, '*.tif'))
for data_path in myImages:
images=gdal.Open(data_path, gdal.GA_ReadOnly)
type(images)
osgeo.gdal.Dataset
我如何修改代码以解决错误并获得(宽度、高度、波段数)形式的图像?
您可以使用 GDAL 阅读图像,如下所述 post。
示例:
import gdal
filepath = r'c:\Tmp\ds\images\remote_sensing\otherDatasets\sentinel_2\tif\Forest\Forest_1.tif'
#
# Load one GeoTIFF image using GDAL
dataset = gdal.Open(filepath)
image = dataset.ReadAsArray()
print(type(image)) # <class 'numpy.ndarray'>
print(image.shape) # (13, 64, 64)
print(image.dtype) # uint16
在 Windows 中安装 GDAL 并不是那么简单。
我开始使用以下 instructions,但它不是最新的。
我从 here.
下载了gdal-204-1911-x64-core.msi
和 GDAL-2.4.4.win-amd64-py3.6.msi
(匹配 Python 3.6)
我将环境变量更新为 C:\Program Files
而不是 C:\Program Files (x86)
(如说明中所述)。
出于某种原因,它仅在我 运行 Python 脚本时有效,但在我 Debug 脚本。
更新:
images
是一个 Python 列表。
您不能使用 images.ReadAsArray()
,因为 Python 列表没有方法 ReadAsArray()
.
您可以在执行后将图像附加到列表中 image = dataset.ReadAsArray()
- 您必须为每个图像调用该方法,并且不能将其应用到列表中。
示例:
import gdal
inputPath="/content/drive/MyDrive/Notebook/test/tiled_stack"
images = []
# Load the images, and append them to a list.
for filepath in os.listdir(inputPath):
dataset = gdal.Open(inputPath+'/{0}'.format(filepath))
image = dataset.ReadAsArray() # Returned image is a NumPy array with shape (13, 64, 64) for example.
images.append(image) # Append the NumPy array to the list.
print(type(images[1]))