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)