TransWikia.com

Calculating 3x3 pixel Neighborhood in GEE

Geographic Information Systems Asked on July 2, 2021

I’m trying to calculate 3×3 pixel kernel for each image on my NDVI image collection. My goal is to do in GEE focal statistics like in Arcgis. My code is below and it generates an NDVI image collection and then a 3×3 kernel NDVI image collection and charts the values of the pixel mean in my geometry from a range of dates. When I print the charts there is no difference in the NDVI values between the two charts. What am I doing wrong?

code:

//-----------------------------------------------------------------------------------
// Set up basic variables (NDVI).
//-----------------------------------------------------------------------------------
var shpfile_name     = 'argentina/Argentina_Shifts_Divided_NEW_buffer_15m_proj20S'; // Shapefile name 
(from Assets).
var folder           = ''
var image_collection = "COPERNICUS/S2_SR"//image collection.
var first_date       = '2019-09-01' // First date in image collection
var last_date        = '2020-03-30' // Last date in image collection
var user_account     = 'users/yonatangoldwasser/'
var fc_ID            = 'Group_'; // Field name of column with plot names.
var min_v            = 0.05
var max_v            = 0.75
//
//-----------------------------------------------------------------------------------
// Load raster data (COPERNICUS/S2_SR).
// Load vector data (shapefile).
var fc = ee.FeatureCollection(user_account+folder+shpfile_name);
var S2 = ee.ImageCollection(image_collection)
        .filterDate(first_date, last_date)
        .filterBounds(fc)
        .filterMetadata('CLOUDY_PIXEL_PERCENTAGE', 'less_than', 18);

//-----------------------------------------------------------------------------------
print(S2)
print(fc)           // CRS must be WGS84 (for the shapefile)          

//-----------------------------------------------------------------------------------
//Image Expression for calculating NDVI----------------------------------------------
var band_1 = 'B4'
var band_2 = 'B8'

var addNDVI = function(image) {
  var NDVI = image.expression(
  '(B8-B4)/(B8+B4)',
  {
    'B4': image.select('B4'),
    'B8': image.select('B8'),

  }).rename('NDVI');
  //NDVI index
  return image.addBands(NDVI.add(ee.Image(0.0)));
  //Add 0.0 value to NDVI (currently not in use)
};


// Calculate NDVIfor images in the Image collection 
var S2_withNDVI = ee.ImageCollection(S2).map(addNDVI);          
print(S2_withNDVI)


// Show image of mean NDVI (over image collection) on GEE. 
var scene = ee.Image(S2_withNDVI.mean());

// Prepare palette for image display     
var palette = ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718',
          '74A901', '66A000', '529400', '3E8601', '207401', '056201',
          '004C00', '023B01', '012E01', '011D01', '011301'];
          //palette for NDVI image on GEE.

// Add NDVI image and shapefile on GEE platform (min;max are range values)
Map.addLayer(scene, {min: min_v, max: max_v, palette: palette}, 'NDVI', 1, 0.85);
var styling = {color: 'red', fillColor: '00000000'};
Map.addLayer(fc.style(styling)) 

//Export NDVI Time Series into a Scatter Chart and a CSV format              
var TimeSeries = Chart.image.seriesByRegion(
S2_withNDVI, fc, ee.Reducer.mean(), 'NDVI', 3, 'system:time_start', fc_ID)
    .setChartType('Table')
    .setOptions({
      title: 'NDVI Time Series For Shift 1-2 Argentina',
      vAxis: {title: 'NDVI'},
      hAxis: {title: 'Date'},
      lineWidth: 1,
      pointSize: 4,
      series: {
}});
// Display.
print(TimeSeries); 

// Compute 3X3 kernel as texture of the NDVI.
var addTexture_3X3 = function(image) {
var texture = image.select('NDVI').reduceNeighborhood({
    reducer: ee.Reducer.mean(),
    kernel: ee.Kernel.square(1),
    }).rename('NDVI_3X3');
    return image.addBands(texture.add(ee.Image(0.0)));
};
var S2_withNDVI_Analysis = ee.ImageCollection(S2_withNDVI).map(addTexture_3X3);
print(S2_withNDVI_Analysis)

// Show image of 3X3 NDVI (over image collection) on GEE. 

var scene2 = ee.Image(S2_withNDVI_Analysis.mean());
// Add NDVI image and shapefile on GEE platform (min;max are range values)

Map.addLayer(scene2.select('NDVI_3X3'), {min: min_v, max: max_v, palette: palette}, 'NDVI_3X3', 1,      0.85);



//Export 3X3 kernel NDVI Time Series into a Scatter Chart and a CSV format              
var TimeSeries = Chart.image.seriesByRegion(
S2_withNDVI_Analysis, fc, ee.Reducer.mean(), 'NDVI_3X3', 3, 'system:time_start', fc_ID)
    .setChartType('Table')
    .setOptions({
      title: 'NDVI 3X3 Time Series For Shift 1-2 Argentina',
      vAxis: {title: 'NDVI mean'},
      hAxis: {title: 'Date'},
      lineWidth: 1,
      pointSize: 4,
      series: {
}});
// Display.
print(TimeSeries); 

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP