2

I'm trying to create a mask of only pixels surrounded by zero-value pixels in the GHSL layer on Google Earth Engine.

The layer image would be: ee.Image('JRC/GHSL/P2016/POP_GPW_GLOBE_V1/2015')

Anyone have advice on how to do this?

1 Answer 1

2

You can use connectedPixelCount method:

var i = ee.Image('JRC/GHSL/P2016/POP_GPW_GLOBE_V1/2015')

// Show original
Map.addLayer(i, null, 'POP_GPW_GLOBE_V1')

// Image projection
var proj = i.projection()

// Mask out pixels with values not equal to zero
var mask = i.neq(0)
var masked = i.updateMask(mask).neq(0)

// get pixel's connectedness
var conn = masked.connectedPixelCount(2).reproject(proj)

// Mask where pixels surrounded by zeros have a value of one
var zeros_around = conn.eq(1)

// Show
Map.addLayer(zeros_around, null, 'pixels surrounded by zeros')
2
  • This works perfectly. Quick question: why is the connectedPixedCount equal to 2?
    – mdaniels
    Commented Oct 8, 2018 at 3:26
  • It's the maxSize parameter (the maximum size of the neighborhood in pixels). 2 is the minimum allowed, and as you care only for value 1 (itself), there's no need to set it higher than the minimum Commented Oct 8, 2018 at 10:43

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.