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