Python—图像中无效值像元最临近填充

import gdal
import numpy as np
from scipy import ndimage as nd
file = r"D:\微信公众号\数据填补\处理前.tif"
outfile = r"D:\微信公众号\数据填补\处理后.tif"
ds = gdal.Open(file)
cols = ds.RasterXSize
rows = ds.RasterYSize
geo = ds.GetGeoTransform()
proj = ds.GetProjection()
band = ds.GetRasterBand(1)
d_type = band.DataType
nodata = band.GetNoDataValue()
data = (ds.ReadAsArray()).astype(np.float32)
data[data ==nodata] = np.nan
mask = np.isnan(data)
ind = nd.distance_transform_edt(mask, 
                                return_distances=False, 
                                return_indices=True)
data = data[tuple(ind)]
driver = gdal.GetDriverByName("GTiff")
outds = driver.Create(outfile, cols, rows, 1, d_type)
outds.SetGeoTransform(geo)
outds.SetProjection(proj)
outband = outds.GetRasterBand(1)
outband.WriteArray(data)
outband.SetNoDataValue(nodata)

测试数据:

百度网盘:https://pan.baidu.com/s/1ZqAlIe4vGBF7SvTMNhhfrg

密 码:e3pd


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容