欢迎光临散文网 会员登陆 & 注册

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

2022-05-15 06:45 作者:果子狸爱吃大果子  | 我要投稿
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


Python—图像中无效值像元最临近填充的评论 (共 条)

分享到微博请遵守国家法律