Without applying a filter function like the mode or similar, I want to apply a sieve to a raster with two classes.

Let's say I have a raster with pixel values 0 and 1; I want to remove groups of pixels smaller than 4. For instance, if I have two adjacent pixels with value 1 sounding by pixels with value 0, I want to assign value 0 to those two pixels in GEE. This would be equivalent to the sieve function in QGis.

Has anyone solved this problem?

1 Answer 1


If you only have 0 and 1, you can simply do this:

var minPixels = 4
var count = image.connectedPixelCount(minPixels)

var filtered = image
  .where(count.lt(minPixels), image.not())



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.