在我關注的 
但我未能將此模塊安裝到我的 Python 3.8 環境中。相反,我嘗試使用不同的包來讀取 31 波段 tiff 影像的波段。
from osgeo import gdal
from PIL import Image
import numpy as np
import matplotlib as mtp
import matplotlib.pyplot as plt
import pandas as pd
import geopandas as gpd
import earthpy.plot as ep
import rasterio
from rasterio.plot import reshape_as_raster, reshape_as_image
%matplotlib inline
pd.options.display.max_colwidth = 89
#setting the path for image
S1_S2_stack = 'S1_S2_stack.tif'
#path to training and validation data
training_points = 'testing.shp'
validation_points = 'training.shp'
colors = dict ((
(0, (0,76,153,255)), #wheat
(1, (0,153,0,255)), #corn
(2, (255,0,0,255)), #other
(3, (255,153,51,255)),
(4, (255,255,0,255))
))
for k in colors:
v = colors [k]
_v = [_v / 255.0 for _v in v]
colors[k] = _v
index_colors = [colors[key] if key in colors else (1,1,1,0) for key in range (0,5)]
cmap = plt.matplotlib.colors.ListedColormap(index_colors, 'Classification', 5)
src = rasterio.open(S1_S2_stack)
src1 = src.read(S1_S2_stack)
bands = list (src1.tags())
當我運行最后一部分時,它會拋出一個錯誤,上面寫著:
IndexError: band index S out of range (not in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31))
因此,我將不勝感激任何其他建議。
uj5u.com熱心網友回復:
檔案名不是波段索引。
S1_S2_stack = 'S1_S2_stack.tif'
src = rasterio.open(S1_S2_stack)
src1 = src.read(S1_S2_stack) # Error: sending a name instead of an index
以下是我們對 read 方法的了解:
def read(indexes=None, ...)
"""
indexes : int or list, optional
If `indexes` is a list, the result is a 3D array, but is
a 2D array if it is a band index number.
"""
它通過索引讀取波段,索引是第一個引數,應該是整數或整數串列。如果None它回傳所有波段。因此代碼應該類似于
src1 = src.read() # Read all bands together
或者
index = 0 # set the band to read
assert 1 <= index <= src.count # make sure the index does not exceed the number of bands
src1 = src.read(index) # read the band
或者
indexes = [1,2,3]
src1 = src.read(indexes) # read some of the bands
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/489341.html
標籤:Python 图像处理 jupyter-笔记本 tiff
上一篇:當影像在黑色背景上有白色文本時如何對影像進行二值化,反之亦然?
下一篇:Elif不在程式中執行
