From 02592cc1811b4cbf5da4c899708163506e420fcb Mon Sep 17 00:00:00 2001 From: duxin Date: Tue, 28 Jul 2026 14:59:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=85=89=E8=B0=B1=E6=8F=90=E5=8F=96=20N?= =?UTF-8?q?aN/Inf=20=E9=98=B2=E6=8A=A4=20+=20=E8=BE=B9=E7=95=8C=E6=8E=A9?= =?UTF-8?q?=E8=86=9C=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 逐波段读取路径:防止 GDAL ReadAsArray 返回 NaN/Inf 污染光谱 - 批量读取路径:astype(float64) 后统一过滤非法值 - 两路径均支持 boundary_mask:水体之外光谱置零 --- src/core/glint_removal/get_spectral.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/core/glint_removal/get_spectral.py b/src/core/glint_removal/get_spectral.py index 687f56c..02c514e 100644 --- a/src/core/glint_removal/get_spectral.py +++ b/src/core/glint_removal/get_spectral.py @@ -779,7 +779,14 @@ def get_spectral_in_coor(imgpath, coorpath, outpath, radius=0, flare_path=None, # GDAL读取的数组形状是 (bands, height, width),像素坐标 (x,y) 对应数组索引 [:, y, x] # 注意:py是行(y坐标),px是列(x坐标) if 0 <= px < all_bands_data.shape[2] and 0 <= py < all_bands_data.shape[1]: - spectrum = all_bands_data[:, py, px] # 直接索引,非常快 + spectrum = all_bands_data[:, py, px].astype(np.float64) + # ★ 防 NaN/Inf:替换非法值为 0(与 radius>0 路径行为一致) + spectrum[~np.isfinite(spectrum)] = 0.0 + # ★ 应用边界掩膜:若像素在水体之外,光谱置零 + if boundary_mask is not None: + if 0 <= py < boundary_mask.shape[0] and 0 <= px < boundary_mask.shape[1]: + if boundary_mask[py, px] != 1: + spectrum[:] = 0.0 coor_spectral[i, original_cols + 2:] = spectrum else: coor_spectral[i, original_cols + 2:] = np.zeros(num_bands) @@ -799,7 +806,14 @@ def get_spectral_in_coor(imgpath, coorpath, outpath, radius=0, flare_path=None, spectrum = np.zeros(num_bands) for band_idx in range(num_bands): band = dataset.GetRasterBand(band_idx + 1) - spectrum[band_idx] = band.ReadAsArray(px, py, 1, 1)[0, 0] + val = band.ReadAsArray(px, py, 1, 1)[0, 0] + # ★ 防 NaN/Inf:替换非法值为 0 + spectrum[band_idx] = val if np.isfinite(val) else 0.0 + # ★ 应用边界掩膜 + if boundary_mask is not None: + if 0 <= py < boundary_mask.shape[0] and 0 <= px < boundary_mask.shape[1]: + if boundary_mask[py, px] != 1: + spectrum[:] = 0.0 coor_spectral[i, original_cols + 2:] = spectrum else: coor_spectral[i, original_cols + 2:] = np.zeros(num_bands)