加权缩减

默认情况下,应用于影像的归约函数会根据遮罩值对输入进行加权。 这与通过 clip() 等操作创建的分数像素有关。通过对 reducer 调用 unweighted() 来调整此行为。使用非加权归约函数会强制使区域中的所有像素具有相同的权重。以下示例说明了像素权重如何影响 reducer 输出:

对于加权缩减,像素交集权重在内部表示为 8 位整数(0 到 255)。此函数会将像素覆盖率(以小数表示)量化为 256 个离散级别。 因此,任何小于大约 1/256(约 0.4%)的覆盖率都会向下舍入为权重 0(无效)。如需详细了解如何对区域中的像素进行加权,请参阅减少区域指南

代码编辑器 (JavaScript)

// Load a Landsat 8 input image.
var image = ee.Image('LANDSAT/LC08/C02/T1/LC08_044034_20140318');

// Create an arbitrary region.
var geometry = ee.Geometry.Rectangle(-122.496, 37.532, -121.554, 37.538);

// Make an NDWI image.  It will have one band named 'nd'.
var ndwi = image.normalizedDifference(['B3', 'B5']);

// Compute the weighted mean of the NDWI image clipped to the region.
var weighted = ndwi.clip(geometry)
  .reduceRegion({
    reducer: ee.Reducer.mean(),
    geometry: geometry,
    scale: 30})
  .get('nd');

// Compute the UN-weighted mean of the NDWI image clipped to the region.
var unweighted = ndwi.clip(geometry)
  .reduceRegion({
    reducer: ee.Reducer.mean().unweighted(),
    geometry: geometry,
    scale: 30})
  .get('nd');

// Observe the difference between weighted and unweighted reductions.
print('weighted:', weighted);
print('unweighted', unweighted);

Python 设置

如需了解 Python API 和如何使用 geemap 进行交互式开发,请访问 Python 环境页面。

import ee
import geemap.core as geemap

Colab (Python)

# Load a Landsat 8 input image.
image = ee.Image('LANDSAT/LC08/C02/T1/LC08_044034_20140318')

# Create an arbitrary region.
geometry = ee.Geometry.Rectangle(-122.496, 37.532, -121.554, 37.538)

# Make an NDWI image.  It will have one band named 'nd'.
ndwi = image.normalizedDifference(['B3', 'B5'])

# Compute the weighted mean of the NDWI image clipped to the region.
weighted = (
    ndwi.clip(geometry)
    .reduceRegion(reducer=ee.Reducer.mean(), geometry=geometry, scale=30)
    .get('nd')
)

# Compute the UN-weighted mean of the NDWI image clipped to the region.
unweighted = (
    ndwi.clip(geometry)
    .reduceRegion(
        reducer=ee.Reducer.mean().unweighted(), geometry=geometry, scale=30
    )
    .get('nd')
)

# Observe the difference between weighted and unweighted reductions.
display('weighted:', weighted)
display('unweighted', unweighted)

结果之所以不同,是因为在 reducer 上调用 unweighted() 后,区域边缘的像素获得了 1 的权重。

为了获得明确加权的输出,最好通过在 reducer 上调用 splitWeights() 来明确设置权重。由 splitWeights() 修改的 reducer 接受两个输入,其中第二个输入是权重。以下示例通过计算某个区域中加权平均归一化差值植被指数 (NDVI) 来演示 splitWeights(),其中权重由云得分给出(云量越大,权重越低):

代码编辑器 (JavaScript)

// Load an input Landsat 8 image.
var image = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_186059_20130419');

// Compute cloud score and reverse it such that the highest
// weight (100) is for the least cloudy pixels.
var cloudWeight = ee.Image(100).subtract(
  ee.Algorithms.Landsat.simpleCloudScore(image).select(['cloud']));

// Compute NDVI and add the cloud weight band.
var ndvi = image.normalizedDifference(['B5', 'B4']).addBands(cloudWeight);

// Define an arbitrary region in a cloudy area.
var region = ee.Geometry.Rectangle(9.9069, 0.5981, 10.5, 0.9757);

// Use a mean reducer.
var reducer = ee.Reducer.mean();

// Compute the unweighted mean.
var unweighted = ndvi.select(['nd']).reduceRegion(reducer, region, 30);

// compute mean weighted by cloudiness.
var weighted = ndvi.reduceRegion(reducer.splitWeights(), region, 30);

// Observe the difference as a result of weighting by cloudiness.
print('unweighted:', unweighted);
print('weighted:', weighted);

Python 设置

如需了解 Python API 和如何使用 geemap 进行交互式开发,请访问 Python 环境页面。

import ee
import geemap.core as geemap

Colab (Python)

# Load an input Landsat 8 image.
image = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_186059_20130419')

# Compute cloud score and reverse it such that the highest
# weight (100) is for the least cloudy pixels.
cloud_weight = ee.Image(100).subtract(
    ee.Algorithms.Landsat.simpleCloudScore(image).select(['cloud'])
)

# Compute NDVI and add the cloud weight band.
ndvi = image.normalizedDifference(['B5', 'B4']).addBands(cloud_weight)

# Define an arbitrary region in a cloudy area.
region = ee.Geometry.Rectangle(9.9069, 0.5981, 10.5, 0.9757)

# Use a mean reducer.
reducer = ee.Reducer.mean()

# Compute the unweighted mean.
unweighted = ndvi.select(['nd']).reduceRegion(reducer, region, 30)

# compute mean weighted by cloudiness.
weighted = ndvi.reduceRegion(reducer.splitWeights(), region, 30)

# Observe the difference as a result of weighting by cloudiness.
display('unweighted:', unweighted)
display('weighted:', weighted)

请注意,在调用 reduceRegion() 之前,需要将 cloudWeight 添加为频段。结果表明,降低多云像素的权重后,估计的平均 NDVI 会更高。