# Obtain percentiles from an image in Google Earth Engine [closed]

Suppose I have a 1-band image that represents a species' predicted density and distribution. I'd like to be able to spatially define regions based on density. For instance I'd like to extract pixels that represent the top 10% of of the population. That is, identify the largest valued pixels that when summed are 10% of the total sum of all pixels. Or top 20%, top 30% and so on.

I don't know if this is the best way but in my mind I see the workflow as the following possibility: 1) convert image to array, 2) sort those values from min to max and calculate a cumulative sum, 4) convert back to an image and divide by the maximum value...now any pixel value greater than .9 represents a pixel in the top 10%.

This is the script I've been playing with.

## closed as off-topic by PolyGeo♦Jun 26 '18 at 10:35

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

You can use the `.percentile` reducer in combination with `.reduceRegion` to get the percentile values of all pixels within your study area.

With those you can mask values higher/lower than the desired percentile.

Here is an example calculating the percentiles at reduced resolution of 300m and masking the top 70%: https://code.earthengine.google.com/1ed53b53c8712fad7bedbc031c0022e2

``````var abundance_perc = bird_abundance.reduceRegion(ee.Reducer.percentile([10, 20, 30, 40, 50, 60, 70, 80, 90]), geometry, 300)
print(abundance_perc)

var top70 = bird_abundance.gte(ee.Number(abundance_perc.get("landcover_p70")))