[英]Resample image rasterio/gdal, Python
如何使用双线性插值对单波段 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)
我试过下面的代码,我的 output 如下:
我正在尝试实现以下 output:
一种选择是使用 GDAL python 绑定。 然后您可以在 memory 中执行重新采样(或者您可以根据需要保存图像)。 假设旧的光栅分辨率为 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')
如果要保存图像,请将 output 文件路径而不是空字符串作为第一个参数并将格式更改为“tif”!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.