eMapR / LT-GEE

Google Earth Engine implementation of the LandTrendr spectral-temporal segmentation algorithm. For documentation see:
https://emapr.github.io/LT-GEE
Apache License 2.0
198 stars 65 forks source link

Issue: 'FUNCTION TO REDUCE COLLECTION TO SINGLE IMAGE PER YEAR BY MEDOID' #40

Open Towhid-Islam opened 1 year ago

Towhid-Islam commented 1 year ago

Thank you so much for developing the amazing tool in GEE. I found that medoid method was applied to create yearly landsat composite (article link). I found the relevant codes in GEE for this operation (medoid method):

// make a medoid composite with equal weight among indices
var medoidMosaic = function(inCollection, dummyCollection) {

  // fill in missing years with the dummy collection
  // ** braaten edit 2023-05-02: toList and If are resource intensive, simply
  // ** merging the inCollection and dummyCollection should achieve the goal
  // ** of have at least one image for the median operation.
  var imageCount = inCollection.toList(1).length();                                                            // get the number of images 
  var finalCollection = ee.ImageCollection(ee.Algorithms.If(imageCount.gt(0), inCollection, dummyCollection)); // if the number of images in this year is 0, then use the dummy collection, otherwise use the SR collection
  // var finalCollection = inCollection.merge(dummyCollection);

  // calculate median across images in collection per band
  var median = finalCollection.median();                                                                       // calculate the median of the annual image collection - returns a single 6 band image - the collection median per band

  // calculate the different between the median and the observation per image per band
  var difFromMedian = finalCollection.map(function(img) {
    var diff = ee.Image(img).subtract(median).pow(ee.Image.constant(2));                                       // get the difference between each image/band and the corresponding band median and take to power of 2 to make negatives positive and make greater differences weight more
    return diff.reduce('sum').addBands(img);                                                                   // per image in collection, sum the powered difference across the bands - set this as the first band add the SR bands to it - now a 7 band image collection
  });

  // get the medoid by selecting the image pixel with the smallest difference between median and observation per band 
  return ee.ImageCollection(difFromMedian).reduce(ee.Reducer.min(7)).select([1,2,3,4,5,6], ['B1','B2','B3','B4','B5','B7']); // find the powered difference that is the least - what image object is the closest to the median of teh collection - and then subset the SR bands and name them - leave behind the powered difference band
};

Here, .reduce(ee.Reducer.min(7) gave me output with the lowest value of the pixels rather than the closest value to the median. I am not so sure through, this may lead to the inappropriate result. Could you please confirm that the tool actually used medoid method to prepare yearly single image? I will be very grateful for that. I might be wrong. If so, please accept my apologies and let me know why it is alright. Thank you so much! I will be looking forward to your kind response.

Towhid-Islam commented 1 year ago

I modified the codes like this:

var medoidMosaic = function(inCollection, dummyCollection) {
  var imageCount = inCollection.toList(1).length();
  var finalCollection = ee.ImageCollection(ee.Algorithms.If(imageCount.gt(0), inCollection, dummyCollection));
  var median = finalCollection.median();
  var difFromMedian = finalCollection.map(function(img) {
    var diff = (ee.Image(img).subtract(median)).abs();
    return diff.reduce('sum').multiply(-1).addBands(img);
  });
  return ee.ImageCollection(difFromMedian).qualityMosaic('sum').select([1,2,3,4,5,6], ['B1','B2','B3','B4','B5','B7']);
};

Could you please let me know what you think about it. Thanks a lot!

jdbcode commented 1 year ago

Can you send a script that shows how you arrived at these conclusions?

Thank you! (I may not be able to look or respond until the week of Oct 16th)

Towhid-Islam commented 1 year ago

Thank you so much for your kind response. Here is the script link: (https://code.earthengine.google.com/e7ab3f7ecd228feccc47db989dd3be13)

If you inspect a point, you see band values of both Output_ReducerMin & closestToMedianImage and compare those with the bands in the list of images in ImageCollection: True Color (432) and the median image.

I will be waiting to hear back from you. Thanks Again!