0

I have an image with 3 bands. I would like to count the number of pixels with unique band-value-combinations.

Example:

PIXELS       BAND1       BAND2       BAND3
pixel_1      22          17          1
pixel_2      12           6          1
pixel_3      12           6          1
pixel_4      56          17          4
pixel_5      9           17          2
pixel_6      22          17          1
pixel_7      22          17          1

The result here should be 4.

Can anyone help me?

1
  • 1
    Could you provide a working example?
    – M. Nicolas
    Feb 8, 2022 at 22:22

1 Answer 1

1

You can turn your image into an array image and reduce region with a countDistinct() reducer.

// Dummy setup, since you didn't provide a functioning script
var scale = 30
var region = ee.Geometry.Point([0, 0]).buffer(90).bounds()
var image = ee.Image([
  ee.Image.random(0).multiply(2).round().int8().rename('band1'),
  ee.Image.random(1).multiply(2).round().int8().rename('band2'),
  ee.Image.random(2).multiply(2).round().int8().rename('band3'),
]).reproject('EPSG:4326', null, scale).clip(region)


var count = image
  .toArray()
  .reduceRegion({
    reducer: ee.Reducer.countDistinct(),
    geometry: region, 
    scale: scale
  }).get('array')

print(count)
Map.addLayer(image, {min: 0, max: 2})
Map.centerObject(region)

https://code.earthengine.google.com/cb7a6c1cdd2368c655621db0259242c8

1
  • Hi Daniel, it works flawlessly, thank you so much!!
    – protea
    Feb 13, 2022 at 17:38

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.