1

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')))

2 Answers 2

1

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)
1
1

Issues are produced because there is a wrong parameters inside max reducer (numInputs; Integer, default: 1). In this case, you can get the greenest pixels from the image stack in two ways; as it can be observed in following code.

var LGA = ee.Geometry.Polygon(
        [[[-118.491015625, 39.863116755771],
          [-118.491015625, 38.5006330236386],
          [-116.82109375, 38.5006330236386],
          [-116.82109375, 39.863116755771]]], null, false);

var imageVisParam = {"opacity":1,
                     "bands":["B3_max"],
                     "min":9326,
                     "max":16916,
                     "gamma":1};

var s2_col = ee.ImageCollection('COPERNICUS/S2_SR');

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

print(s2);

Map.centerObject(s2);
Map.addLayer(s2, imageVisParam, 's2');

var s2_new = s2_col.filterDate('2019','2022')
               .filterBounds(LGA)
               .select('B3');

var B3_max = s2_new.max().rename('B3_max')
                         .clip(LGA);

Map.addLayer(B3_max, imageVisParam, 'B3_max');

After running above code in GEE code editor, you can corroborate in Inspector tab that both loaded images are identical.

enter image description here

1
  • Great answer, thanks. I think I wasn't clear enough in my question. I need to retain all bands in the output. So the inspector would read "s2: Image (13 bands)". (I modified the question to reflect this comment.)
    – GlenS
    Commented Jul 3, 2022 at 22:23

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.