【问题标题】:Tiff images to numpy arraysTiff 图像到 numpy 数组
【发布时间】: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


【解决方案1】:

你改变了你试图重塑的索引的大小,但没有改变 reshape 命令参数:

img1d=imgxyb[:,:,:7].reshape((imgxyb.shape[0]*imgxyb.shape[1],imgxyb.shape[2]))

这应该是:

img1d=imgxyb[:,:,:7].reshape((imgxyb.shape[0]*imgxyb.shape[1],7))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    • 2014-01-30
    • 1970-01-01
    • 2022-12-05
    • 2012-11-13
    • 2012-12-31
    • 1970-01-01
    相关资源
    最近更新 更多