Earth Engine – reduce image collection based on pixels with max in one band

google-earth-enginegoogle-earth-engine-javascript-api

I'm trying to reduce an ImageCollection based on pixels with the highest value in a particular band (Sentinel 2, band 3, in case that matters). I also need to retain all bands in the output. So, I want to choose the greenest pixels from the image stack. The max() reducer doesn't appear to work this way? How would I go about this?

var s2 = s2_col.filterDate('2019','2022')
               .filterBounds(LGA)
               .reduce(ee.Reducer.max(s2_col.select('B3')))

Best Answer

You can use qualityMosaic to do this, or you can use ee.Reducer.max({numInputs:13}), however with the reducer, the band with the max you want to select on has to be the first band.

var s2 = s2_col.filterDate('2019','2022')
               .filterBounds(LGA)

// Move B3 to position 0 in the list of bands.
var bands = s2.first().bandNames()
bands = bands.remove('B3')
bands = bands.insert('B3', 0)

var result = s2.select(bands).reduce(ee.Reducer.max(13)).rename(bands)