I would like to use a function like argmax()
on pixels of an image collection, but see no corresponding reducer function in GEE. I think I need a custom reducer because, as far as I can tell this doesn't exist.
None of the provided reducers do what I need. I can see the "minimum" pixel. But what I want to know is which layer has the minimum pixel (so really I need an argmin()
, I guess
List here: https://developers.google.com/earth-engine/apidocs/ee-reducer-max
For example, say I want to run a any neighborhood analysis at multiple scales and want to know at what scale is stdev minimzed.
I can
- create an image collection of the analysis at each scale, then
- check each corresponding pixels and see what the maximum value is.
But I am looking to find out which layer has the maximum value not the maximum itself (ie, argmin()
not min()
).
Is there a reducer function I am missing, or is there a clever way to combine reducers to get there?
Here is what I have so far:
// get imagery in AOI
var aoi = ee.Geometry.Polygon(
[[[-84.10607,31.49740],
[-84.07174,31.49740],
[-84.07174,31.52689],
[-84.10607,31.52689],
[-84.10607,31.49740]]])
var img = ee.ImageCollection('USDA/NAIP/DOQQ')
.filterBounds(aoi)
.filter(ee.Filter.date('2015-01-01', '2018-09-10'))
.first()
.clip(aoi)
// convert image to monochrome
var makeMonochrome = function(image) {
var mono = image.expression('RED + BLUE + GREEN', {
'RED' : image.select('R'),
'BLUE' : image.select('B'),
'GREEN' : image.select('G')
});
return image.addBands(mono.rename('MONO'));
};
// focal analysis function for different scales
var myfun = function(img, dim) {
var kern = ee.Kernel.circle(dim)
var texture = img.reduceNeighborhood({
reducer: ee.Reducer.stdDev(),
kernel: kern,
});
return(texture)
}
img = makeMonochrome(img).select('MONO')
var scales = ee.Array([5, 10, 15])
var coll = scales.toList().map(function(scale) {return myfun(img, scale)})
coll = ee.ImageCollection(coll)
print(coll, 'collection')
// Gives the maximum variance... But I want to know *at which scale* max variance occurs
var reduced_coll = coll.reduce(ee.Reducer.max()); \\not what i need
//var reduced_coll = coll.reduce(ee.Reducer.argmax()); \\ doesn't exist
Map.addLayer(reduced_coll,{min: 0,max: 100}, 'reduced')
print(reduced_coll)
/// If argmax could be applied as a reducer and return the layeer number with the highest value, that would work fine for me!
print(scales.argmax()) // but i need min