【问题标题】:Resample image rasterio/gdal, Python重新采样图像光栅/gdal,Python
【发布时间】:2020-07-20 04:48:01
【问题描述】:

如何使用双线性插值对单波段 GeoTIFF 进行重新采样?

import os

import rasterio
from rasterio.enums import Resampling
from rasterio.plot import show,show_hist
import numpy as np

if __name__ == "__main__":
    

    input_Dir = 'sample.tif'
    #src = rasterio.open(input_Dir) 
    #show(src,cmap="magma")

    upscale_factor = 2

    with rasterio.open(input_Dir) as dataset:
        
        # resample data to target shape
        data = dataset.read(
            out_shape=(
                dataset.count,
                int(dataset.height * upscale_factor),
                int(dataset.width * upscale_factor)
            ),
            resampling=Resampling.bilinear
        )

        # scale image transform
        transform = dataset.transform * dataset.transform.scale(
            (dataset.width / data.shape[-1]),
            (dataset.height / data.shape[-2])
        )
        show(dataset,cmap="magma",transform=transform)

我试过下面的代码,我的输出如下:

我正在尝试实现以下输出:

【问题讨论】:

  • 如果您不需要保存重新采样的 tif,matplotlib 将为您进行插值matplotlib.org/3.1.1/gallery/images_contours_and_fields/…
  • 但是,您没有显示重新采样的data。你显示的是原始的dataset
  • 我必须创建一个新的栅格吗?原因只是在 show 方法中传递数据变量不会完成这项工作。
  • 如果你直接使用matplotlib,不,你不需要像我说的那样创建一个新的tiff
  • 谢谢。它奏效了

标签: python gis raster gdal rasterio


【解决方案1】:

一种选择是使用 GDAL python 绑定。然后您可以在内存中执行重新采样(或者您可以根据需要保存图像)。假设旧的光栅分辨率为 0.25x0.25,而您重新采样为 0.10x0.10:

from osgeo import gdal

input_Dir = 'sample.tif'

ds = gdal.Translate('', input_Dir, xres=0.1, yres=0.1, resampleAlg="bilinear", format='vrt')

如果要保存图像,请将输出文件路径而不是空字符串作为第一个参数并将格式更改为'tif'!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多