fix: 光谱提取 NaN/Inf 防护 + 边界掩膜支持
- 逐波段读取路径:防止 GDAL ReadAsArray 返回 NaN/Inf 污染光谱 - 批量读取路径:astype(float64) 后统一过滤非法值 - 两路径均支持 boundary_mask:水体之外光谱置零
This commit is contained in:
@ -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]
|
# GDAL读取的数组形状是 (bands, height, width),像素坐标 (x,y) 对应数组索引 [:, y, x]
|
||||||
# 注意:py是行(y坐标),px是列(x坐标)
|
# 注意:py是行(y坐标),px是列(x坐标)
|
||||||
if 0 <= px < all_bands_data.shape[2] and 0 <= py < all_bands_data.shape[1]:
|
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
|
coor_spectral[i, original_cols + 2:] = spectrum
|
||||||
else:
|
else:
|
||||||
coor_spectral[i, original_cols + 2:] = np.zeros(num_bands)
|
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)
|
spectrum = np.zeros(num_bands)
|
||||||
for band_idx in range(num_bands):
|
for band_idx in range(num_bands):
|
||||||
band = dataset.GetRasterBand(band_idx + 1)
|
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
|
coor_spectral[i, original_cols + 2:] = spectrum
|
||||||
else:
|
else:
|
||||||
coor_spectral[i, original_cols + 2:] = np.zeros(num_bands)
|
coor_spectral[i, original_cols + 2:] = np.zeros(num_bands)
|
||||||
|
|||||||
Reference in New Issue
Block a user