在 Earth Engine 中,缩减器是一种用于跨时间、空间、波段、数组和其他数据结构聚合数据的方法。ee.Reducer
类指定数据的汇总方式。此类中的归约器可以指定要用于汇总的简单统计信息(例如最小值、最大值、平均值、中位数、标准差等),也可以指定输入数据的更复杂的摘要(例如直方图、线性回归、列表)。减少可能发生在时间 (imageCollection.reduce()
)、空间 (image.reduceRegion()
、image.reduceNeighborhood()
)、频段 (image.reduce()
) 或 FeatureCollection
的属性空间(featureCollection.reduceColumns()
或以 aggregate_
开头的 FeatureCollection
方法)中。
Reducer 具有输入和输出
Reducer 会接受一个输入数据集,并生成单个输出。将单个输入缩减器应用于多波段图像时,Earth Engine 会自动复制该缩减器,并将其分别应用于每个波段。因此,输出图片的波段数与输入图片相同;输出中的每个波段都是输入数据中相应波段的像素减少。某些 reducer 接受输入数据集的元组。系统不会为每个频段自动复制这些 reducer。例如,ee.Reducer.LinearRegression()
会按特定顺序获取多个预测器数据集(表示回归中的自变量)(请参阅回归归约器)。
有些 reducer 会生成多个输出,例如 ee.Reducer.minMax()
、ee.Reducer.histogram()
或 ee.Reducer.toList()
。例如:
// Load and filter the Sentinel-2 image collection. var collection = ee.ImageCollection('COPERNICUS/S2_HARMONIZED') .filterDate('2016-01-01', '2016-12-31') .filterBounds(ee.Geometry.Point([-81.31, 29.90])); // Reduce the collection. var extrema = collection.reduce(ee.Reducer.minMax());
import ee import geemap.core as geemap
# Load and filter the Sentinel-2 image collection. collection = ( ee.ImageCollection('COPERNICUS/S2_HARMONIZED') .filterDate('2016-01-01', '2016-12-31') .filterBounds(ee.Geometry.Point([-81.31, 29.90])) ) # Reduce the collection. extrema = collection.reduce(ee.Reducer.minMax())
这将生成一个输出,其波段数是输入波段数的两倍,其中输出中的波段名称会在波段名称后附加“_min”或“_max”。
输出类型应与计算相匹配。例如,应用于 ImageCollection
的 reducer 具有 Image
输出。由于输出会被解读为像素值,因此您必须使用具有数字输出的缩减器来缩减 ImageCollection
(toList()
或 histogram()
等缩减器不起作用)。
Reducer 使用加权输入
默认情况下,对像素值的减少会按其掩码进行加权,但此行为可以更改(请参阅“权重”部分)。掩码等于 0 的像素不会用于缩减。
组合 reducer
如果您的意图是将多个 reducer 应用于相同的输入,最好对 reducer 进行 combine()
以提高效率。具体而言,如果将 sharedInputs
设置为 true
,对 reducer 调用 combine()
将只会对数据进行一次传递。例如,如需计算图片中像素的均值和标准差,您可以使用如下代码:
// Load a Landsat 8 image. var image = ee.Image('LANDSAT/LC08/C02/T1/LC08_044034_20140318'); // Combine the mean and standard deviation reducers. var reducers = ee.Reducer.mean().combine({ reducer2: ee.Reducer.stdDev(), sharedInputs: true }); // Use the combined reducer to get the mean and SD of the image. var stats = image.reduceRegion({ reducer: reducers, bestEffort: true, }); // Display the dictionary of band means and SDs. print(stats);
import ee import geemap.core as geemap
# Load a Landsat 8 image. image = ee.Image('LANDSAT/LC08/C02/T1/LC08_044034_20140318') # Combine the mean and standard deviation reducers. reducers = ee.Reducer.mean().combine( reducer2=ee.Reducer.stdDev(), sharedInputs=True ) # Use the combined reducer to get the mean and SD of the image. stats = image.reduceRegion(reducer=reducers, bestEffort=True) # Display the dictionary of band means and SDs. display(stats)
请注意,在输出中,我们已将 reducer 的名称附加到输入的名称后面,以便区分 reducer 输出。此行为也适用于图片输出,系统会将 reducer 的名称附加到输出频段名称。
如果您要组合使用未加权的输入的 reducer 和使用加权的输入的 reducer,则所有加权的输入都必须位于所有未加权的输入之前。