Skip to main content
 首页 » 编程设计

python之光栅变换和仿射

2025年04月02日12langtianya

我正在尝试进行一些基本的图像过滤。我从 rasterio cookbook 中逐字包含了一个片段(我从中值滤波器输出中删除了 .astype() )。问题是我的输入和输出栅格应该具有相同的范围但不是。输入和输出的变换和仿射不同。这是预期的行为吗?我是否需要对仿射和变换做一些事情才能使输出与输入相同?

Python 2.7.11 |Anaconda 4.0.0(64 位)| (默认,2016 年 2 月 16 日,09:58:36)Win32 上的 [MSC v.1500 64 位 (AMD64)]

光栅==0.36.0

import rasterio 
from scipy.signal import medfilt 
 
path = "map.tif" 
output = "map2.tif" 
 
with rasterio.open(path) as src: 
    array = src.read() 
    profile = src.profile 
 
# apply a 5x5 median filter to each band 
filtered = medfilt(array, (1, 5, 5)) 
 
# Write to tif, using the same profile as the source 
with rasterio.open(output, 'w', **profile) as dst: 
    dst.write(filtered) 
 
    print profile 
    print dst.profile 
 
>>> {'count': 1, 'crs': CRS({'init': u'epsg:3857'}), 'interleave': 'band', 'dtype': 'float64', 'affine': Affine(100.0, 0.0, -13250000.0, 0.0, 100.0, 3980000.0), 'driver': u'GTiff', 'transform': (-13250000.0, 100.0, 0.0, 3980000.0, 0.0, 100.0), 'height': 1700, 'width': 1700, 'tiled': False, 'nodata': None} 
>>> {'count': 1, 'crs': CRS({'init': u'epsg:3857'}), u'interleave': 'band', 'dtype': 'float64', 'affine': Affine(-13250000.0, 100.0, 0.0, 3980000.0, 0.0, 100.0), 'driver': u'GTiff', 'transform': (0.0, -13250000.0, 100.0, 100.0, 3980000.0, 0.0), 'height': 1700, 'width': 1700, u'tiled': False, 'nodata': None} 

请您参考如下方法:

光栅文档包括一个 history affine/transform usage你可能会觉得有用。我曾经有像下面这样的几行来处理这个问题:

out_profile = src.profile.copy() 
out_affine = out_profile.pop("affine") 
out_profile["transform"] = out_affine 
 
# then, write the output raster 
 
with rasterio.open(output, 'w', **out_profile) as dst: 
    dst.write(filtered) 

我认为这是必要的。