根據預設,套用至影像的縮減函式會根據遮罩值加權輸入內容。這與透過 clip() 等作業建立的分數像素相關。如要調整這項行為,請在縮減器上呼叫 unweighted()。使用未加權的縮減器會強制區域中的所有像素具有相同權重。以下範例說明像素權重如何影響縮減器輸出內容:
如果是加權減少,像素交集權重在內部會以 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);
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)
結果差異是因為區域邊緣的像素在呼叫還原器時,會收到權重 1。unweighted()
如要取得明確加權的輸出內容,最好使用在還原器上呼叫的 splitWeights() 明確設定權重。由 splitWeights() 修改的縮減函式會採用兩個輸入值,其中第二個輸入值是權重。以下範例說明 splitWeights(),方法是計算某個區域的加權平均常態化差異植被指數 (NDVI),並以雲層分數做為權重 (雲層越厚,權重越低):
程式碼編輯器 (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);
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)
請注意,您必須先將 cloudWeight 新增為頻帶,才能呼叫 reduceRegion()。結果顯示,降低多雲像素的權重後,預估平均 NDVI 會提高。