本文主要是介绍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
关注我的个人WX_GZH:小Rser
Python—图像中无效值像元最临近填充https://mp.weixin.qq.com/s?__biz=MzkyNjMzNTQ2Mw==&mid=2247483739&idx=1&sn=afdf525c309271c28bf2ff539a7dd8ce&chksm=c239a9d3f54e20c5c6db5a7ba9050684c64a823908fae39ecf222c97322455e32cd3a3d614c5&token=2086761678&lang=zh_CN#rd
这篇关于Python—图像中无效值像元最临近填充的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!