0

I am making greenest pixel composites from Sentinel-2 SR (using the quality mosaic tool in GEE based on NDVI). I then subtract two greenest pixel composites to get a change in NDVI image.

These blue patches are causing problems. Does someone know how to filter them out efficiently? I could try the aggressive cloud filter algorithm, but it's a lot slower.

enter image description here

        // PRE-EVENT IMAGE
        // Dates over which to create a composite.
        var pre_start = ee.Date('2023-06-01');
        var pre_end = ee.Date('2023-08-07');
        
        // POST-EVENT IMAGE
        // Dates over which to create a composite.
        var post_start = ee.Date('2023-08-08');
        var post_end = ee.Date('2023-09-27');

Map.centerObject(ee.Geometry.Point([10.75125, 61.81312]), 14)

// #########################  NDVI Function ##############################
// Add NDVI  
  function addS2NDVI(image) {
    var ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI');
    return image.addBands(ndvi);
  }

// ###############   Import Sentinel-2 Image Collection  ##################

// Sentinel-2 surface reflectance data for the composite.
var S2 = ee.ImageCollection('COPERNICUS/S2_SR');

// #########################   Make S2 Pre Image  #########################

var pre_filtered = S2.filterDate(pre_start, pre_end)
    .map(addS2NDVI);

var pre_image = ee.Image(pre_filtered.qualityMosaic('NDVI')).select(['B2', 'B3', 'B4', 'B8', 'NDVI']);

// #########################   Make S2 Post Image  #########################

var post_filtered = S2.filterDate(post_start, post_end)  
    .map(addS2NDVI);

var post_image = ee.Image(post_filtered.qualityMosaic('NDVI')).select(['B2', 'B3', 'B4', 'B8', 'NDVI'])

// ########################   Make S2 Difference Image  #######################

var diff = post_image.subtract(pre_image);

// #########################   Visualize and display  #########################

// dNDVI
var dNDVI_params = {bands:"NDVI", min:-0.6, max:0.1};

// RGB true colour
var viz_rgb = {bands: ['B4', 'B3', 'B2'], min: 0, max: 3000, gamma: 1.5};

Map.addLayer(pre_image, viz_rgb, 'pre-event RGB composite');
Map.addLayer(post_image, viz_rgb, 'post-event RGB composite', false);
Map.addLayer(diff, dNDVI_params, 'dNDVI', false);

1 Answer 1

0

I found a solution that seems to work based on this post: Mask Sentinel-2 image using SCL product in Google Earth Engine

Greenest-pixel composite filtering using SCL bands

I filtered by the SCL band using the following function. This is still fast.

// Filter by SCL bands
  var s2_clear_sky = function(image){  
  // 1.Locate SCL product
  var scl = image.select('SCL');

  // 2.Apply mask to the image
  // Select values: 1) greater than 3 and less than 8, or 2) equal to 2 or 7
  var wantedPixels = scl.gt(3).and(scl.lt(6))
                              .or(scl.eq(2))
                              .or(scl.eq(7));
  return image.updateMask(wantedPixels);
};

enter image description here

The script is then as follows:


        // PRE-EVENT IMAGE
        // Dates over which to create a composite.
        var pre_start = ee.Date('2023-06-01');
        var pre_end = ee.Date('2023-08-07');
        
        // POST-EVENT IMAGE
        // Dates over which to create a composite.
        var post_start = ee.Date('2023-08-08');
        var post_end = ee.Date('2023-09-27');

Map.centerObject(ee.Geometry.Point([10.75125, 61.81312]), 14)

// #########################  S2 Functions ##############################
// Add NDVI  
  function addS2NDVI(image) {
    var ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI');
    return image.addBands(ndvi);
  }
  
// Filter by SCL bands
  var s2_clear_sky = function(image){  
  // 1.Locate SCL product
  var scl = image.select('SCL');
 
  // 2.Apply mask to the image
  // Select values: 1) greater than 3 and less than 8, or 2) equal to 2 or 7
  var wantedPixels = scl.gt(3).and(scl.lt(6))
                              .or(scl.eq(2))
                              .or(scl.eq(7));
  return image.updateMask(wantedPixels);
};


// ###############   Import Sentinel-2 Image Collection  ##################

// Sentinel-2 surface reflectance data for the composite.
var S2 = ee.ImageCollection('COPERNICUS/S2_SR');


// #########################   Make S2 Pre Image  #########################

var pre_filtered = S2.filterDate(pre_start, pre_end)
    .map(s2_clear_sky)
    .map(addS2NDVI);

var pre_image = ee.Image(pre_filtered.qualityMosaic('NDVI')).select(['B2', 'B3', 'B4', 'B8', 'NDVI']);

// #########################   Make S2 Post Image  #########################

var post_filtered = S2.filterDate(post_start, post_end)  
    .map(s2_clear_sky)
    .map(addS2NDVI);

var post_image = ee.Image(post_filtered.qualityMosaic('NDVI')).select(['B2', 'B3', 'B4', 'B8', 'NDVI'])

// ########################   Make S2 Difference Image  #######################

var diff = post_image.subtract(pre_image);

// #########################   Visualize and display  #########################

// dNDVI
var dNDVI_params = {bands:"NDVI", min:-0.6, max:0.1};

// RGB true colour
var viz_rgb = {bands: ['B4', 'B3', 'B2'], min: 0, max: 3000, gamma: 1.5};

Map.addLayer(pre_image, viz_rgb, 'pre-event RGB composite');
Map.addLayer(post_image, viz_rgb, 'post-event RGB composite', false);
Map.addLayer(diff, dNDVI_params, 'dNDVI', false);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.