重み付けされた削減

デフォルトでは、画像に適用されるリデューサーは、マスク値に応じて入力に重みを付けます。 これは、 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);

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)

結果が異なるのは、リデューサーで unweighted() を呼び出した結果、リージョンの端にあるピクセルの重みが 1 になるためです。

明示的に重み付けされた出力を取得するには、リデューサーで呼び出される splitWeights() を使用して重みを 明示的に設定することをおすすめします。 によって変更されたリデューサーは 2 つの入力を受け取り、2 番目の入力は重みです。splitWeights()次の例は、クラウド スコア(クラウドが多いほど重みが小さくなる)で重みが付けられたリージョン内の重み付き平均正規化植生指数(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 が 高くなることを示しています。