简体   繁体   中英

Resample image rasterio/gdal, Python

How can I resample a single band GeoTIFF using Bilinear interpolation?

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)

I have tried the following code and my output is as follows:

在此处输入图像描述

I am trying to achieve the following output:

在此处输入图像描述

One option would be to use the GDAL python bindings. Then you can perform the resample in memory (or you can save the image if you want). Assuming the old raster resolution was 0.25x0.25 and you're resampling to 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')

If you want to save the image put output filepath instead of the empty string for the first argument and change the format to 'tif'!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM