Use Reducer to Observe the difference between weighted and unweighted mean of the NDVI image clipped to the region.

The Normalized Difference Vegetation Index (NDVI) is a simple graphical indicator that can be used to analyze remote sensing measurements, typically, but not necessarily, from a space platform, and assess whether the target being observed contains live green vegetation or not.

The NDVI is calculated from these individual measurements as follows:

NDVI= (NIR-Red) \ (NIR+Red)

The following code is available in “Weighted Reducer” page of Google Earth Engine Developer page. I found it interesting and worth-sharing so I copied the code as it is here. If you want to learn more about it, please go here:

//Source: https://developers.google.com/earth-engine/reducers_weighting

Map.setOptions("HYBRID");
// Load a Landsat 8 input image.
var image = ee.Image('LANDSAT/LC08/C01/T1/LC08_044034_20140318');

// Creat an arbitrary region.
var geometry = ee.Geometry.Rectangle(-122.496, 37.532, -121.554, 37.538);
Map.centerObject(geometry,10);


// Make an ndvi image.  It will have one band named 'nd'.
var ndvi = image.normalizedDifference(['B5', 'B4']);

Map.addLayer(ndvi, {}, "NDVI");

// Compute the weighted mean of the ndvi image clipped to the region.
var weighted = ndvi.clip(geometry)
  .reduceRegion({
    reducer: ee.Reducer.sum(),
    geometry: geometry,
    scale: 30})
  .get('nd');

// Compute the UN-weighted mean of the ndvi image clipped to the region.
var unweighted = ndvi.clip(geometry)
  .reduceRegion({
    reducer: ee.Reducer.sum().unweighted(),
    geometry: geometry,
    scale: 30})
  .get('nd');

// Observe the difference between weighted and unweighted reductions.
print('weighted:', weighted);
print('unweighted', unweighted);


// Load an input Landsat 8 image.
var image = ee.Image('LANDSAT/LC08/C01/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);

Leave a Reply

%d bloggers like this: