การลดแบบถ่วงน้ำหนัก

โดยค่าเริ่มต้น ตัวลดที่ใช้กับน้ำหนักของภาพจะให้น้ำหนักอินพุตตามค่ามาสก์ ซึ่งเกี่ยวข้องในบริบทของพิกเซลเศษส่วนที่สร้างขึ้นผ่านการดำเนินการต่างๆ เช่น 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)

ความแตกต่างของผลลัพธ์เกิดจากพิกเซลที่ขอบของภูมิภาคที่ได้รับน้ำหนัก เป็น 1 อันเป็นผลมาจากการเรียกใช้ unweighted() ในตัวลด

หากต้องการรับเอาต์พุตที่มีการถ่วงน้ำหนักอย่างชัดเจน คุณควรตั้งค่าการถ่วงน้ำหนัก อย่างชัดเจนด้วย splitWeights() ที่เรียกใช้ในตัวลด ตัวลดที่แก้ไขโดย splitWeights() จะรับอินพุต 2 รายการ โดยอินพุตที่ 2 คือน้ำหนัก ตัวอย่างต่อไปนี้แสดง 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);

การตั้งค่า 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)

โปรดทราบว่าต้องเพิ่ม cloudWeight เป็นแบนด์ก่อนโทร reduceRegion() ผลลัพธ์แสดงให้เห็นว่า NDVI เฉลี่ยโดยประมาณสูงขึ้นเนื่องจากการลดน้ำหนักของพิกเซลที่มีเมฆ