Giảm trọng số

Theo mặc định, bộ giảm áp dụng cho hình ảnh sẽ cân nhắc các giá trị đầu vào theo giá trị mặt nạ. Điều này có liên quan trong bối cảnh các pixel phân số được tạo thông qua các thao tác như clip(). Hãy điều chỉnh hành vi này bằng cách gọi unweighted() trên bộ giảm. Việc sử dụng bộ giảm không có trọng số sẽ buộc tất cả các pixel trong khu vực phải có cùng trọng số. Ví dụ sau minh hoạ cách việc cân nhắc pixel có thể ảnh hưởng đến đầu ra của bộ giảm đầu ra:

Đối với các mức giảm có trọng số, trọng số giao nhau của pixel được biểu thị nội bộ dưới dạng số nguyên 8 bit (từ 0 đến 255). Điều này định lượng mức độ bao phủ pixel phân số thành 256 cấp rời rạc. Do đó, mọi phân số bao phủ nhỏ hơn khoảng 1/256 (~0,4%) sẽ được làm tròn xuống trọng số là 0 (không hợp lệ). Hãy xem hướng dẫn Giảm khu vực để biết thêm thông tin về cách cân nhắc các pixel trong một khu vực.

Trình soạn thảo mã (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);

Thiết lập Python

Hãy xem trang Môi trường Python để biết thông tin về Python API và cách sử dụng geemap cho quá trình phát triển có tính tương tác.

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)

Sự khác biệt về kết quả là do các pixel ở rìa khu vực nhận được trọng số là một do kết quả của việc gọi unweighted() trên bộ giảm.

Để nhận được đầu ra có trọng số rõ ràng, bạn nên đặt trọng số một cách rõ ràng bằng splitWeights() được gọi trên bộ giảm. Bộ giảm được sửa đổi bằng splitWeights() sẽ nhận 2 giá trị đầu vào, trong đó giá trị đầu vào thứ hai là trọng số. Ví dụ sau minh hoạ splitWeights() bằng cách tính giá trị trung bình có trọng số của Chỉ số thực vật khác biệt được chuẩn hoá (NDVI) trong một khu vực, với trọng số được cung cấp theo điểm số đám mây (càng nhiều mây thì trọng số càng thấp):

Trình soạn thảo mã (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);

Thiết lập Python

Hãy xem trang Môi trường Python để biết thông tin về Python API và cách sử dụng geemap cho quá trình phát triển có tính tương tác.

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)

Hãy lưu ý rằng bạn cần thêm cloudWeight làm một dải trước khi gọi reduceRegion(). Kết quả cho thấy giá trị trung bình NDVI ước tính cao hơn do giảm trọng số của các pixel có mây.