0

I am trying to create a mosaic of a several classified images using as criteria that every pixel has to be the same class at least for three images.

A similar process can be done using the .min() or .max() functions, that will return the pixel corresponding either to the min or max value. Example:

// Collection of classified images
var collection = ee.ImageCollection([image1, image2, image3, image4])

// Function to select a specific class
var selectClass = function(img) {
    var select = img.eq(2); //Select class 2
    var maskImg = img.mask(select); //Mask other classes and let only class 2
    return maskImg.unmask(0) //Assign a no data value to masked pixels
}

// Create new collection with only class 2
var collectionNew = collection.map(selectClass)

// Create mosaic using .min(). If there is at least one stacked pixel with the value 0, 
// then it will return this instead of class 2. 
var mosaic = collectionNew.min()

Now, what I want is to create a mosaic with the condition of returning the pixel value of class 2 if there is three or more pixels with the same value, otherwise return 0.

Is this possible in GEE using the JavaScript API?

1
  • If you wish to also ask about how to do this using the Python API then please do that in a separate question that includes a Python code attempt.
    – PolyGeo
    Oct 24 '20 at 22:15
1

I want to post the solution I found for this, using just math logics. If someone has a better suggestion please post it.

// Sum all the pixels, in this case this collection only has pixel values equal
// to 2 and 0
var sumCollection = collectionNew.sum();

// So, I'm interested on keeping only the pixels of class 2 that are present at
// least three times in the stack of images. In this example I have a stack of 4
// images, so the new pixel values according to their presence in the images is:

// Class 2 presence:
// *In 4 images: pixel value = 8
// *In 3 images: pixel value = 6
// *In 2 images: pixel values = 4
// *In 1 image: pixel value = 2
// *Any image: pixel value = 0

With this, I can filter the pixel values again using .gt(6) and then use it as a mask.

var condition = sumCollection.gt(6)
var finalCollection = sumCollection.updateMask(condition)

This was my solution. Kind of tricky.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.