【发布时间】:2021-12-27 01:31:21
【问题描述】:
import rasterio as rio
from rasterio.plot import show
from sklearn import cluster
import matplotlib.pyplot as plt
import numpy as np
import glob
for filepath in glob.iglob('./dengue3/*.tiff'):
elhas_raster = rio.open(filepath)
elhas_arr = elhas_raster.read() # read the opened image
vmin, vmax = np.nanpercentile(elhas_arr, (5,95)) # 5-95% contrast stretch
# create an empty array with same dimension and data type
imgxyb = np.empty((elhas_raster.height, elhas_raster.width, elhas_raster.count), elhas_raster.meta['dtype'])
# loop through the raster's bands to fill the empty array
for band in range(imgxyb.shape[2]):
imgxyb[:,:,band] = elhas_raster.read(band+1)
#print(imgxyb.shape)
# convert to 1d array
img1d=imgxyb[:,:,:7].reshape((imgxyb.shape[0]*imgxyb.shape[1],imgxyb.shape[2]))
#print(img1d.shape)
上面的代码我用来读取文件夹中的 tiff 图像并获取数组。但是,输出是 -
ValueError: cannot reshape array of size 6452775 into shape (921825,12)
图像为 12 波段。我尝试在上面的代码中使用 12 代替 7,但代码没有执行。我该如何解决这个问题?感谢您的宝贵时间。
【问题讨论】:
-
你是想重塑 7 个乐队,还是仅仅重塑第 7 个乐队?
标签: python numpy tiff rasterio